@@ -33,7 +33,7 @@ await Management.Queue(spec).Declare()
3333/// AmqpConnection is the concrete implementation of <see cref="IConnection"/>
3434/// It is a wrapper around the AMQP.Net Lite <see cref="Connection"/> class
3535/// </summary>
36- public class AmqpConnection : AbstractClosable , IConnection
36+ public class AmqpConnection : AbstractResourceStatus , IConnection
3737{
3838 private const string ConnectionNotRecoveredCode = "CONNECTION_NOT_RECOVERED" ;
3939 private const string ConnectionNotRecoveredMessage = "Connection not recovered" ;
@@ -45,7 +45,9 @@ public class AmqpConnection : AbstractClosable, IConnection
4545
4646 private readonly AmqpManagement _management = new ( ) ;
4747 private readonly RecordingTopologyListener _recordingTopologyListener = new ( ) ;
48- private readonly TaskCompletionSource < bool > _connectionCloseTaskCompletionSource = new ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
48+
49+ private readonly TaskCompletionSource < bool > _connectionCloseTaskCompletionSource =
50+ new ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
4951
5052 private readonly ConnectionSettings _connectionSettings ;
5153 internal readonly AmqpSessionManagement NativePubSubSessions ;
@@ -60,9 +62,12 @@ public class AmqpConnection : AbstractClosable, IConnection
6062 /// They key is the publisher Id ( a Guid)
6163 /// See <see cref="AmqpPublisher"/>
6264 /// </summary>
63- internal ConcurrentDictionary < string , AmqpPublisher > Publishers { get ; } = new ( ) ;
65+ internal ConcurrentDictionary < string , IPublisher > Publishers { get ; } = new ( ) ;
66+
67+ internal ConcurrentDictionary < string , IConsumer > Consumers { get ; } = new ( ) ;
68+
6469
65- public ReadOnlyCollection < AmqpPublisher > GetPublishers ( )
70+ public ReadOnlyCollection < IPublisher > GetPublishers ( )
6671 {
6772 return Publishers . Values . ToList ( ) . AsReadOnly ( ) ;
6873 }
@@ -75,7 +80,7 @@ public ReadOnlyCollection<AmqpPublisher> GetPublishers()
7580 /// </summary>
7681 /// <param name="connectionSettings"></param>
7782 /// <returns></returns>
78- public static async Task < AmqpConnection > CreateAsync ( ConnectionSettings connectionSettings )
83+ public static async Task < IConnection > CreateAsync ( ConnectionSettings connectionSettings )
7984 {
8085 var connection = new AmqpConnection ( connectionSettings ) ;
8186 await connection . ConnectAsync ( )
@@ -105,15 +110,26 @@ private void ResumeAllPublishers()
105110 /// </summary>
106111 private async Task CloseAllPublishers ( )
107112 {
108- var cloned = new List < AmqpPublisher > ( Publishers . Values ) ;
113+ var cloned = new List < IPublisher > ( Publishers . Values ) ;
109114
110- foreach ( AmqpPublisher publisher in cloned )
115+ foreach ( IPublisher publisher in cloned )
111116 {
112117 await publisher . CloseAsync ( )
113118 . ConfigureAwait ( false ) ;
114119 }
115120 }
116121
122+ private async Task CloseAllConsumers ( )
123+ {
124+ var cloned = new List < IConsumer > ( Consumers . Values ) ;
125+
126+ foreach ( IConsumer consumer in cloned )
127+ {
128+ await consumer . CloseAsync ( )
129+ . ConfigureAwait ( false ) ;
130+ }
131+ }
132+
117133 private AmqpConnection ( ConnectionSettings connectionSettings )
118134 {
119135 _connectionSettings = connectionSettings ;
@@ -125,7 +141,12 @@ public IManagement Management()
125141 return _management ;
126142 }
127143
128- public Task ConnectAsync ( )
144+ public IConsumerBuilder ConsumerBuilder ( )
145+ {
146+ return new AmqpConsumerBuilder ( this ) ;
147+ }
148+
149+ private Task ConnectAsync ( )
129150 {
130151 EnsureConnection ( ) ;
131152 OnNewStatus ( State . Open , null ) ;
@@ -288,7 +309,7 @@ await _recordingTopologyListener.Accept(visitor)
288309 {
289310 _semaphoreClose . Release ( ) ;
290311 }
291-
312+
292313 _connectionCloseTaskCompletionSource . SetResult ( true ) ;
293314 } ;
294315 }
@@ -306,14 +327,14 @@ public IPublisherBuilder PublisherBuilder()
306327 }
307328
308329
309- public override async Task CloseAsync ( )
330+ public async Task CloseAsync ( )
310331 {
311332 await _semaphoreClose . WaitAsync ( )
312333 . ConfigureAwait ( false ) ;
313334 try
314335 {
315- await CloseAllPublishers ( )
316- . ConfigureAwait ( false ) ;
336+ await CloseAllPublishers ( ) . ConfigureAwait ( false ) ;
337+ await CloseAllConsumers ( ) . ConfigureAwait ( false ) ;
317338
318339 _recordingTopologyListener . Clear ( ) ;
319340
@@ -337,10 +358,10 @@ await _management.CloseAsync()
337358 {
338359 _semaphoreClose . Release ( ) ;
339360 }
340-
361+
341362 await _connectionCloseTaskCompletionSource . Task . WaitAsync ( TimeSpan . FromSeconds ( 10 ) )
342363 . ConfigureAwait ( false ) ;
343-
364+
344365 OnNewStatus ( State . Closed , null ) ;
345366 }
346367
0 commit comments