@@ -76,8 +76,7 @@ public class AutorecoveringConnection : IConnection, IRecoverable
76
76
protected ConcurrentDictionary < RecordedBinding , byte > m_recordedBindings =
77
77
new ConcurrentDictionary < RecordedBinding , byte > ( ) ;
78
78
79
- protected List < EventHandler < ConnectionBlockedEventArgs > > m_recordedBlockedEventHandlers =
80
- new List < EventHandler < ConnectionBlockedEventArgs > > ( ) ;
79
+ protected EventHandler < ConnectionBlockedEventArgs > m_recordedBlockedEventHandlers ;
81
80
82
81
protected IDictionary < string , RecordedConsumer > m_recordedConsumers =
83
82
new ConcurrentDictionary < string , RecordedConsumer > ( ) ;
@@ -88,11 +87,9 @@ public class AutorecoveringConnection : IConnection, IRecoverable
88
87
protected IDictionary < string , RecordedQueue > m_recordedQueues =
89
88
new ConcurrentDictionary < string , RecordedQueue > ( ) ;
90
89
91
- protected List < EventHandler < ShutdownEventArgs > > m_recordedShutdownEventHandlers =
92
- new List < EventHandler < ShutdownEventArgs > > ( ) ;
90
+ protected EventHandler < ShutdownEventArgs > m_recordedShutdownEventHandlers ;
93
91
94
- protected List < EventHandler < EventArgs > > m_recordedUnblockedEventHandlers =
95
- new List < EventHandler < EventArgs > > ( ) ;
92
+ protected EventHandler < EventArgs > m_recordedUnblockedEventHandlers ;
96
93
97
94
private EventHandler < ConsumerTagChangedAfterRecoveryEventArgs > m_consumerTagChange ;
98
95
private EventHandler < QueueNameChangedAfterRecoveryEventArgs > m_queueNameChange ;
@@ -183,15 +180,15 @@ public event EventHandler<ConnectionBlockedEventArgs> ConnectionBlocked
183
180
{
184
181
lock ( m_eventLock )
185
182
{
186
- m_recordedBlockedEventHandlers . Add ( value ) ;
183
+ m_recordedBlockedEventHandlers += value ;
187
184
m_delegate . ConnectionBlocked += value ;
188
185
}
189
186
}
190
187
remove
191
188
{
192
189
lock ( m_eventLock )
193
190
{
194
- m_recordedBlockedEventHandlers . Remove ( value ) ;
191
+ m_recordedBlockedEventHandlers -= value ;
195
192
m_delegate . ConnectionBlocked -= value ;
196
193
}
197
194
}
@@ -203,15 +200,15 @@ public event EventHandler<ShutdownEventArgs> ConnectionShutdown
203
200
{
204
201
lock ( m_eventLock )
205
202
{
206
- m_recordedShutdownEventHandlers . Add ( value ) ;
203
+ m_recordedShutdownEventHandlers += value ;
207
204
m_delegate . ConnectionShutdown += value ;
208
205
}
209
206
}
210
207
remove
211
208
{
212
209
lock ( m_eventLock )
213
210
{
214
- m_recordedShutdownEventHandlers . Remove ( value ) ;
211
+ m_recordedShutdownEventHandlers -= value ;
215
212
m_delegate . ConnectionShutdown -= value ;
216
213
}
217
214
}
@@ -223,15 +220,15 @@ public event EventHandler<EventArgs> ConnectionUnblocked
223
220
{
224
221
lock ( m_eventLock )
225
222
{
226
- m_recordedUnblockedEventHandlers . Add ( value ) ;
223
+ m_recordedUnblockedEventHandlers += value ;
227
224
m_delegate . ConnectionUnblocked += value ;
228
225
}
229
226
}
230
227
remove
231
228
{
232
229
lock ( m_eventLock )
233
230
{
234
- m_recordedUnblockedEventHandlers . Remove ( value ) ;
231
+ m_recordedUnblockedEventHandlers -= value ;
235
232
m_delegate . ConnectionUnblocked -= value ;
236
233
}
237
234
}
@@ -492,7 +489,7 @@ public void DeleteRecordedExchange(string name)
492
489
// find bindings that need removal, check if some auto-delete exchanges
493
490
// might need the same
494
491
var bs = m_recordedBindings . Keys . Where ( b => name . Equals ( b . Destination ) ) ;
495
- foreach ( RecordedBinding b in bs )
492
+ foreach ( var b in bs )
496
493
{
497
494
DeleteRecordedBinding ( b ) ;
498
495
MaybeDeleteRecordedAutoDeleteExchange ( b . Source ) ;
@@ -508,7 +505,7 @@ public void DeleteRecordedQueue(string name)
508
505
// find bindings that need removal, check if some auto-delete exchanges
509
506
// might need the same
510
507
var bs = m_recordedBindings . Keys . Where ( b => name . Equals ( b . Destination ) ) ;
511
- foreach ( RecordedBinding b in bs )
508
+ foreach ( var b in bs )
512
509
{
513
510
DeleteRecordedBinding ( b ) ;
514
511
MaybeDeleteRecordedAutoDeleteExchange ( b . Source ) ;
@@ -651,10 +648,7 @@ private void Init(IFrameHandler fh)
651
648
lock ( m_eventLock )
652
649
{
653
650
ConnectionShutdown += recoveryListener ;
654
- if ( ! m_recordedShutdownEventHandlers . Contains ( recoveryListener ) )
655
- {
656
- m_recordedShutdownEventHandlers . Add ( recoveryListener ) ;
657
- }
651
+ m_recordedShutdownEventHandlers += recoveryListener ;
658
652
}
659
653
}
660
654
@@ -747,18 +741,33 @@ public void HandleConnectionUnblocked()
747
741
748
742
void IDisposable . Dispose ( )
749
743
{
750
- try
751
- {
752
- Abort ( ) ;
753
- }
754
- catch ( Exception )
755
- {
756
- // TODO: log
757
- }
758
- finally
744
+ Dispose ( true ) ;
745
+ GC . SuppressFinalize ( this ) ;
746
+ }
747
+
748
+ protected virtual void Dispose ( bool disposing )
749
+ {
750
+ if ( disposing )
759
751
{
760
- m_models . Clear ( ) ;
752
+ // dispose managed resources
753
+ try
754
+ {
755
+ Abort ( ) ;
756
+ }
757
+ catch ( Exception )
758
+ {
759
+ // TODO: log
760
+ }
761
+ finally
762
+ {
763
+ m_models . Clear ( ) ;
764
+ m_recordedBlockedEventHandlers = null ;
765
+ m_recordedShutdownEventHandlers = null ;
766
+ m_recordedUnblockedEventHandlers = null ;
767
+ }
761
768
}
769
+
770
+ // dispose unmanaged resources
762
771
}
763
772
764
773
protected void EnsureIsOpen ( )
@@ -815,13 +824,9 @@ protected void RecoverBindings()
815
824
816
825
protected void RecoverConnectionBlockedHandlers ( )
817
826
{
818
- List < EventHandler < ConnectionBlockedEventArgs > > handler = m_recordedBlockedEventHandlers ;
819
- if ( handler != null )
827
+ lock ( m_eventLock )
820
828
{
821
- foreach ( EventHandler < ConnectionBlockedEventArgs > eh in handler )
822
- {
823
- m_delegate . ConnectionBlocked += eh ;
824
- }
829
+ m_delegate . ConnectionBlocked += m_recordedBlockedEventHandlers ;
825
830
}
826
831
}
827
832
@@ -871,22 +876,12 @@ protected bool RecoverConnectionDelegate()
871
876
872
877
protected void RecoverConnectionShutdownHandlers ( )
873
878
{
874
- foreach ( EventHandler < ShutdownEventArgs > eh in m_recordedShutdownEventHandlers )
875
- {
876
- m_delegate . ConnectionShutdown += eh ;
877
- }
879
+ m_delegate . ConnectionShutdown += m_recordedShutdownEventHandlers ;
878
880
}
879
881
880
882
protected void RecoverConnectionUnblockedHandlers ( )
881
883
{
882
- List < EventHandler < EventArgs > > handler = m_recordedUnblockedEventHandlers ;
883
- if ( handler != null )
884
- {
885
- foreach ( EventHandler < EventArgs > eh in handler )
886
- {
887
- m_delegate . ConnectionUnblocked += eh ;
888
- }
889
- }
884
+ m_delegate . ConnectionUnblocked += m_recordedUnblockedEventHandlers ;
890
885
}
891
886
892
887
protected void RecoverConsumers ( )
@@ -1054,8 +1049,8 @@ protected void RunRecoveryEventHandlers()
1054
1049
protected bool ShouldTriggerConnectionRecovery ( ShutdownEventArgs args )
1055
1050
{
1056
1051
return ( args . Initiator == ShutdownInitiator . Peer ||
1057
- // happens when EOF is reached, e.g. due to RabbitMQ node
1058
- // connectivity loss or abrupt shutdown
1052
+ // happens when EOF is reached, e.g. due to RabbitMQ node
1053
+ // connectivity loss or abrupt shutdown
1059
1054
args . Initiator == ShutdownInitiator . Library ) ;
1060
1055
}
1061
1056
}
0 commit comments