@@ -46,6 +46,55 @@ public class AmqpConnection : AbstractLifeCycle, IConnection
4646 private readonly AmqpManagement _management ;
4747 private readonly RecordingTopologyListener _recordingTopologyListener = new ( ) ;
4848
49+ private void ChangeEntitiesStatus ( State state , Error ? error )
50+ {
51+ ChangePublishersStatus ( state , error ) ;
52+ ChangeConsumersStatus ( state , error ) ;
53+ _management . ChangeStatus ( state , error ) ;
54+ }
55+
56+ private void ChangePublishersStatus ( State state , Error ? error )
57+ {
58+ foreach ( var publisher1 in Publishers . Values )
59+ {
60+ var publisher = ( AmqpPublisher ) publisher1 ;
61+ publisher . ChangeStatus ( state , error ) ;
62+ }
63+ }
64+
65+ private void ChangeConsumersStatus ( State state , Error ? error )
66+ {
67+ foreach ( var consumer1 in Consumers . Values )
68+ {
69+ var consumer = ( AmqpConsumer ) consumer1 ;
70+ consumer . ChangeStatus ( state , error ) ;
71+ }
72+ }
73+
74+
75+ private async Task ReconnectEntities ( )
76+ {
77+ await ReconnectPublishers ( ) . ConfigureAwait ( false ) ;
78+ await ReconnectConsumers ( ) . ConfigureAwait ( false ) ;
79+ }
80+
81+ private async Task ReconnectPublishers ( )
82+ {
83+ foreach ( var publisher1 in Publishers . Values )
84+ {
85+ var publisher = ( AmqpPublisher ) publisher1 ;
86+ await publisher . Reconnect ( ) . ConfigureAwait ( false ) ;
87+ }
88+ }
89+
90+ private async Task ReconnectConsumers ( )
91+ {
92+ foreach ( var consumer1 in Consumers . Values )
93+ {
94+ var consumer = ( AmqpConsumer ) consumer1 ;
95+ await consumer . Reconnect ( ) . ConfigureAwait ( false ) ;
96+ }
97+ }
4998
5099 private readonly ConnectionSettings _connectionSettings ;
51100 internal readonly AmqpSessionManagement _nativePubSubSessions ;
@@ -138,7 +187,6 @@ protected override Task OpenAsync()
138187
139188 private void EnsureConnection ( )
140189 {
141- // await _semaphore.WaitAsync();
142190 try
143191 {
144192 if ( _nativeConnection is { IsClosed : false } )
@@ -180,14 +228,6 @@ [new Symbol("connection_name")] = _connectionSettings.ConnectionName(),
180228 Trace . WriteLine ( TraceLevel . Error , $ "Error trying to connect. Info: { ToString ( ) } , error: { e } ") ;
181229 throw new ConnectionException ( $ "Error trying to connect. Info: { ToString ( ) } , error: { e } ") ;
182230 }
183-
184-
185- finally
186- {
187- // _semaphore.Release();
188- }
189-
190- // return Task.CompletedTask;
191231 }
192232
193233 /// <summary>
@@ -205,8 +245,12 @@ private ClosedCallback MaybeRecoverConnection()
205245
206246 try
207247 {
248+ // close all the sessions, if the connection is closed the sessions are not valid anymore
249+ _nativePubSubSessions . ClearSessions ( ) ;
250+
208251 if ( error != null )
209252 {
253+ // we assume here that the connection is closed unexpectedly, since the error is not null
210254 Trace . WriteLine ( TraceLevel . Warning , $ "connection is closed unexpectedly. " +
211255 $ "Info: { ToString ( ) } ") ;
212256
@@ -216,11 +260,16 @@ private ClosedCallback MaybeRecoverConnection()
216260 if ( ! _connectionSettings . RecoveryConfiguration . IsActivate ( ) )
217261 {
218262 OnNewStatus ( State . Closed , Utils . ConvertError ( error ) ) ;
263+ ChangeEntitiesStatus ( State . Closed , Utils . ConvertError ( error ) ) ;
219264 return ;
220265 }
221266
222- // TODO: Block the publishers and consumers
267+ // change the status for the connection and all the entities
268+ // to reconnecting and all the events are fired
223269 OnNewStatus ( State . Reconnecting , Utils . ConvertError ( error ) ) ;
270+ ChangeEntitiesStatus ( State . Reconnecting , Utils . ConvertError ( error ) ) ;
271+
272+
224273 await Task . Run ( async ( ) =>
225274 {
226275 bool connected = false ;
@@ -266,6 +315,10 @@ await Task.Delay(TimeSpan.FromMilliseconds(next))
266315 OnNewStatus ( State . Closed ,
267316 new Error ( ConnectionNotRecoveredCode ,
268317 $ "{ ConnectionNotRecoveredMessage } , recover status: { _connectionSettings . RecoveryConfiguration } ") ) ;
318+
319+ ChangeEntitiesStatus ( State . Closed , new Error ( ConnectionNotRecoveredCode ,
320+ $ "{ ConnectionNotRecoveredMessage } , recover status: { _connectionSettings . RecoveryConfiguration } ") ) ;
321+
269322 return ;
270323 }
271324
@@ -278,9 +331,10 @@ await _recordingTopologyListener.Accept(visitor)
278331 }
279332
280333 OnNewStatus ( State . Open , null ) ;
281- } ) . ConfigureAwait ( false ) ;
282-
334+ // after the connection is recovered we have to reconnect all the publishers and consumers
283335
336+ await ReconnectEntities ( ) . ConfigureAwait ( false ) ;
337+ } ) . ConfigureAwait ( false ) ;
284338 return ;
285339 }
286340
@@ -320,6 +374,7 @@ await _semaphoreClose.WaitAsync()
320374 await CloseAllConsumers ( ) . ConfigureAwait ( false ) ;
321375
322376 _recordingTopologyListener . Clear ( ) ;
377+ _nativePubSubSessions . ClearSessions ( ) ;
323378
324379 if ( State == State . Closed )
325380 {
0 commit comments