|
7 | 7 | import com.rabbitmq.client.Connection;
|
8 | 8 | import com.rabbitmq.client.MissedHeartbeatException;
|
9 | 9 | import com.rabbitmq.client.Recoverable;
|
| 10 | +import com.rabbitmq.client.RecoverableConnection; |
| 11 | +import com.rabbitmq.client.RecoverableShutdownSignalException; |
10 | 12 | import com.rabbitmq.client.RecoveryListener;
|
11 | 13 | import com.rabbitmq.client.ShutdownListener;
|
12 | 14 | import com.rabbitmq.client.ShutdownSignalException;
|
13 | 15 | import com.rabbitmq.client.TopologyRecoveryException;
|
14 | 16 | import com.rabbitmq.client.impl.ConnectionParams;
|
15 | 17 | import com.rabbitmq.client.ExceptionHandler;
|
| 18 | +import com.rabbitmq.client.impl.ForwardingShutdownNotifier; |
16 | 19 | import com.rabbitmq.client.impl.FrameHandlerFactory;
|
17 | 20 | import com.rabbitmq.client.impl.NetworkConnection;
|
18 | 21 |
|
@@ -56,7 +59,7 @@ public class AutorecoveringConnection implements Connection, Recoverable, Networ
|
56 | 59 | private final ConnectionParams params;
|
57 | 60 | private RecoveryAwareAMQConnection delegate;
|
58 | 61 |
|
59 |
| - private final List<ShutdownListener> shutdownHooks = new ArrayList<ShutdownListener>(); |
| 62 | + private final ForwardingShutdownNotifier shutdownHooks = new ForwardingShutdownNotifier(); |
60 | 63 | private final List<RecoveryListener> recoveryListeners = new ArrayList<RecoveryListener>();
|
61 | 64 | private final List<BlockedListener> blockedListeners = new ArrayList<BlockedListener>();
|
62 | 65 |
|
@@ -299,16 +302,16 @@ public void close(int closeCode, String closeMessage) throws IOException {
|
299 | 302 | * @see Connection#addShutdownListener(com.rabbitmq.client.ShutdownListener)
|
300 | 303 | */
|
301 | 304 | public void addShutdownListener(ShutdownListener listener) {
|
302 |
| - this.shutdownHooks.add(listener); |
303 |
| - delegate.addShutdownListener(listener); |
| 305 | + this.shutdownHooks.addShutdownListener(listener); |
| 306 | + //delegate.addShutdownListener(listener); |
304 | 307 | }
|
305 | 308 |
|
306 | 309 | /**
|
307 | 310 | * @see com.rabbitmq.client.ShutdownNotifier#removeShutdownListener(com.rabbitmq.client.ShutdownListener)
|
308 | 311 | */
|
309 | 312 | public void removeShutdownListener(ShutdownListener listener) {
|
310 |
| - this.shutdownHooks.remove(listener); |
311 |
| - delegate.removeShutdownListener(listener); |
| 313 | + this.shutdownHooks.removeShutdownListener(listener); |
| 314 | + //delegate.removeShutdownListener(listener); |
312 | 315 | }
|
313 | 316 |
|
314 | 317 | /**
|
@@ -378,21 +381,20 @@ private void addAutomaticRecoveryListener() {
|
378 | 381 | final AutorecoveringConnection c = this;
|
379 | 382 | ShutdownListener automaticRecoveryListener = new ShutdownListener() {
|
380 | 383 | public void shutdownCompleted(ShutdownSignalException cause) {
|
| 384 | + boolean attemptRecovery = shouldTriggerConnectionRecovery(cause); |
| 385 | + //Send a notice to user added hooks. |
| 386 | + c.shutdownHooks.notifyListeners(new RecoverableShutdownSignalException(cause, attemptRecovery)); |
| 387 | + |
381 | 388 | try {
|
382 |
| - if (shouldTriggerConnectionRecovery(cause)) { |
| 389 | + if (attemptRecovery) { |
383 | 390 | c.beginAutomaticRecovery();
|
384 | 391 | }
|
385 | 392 | } catch (Exception e) {
|
386 | 393 | c.delegate.getExceptionHandler().handleConnectionRecoveryException(c, e);
|
387 | 394 | }
|
388 | 395 | }
|
389 | 396 | };
|
390 |
| - synchronized (this) { |
391 |
| - if(!this.shutdownHooks.contains(automaticRecoveryListener)) { |
392 |
| - this.shutdownHooks.add(automaticRecoveryListener); |
393 |
| - } |
394 |
| - this.delegate.addShutdownListener(automaticRecoveryListener); |
395 |
| - } |
| 397 | + this.delegate.addShutdownListener(automaticRecoveryListener); |
396 | 398 | }
|
397 | 399 |
|
398 | 400 | protected boolean shouldTriggerConnectionRecovery(ShutdownSignalException cause) {
|
@@ -456,9 +458,7 @@ synchronized private void beginAutomaticRecovery() throws InterruptedException,
|
456 | 458 | }
|
457 | 459 |
|
458 | 460 | private void recoverShutdownListeners() {
|
459 |
| - for (ShutdownListener sh : this.shutdownHooks) { |
460 |
| - this.delegate.addShutdownListener(sh); |
461 |
| - } |
| 461 | + addAutomaticRecoveryListener(); |
462 | 462 | }
|
463 | 463 |
|
464 | 464 | private void recoverBlockedListeners() {
|
|
0 commit comments