|
4 | 4 | using System; |
5 | 5 | using System.Collections.Concurrent; |
6 | 6 | using Azure.Core; |
| 7 | +using Azure.Messaging.EventHubs; |
7 | 8 | using Azure.Messaging.EventHubs.Consumer; |
8 | 9 | using Azure.Messaging.EventHubs.Producer; |
9 | 10 | using Azure.Storage.Blobs; |
@@ -48,37 +49,36 @@ internal EventHubProducerClient GetEventHubProducerClient(string eventHubName, s |
48 | 49 | eventHubName = _nameResolver.ResolveWholeString(eventHubName); |
49 | 50 | connection = _nameResolver.ResolveWholeString(connection); |
50 | 51 |
|
51 | | - return _producerCache.GetOrAdd(eventHubName, key => |
| 52 | + if (!string.IsNullOrWhiteSpace(connection)) |
52 | 53 | { |
53 | | - if (!string.IsNullOrWhiteSpace(connection)) |
| 54 | + var info = ResolveConnectionInformation(connection); |
| 55 | + var eventHubProducerClientOptions = new EventHubProducerClientOptions |
54 | 56 | { |
55 | | - var info = ResolveConnectionInformation(connection); |
56 | | - |
57 | | - if (info.FullyQualifiedEndpoint != null && |
58 | | - info.TokenCredential != null) |
59 | | - { |
60 | | - return new EventHubProducerClient( |
61 | | - info.FullyQualifiedEndpoint, |
62 | | - eventHubName, |
63 | | - info.TokenCredential, |
64 | | - new EventHubProducerClientOptions |
65 | | - { |
66 | | - RetryOptions = _options.ClientRetryOptions, |
67 | | - ConnectionOptions = _options.ConnectionOptions |
68 | | - }); |
69 | | - } |
| 57 | + RetryOptions = _options.ClientRetryOptions, |
| 58 | + ConnectionOptions = _options.ConnectionOptions |
| 59 | + }; |
70 | 60 |
|
71 | | - return new EventHubProducerClient( |
72 | | - NormalizeConnectionString(info.ConnectionString, eventHubName), |
73 | | - new EventHubProducerClientOptions |
74 | | - { |
75 | | - RetryOptions = _options.ClientRetryOptions, |
76 | | - ConnectionOptions = _options.ConnectionOptions |
77 | | - }); |
| 61 | + EventHubConnection eventHubConnection; |
| 62 | + |
| 63 | + if (info.FullyQualifiedEndpoint != null && |
| 64 | + info.TokenCredential != null) |
| 65 | + { |
| 66 | + eventHubConnection = new EventHubConnection(info.FullyQualifiedEndpoint, eventHubName, info.TokenCredential, eventHubProducerClientOptions.ConnectionOptions); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + eventHubConnection = new EventHubConnection(NormalizeConnectionString(info.ConnectionString, eventHubName), eventHubProducerClientOptions.ConnectionOptions); |
78 | 71 | } |
79 | 72 |
|
80 | | - throw new InvalidOperationException("No event hub sender named " + eventHubName); |
81 | | - }); |
| 73 | + return _producerCache.GetOrAdd(GenerateCacheKey(eventHubConnection), key => |
| 74 | + { |
| 75 | + return new EventHubProducerClient( |
| 76 | + eventHubConnection, |
| 77 | + eventHubProducerClientOptions); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + throw new InvalidOperationException("No event hub sender named " + eventHubName); |
82 | 82 | } |
83 | 83 |
|
84 | 84 | internal EventProcessorHost GetEventProcessorHost(string eventHubName, string connection, string consumerGroup, bool singleDispatch) |
@@ -123,48 +123,36 @@ internal IEventHubConsumerClient GetEventHubConsumerClient(string eventHubName, |
123 | 123 | connection = _nameResolver.ResolveWholeString(connection); |
124 | 124 | consumerGroup = _nameResolver.ResolveWholeString(consumerGroup); |
125 | 125 |
|
126 | | - return _consumerCache.GetOrAdd(eventHubName, name => |
| 126 | + if (!string.IsNullOrEmpty(connection)) |
127 | 127 | { |
128 | | - EventHubConsumerClient client = null; |
129 | | - |
130 | | - if (!string.IsNullOrEmpty(connection)) |
| 128 | + var info = ResolveConnectionInformation(connection); |
| 129 | + var eventHubConsumerClientOptions = new EventHubConsumerClientOptions |
131 | 130 | { |
132 | | - var info = ResolveConnectionInformation(connection); |
| 131 | + RetryOptions = _options.ClientRetryOptions, |
| 132 | + ConnectionOptions = _options.ConnectionOptions |
| 133 | + }; |
133 | 134 |
|
134 | | - if (info.FullyQualifiedEndpoint != null && |
135 | | - info.TokenCredential != null) |
136 | | - { |
137 | | - client = new EventHubConsumerClient( |
138 | | - consumerGroup, |
139 | | - info.FullyQualifiedEndpoint, |
140 | | - eventHubName, |
141 | | - info.TokenCredential, |
142 | | - new EventHubConsumerClientOptions |
143 | | - { |
144 | | - RetryOptions = _options.ClientRetryOptions, |
145 | | - ConnectionOptions = _options.ConnectionOptions |
146 | | - }); |
147 | | - } |
148 | | - else |
149 | | - { |
150 | | - client = new EventHubConsumerClient( |
151 | | - consumerGroup, |
152 | | - NormalizeConnectionString(info.ConnectionString, eventHubName), |
153 | | - new EventHubConsumerClientOptions |
154 | | - { |
155 | | - RetryOptions = _options.ClientRetryOptions, |
156 | | - ConnectionOptions = _options.ConnectionOptions |
157 | | - }); |
158 | | - } |
159 | | - } |
| 135 | + EventHubConnection eventHubConnection; |
160 | 136 |
|
161 | | - if (client != null) |
| 137 | + if (info.FullyQualifiedEndpoint != null && |
| 138 | + info.TokenCredential != null) |
162 | 139 | { |
163 | | - return new EventHubConsumerClientImpl(client); |
| 140 | + eventHubConnection = new EventHubConnection(info.FullyQualifiedEndpoint, eventHubName, info.TokenCredential, eventHubConsumerClientOptions.ConnectionOptions); |
164 | 141 | } |
| 142 | + else |
| 143 | + { |
| 144 | + eventHubConnection = new EventHubConnection(NormalizeConnectionString(info.ConnectionString, eventHubName), eventHubConsumerClientOptions.ConnectionOptions); |
| 145 | + } |
| 146 | + |
| 147 | + return _consumerCache.GetOrAdd(GenerateCacheKey(eventHubConnection, consumerGroup), key => |
| 148 | + new EventHubConsumerClientImpl( |
| 149 | + new EventHubConsumerClient( |
| 150 | + consumerGroup, |
| 151 | + eventHubConnection, |
| 152 | + eventHubConsumerClientOptions))); |
| 153 | + } |
165 | 154 |
|
166 | | - throw new InvalidOperationException("No event hub receiver named " + eventHubName); |
167 | | - }); |
| 155 | + throw new InvalidOperationException("No event hub receiver named " + eventHubName); |
168 | 156 | } |
169 | 157 |
|
170 | 158 | internal BlobContainerClient GetCheckpointStoreClient() |
@@ -212,6 +200,11 @@ private EventHubsConnectionInformation ResolveConnectionInformation(string conne |
212 | 200 | return new EventHubsConnectionInformation(fullyQualifiedNamespace, credential); |
213 | 201 | } |
214 | 202 |
|
| 203 | + private static string GenerateCacheKey(EventHubConnection eventHubConnection, string consumerGroup = null) => |
| 204 | + consumerGroup == null |
| 205 | + ? $"{eventHubConnection.FullyQualifiedNamespace}/{eventHubConnection.EventHubName}" |
| 206 | + : $"{eventHubConnection.FullyQualifiedNamespace}/{eventHubConnection.EventHubName}/{consumerGroup}"; |
| 207 | + |
215 | 208 | private record EventHubsConnectionInformation |
216 | 209 | { |
217 | 210 | public EventHubsConnectionInformation(string connectionString) |
|
0 commit comments