@@ -45,6 +45,56 @@ public class AmqpConnection : AbstractLifeCycle, IConnection
4545 private readonly AmqpManagement _management ;
4646 private readonly RecordingTopologyListener _recordingTopologyListener = new ( ) ;
4747
48+ private void ChangeEntitiesStatus ( State state , Error ? error )
49+ {
50+ ChangePublishersStatus ( state , error ) ;
51+ ChangeConsumersStatus ( state , error ) ;
52+ _management . ChangeStatus ( state , error ) ;
53+ }
54+
55+ private void ChangePublishersStatus ( State state , Error ? error )
56+ {
57+ foreach ( var publisher1 in Publishers . Values )
58+ {
59+ var publisher = ( AmqpPublisher ) publisher1 ;
60+ publisher . ChangeStatus ( state , error ) ;
61+ }
62+ }
63+
64+ private void ChangeConsumersStatus ( State state , Error ? error )
65+ {
66+ foreach ( var consumer1 in Consumers . Values )
67+ {
68+ var consumer = ( AmqpConsumer ) consumer1 ;
69+ consumer . ChangeStatus ( state , error ) ;
70+ }
71+ }
72+
73+
74+ private async Task ReconnectEntities ( )
75+ {
76+ await ReconnectPublishers ( ) . ConfigureAwait ( false ) ;
77+ await ReconnectConsumers ( ) . ConfigureAwait ( false ) ;
78+ }
79+
80+ private async Task ReconnectPublishers ( )
81+ {
82+ foreach ( var publisher1 in Publishers . Values )
83+ {
84+ var publisher = ( AmqpPublisher ) publisher1 ;
85+ await publisher . Reconnect ( ) . ConfigureAwait ( false ) ;
86+ }
87+ }
88+
89+ private async Task ReconnectConsumers ( )
90+ {
91+ foreach ( var consumer1 in Consumers . Values )
92+ {
93+ var consumer = ( AmqpConsumer ) consumer1 ;
94+ await consumer . Reconnect ( ) . ConfigureAwait ( false ) ;
95+ }
96+ }
97+
4898 private readonly ConnectionSettings _connectionSettings ;
4999 internal readonly AmqpSessionManagement _nativePubSubSessions ;
50100
@@ -210,14 +260,6 @@ void onOpened(Amqp.IConnection connection, Open open1)
210260 Trace . WriteLine ( TraceLevel . Error , $ "Error trying to connect. Info: { ToString ( ) } , error: { e } ") ;
211261 throw new ConnectionException ( $ "Error trying to connect. Info: { ToString ( ) } , error: { e } ") ;
212262 }
213-
214-
215- finally
216- {
217- // _semaphore.Release();
218- }
219-
220- // return Task.CompletedTask;
221263 }
222264
223265 /// <summary>
@@ -235,8 +277,12 @@ private ClosedCallback MaybeRecoverConnection()
235277
236278 try
237279 {
280+ // close all the sessions, if the connection is closed the sessions are not valid anymore
281+ _nativePubSubSessions . ClearSessions ( ) ;
282+
238283 if ( error != null )
239284 {
285+ // we assume here that the connection is closed unexpectedly, since the error is not null
240286 Trace . WriteLine ( TraceLevel . Warning , $ "connection is closed unexpectedly. " +
241287 $ "Info: { ToString ( ) } ") ;
242288
@@ -246,11 +292,16 @@ private ClosedCallback MaybeRecoverConnection()
246292 if ( ! _connectionSettings . RecoveryConfiguration . IsActivate ( ) )
247293 {
248294 OnNewStatus ( State . Closed , Utils . ConvertError ( error ) ) ;
295+ ChangeEntitiesStatus ( State . Closed , Utils . ConvertError ( error ) ) ;
249296 return ;
250297 }
251298
252- // TODO: Block the publishers and consumers
299+ // change the status for the connection and all the entities
300+ // to reconnecting and all the events are fired
253301 OnNewStatus ( State . Reconnecting , Utils . ConvertError ( error ) ) ;
302+ ChangeEntitiesStatus ( State . Reconnecting , Utils . ConvertError ( error ) ) ;
303+
304+
254305 await Task . Run ( async ( ) =>
255306 {
256307 bool connected = false ;
@@ -297,6 +348,10 @@ await EnsureConnection()
297348 OnNewStatus ( State . Closed ,
298349 new Error ( ConnectionNotRecoveredCode ,
299350 $ "{ ConnectionNotRecoveredMessage } , recover status: { _connectionSettings . RecoveryConfiguration } ") ) ;
351+
352+ ChangeEntitiesStatus ( State . Closed , new Error ( ConnectionNotRecoveredCode ,
353+ $ "{ ConnectionNotRecoveredMessage } , recover status: { _connectionSettings . RecoveryConfiguration } ") ) ;
354+
300355 return ;
301356 }
302357
@@ -309,9 +364,10 @@ await _recordingTopologyListener.Accept(visitor)
309364 }
310365
311366 OnNewStatus ( State . Open , null ) ;
312- } ) . ConfigureAwait ( false ) ;
313-
367+ // after the connection is recovered we have to reconnect all the publishers and consumers
314368
369+ await ReconnectEntities ( ) . ConfigureAwait ( false ) ;
370+ } ) . ConfigureAwait ( false ) ;
315371 return ;
316372 }
317373
@@ -351,6 +407,7 @@ await _semaphoreClose.WaitAsync()
351407 await CloseAllConsumers ( ) . ConfigureAwait ( false ) ;
352408
353409 _recordingTopologyListener . Clear ( ) ;
410+ _nativePubSubSessions . ClearSessions ( ) ;
354411
355412 if ( State == State . Closed )
356413 {
0 commit comments