3737import java .util .concurrent .TimeoutException ;
3838import java .util .concurrent .locks .Lock ;
3939import java .util .concurrent .locks .ReentrantLock ;
40+ import java .util .function .Predicate ;
4041
4142/**
4243 * Connection implementation that performs automatic recovery when
6162 */
6263public class AutorecoveringConnection implements RecoverableConnection , NetworkConnection {
6364
65+ public static final Predicate <ShutdownSignalException > DEFAULT_CONNECTION_RECOVERY_TRIGGERING_CONDITION =
66+ cause -> !cause .isInitiatedByApplication () || (cause .getCause () instanceof MissedHeartbeatException );
67+
6468 private static final Logger LOGGER = LoggerFactory .getLogger (AutorecoveringConnection .class );
6569
6670 private final RecoveryAwareAMQConnectionFactory cf ;
@@ -87,6 +91,8 @@ public class AutorecoveringConnection implements RecoverableConnection, NetworkC
8791 // be created after application code has initiated shutdown.
8892 private final Object recoveryLock = new Object ();
8993
94+ private final Predicate <ShutdownSignalException > connectionRecoveryTriggeringCondition ;
95+
9096 public AutorecoveringConnection (ConnectionParams params , FrameHandlerFactory f , List <Address > addrs ) {
9197 this (params , f , new ListAddressResolver (addrs ));
9298 }
@@ -99,9 +105,14 @@ public AutorecoveringConnection(ConnectionParams params, FrameHandlerFactory f,
99105 this .cf = new RecoveryAwareAMQConnectionFactory (params , f , addressResolver , metricsCollector );
100106 this .params = params ;
101107
108+ this .connectionRecoveryTriggeringCondition = params .getConnectionRecoveryTriggeringCondition () == null ?
109+ DEFAULT_CONNECTION_RECOVERY_TRIGGERING_CONDITION : params .getConnectionRecoveryTriggeringCondition ();
110+
111+ System .out .println (this .connectionRecoveryTriggeringCondition );
112+
102113 setupErrorOnWriteListenerForPotentialRecovery ();
103114
104- this .channels = new ConcurrentHashMap <Integer , AutorecoveringChannel >();
115+ this .channels = new ConcurrentHashMap <>();
105116 }
106117
107118 private void setupErrorOnWriteListenerForPotentialRecovery () {
@@ -484,16 +495,13 @@ private void addAutomaticRecoveryListener(final RecoveryAwareAMQConnection newCo
484495 final AutorecoveringConnection c = this ;
485496 // this listener will run after shutdown listeners,
486497 // see https://github.com/rabbitmq/rabbitmq-java-client/issues/135
487- RecoveryCanBeginListener starter = new RecoveryCanBeginListener () {
488- @ Override
489- public void recoveryCanBegin (ShutdownSignalException cause ) {
490- try {
491- if (shouldTriggerConnectionRecovery (cause )) {
492- c .beginAutomaticRecovery ();
493- }
494- } catch (Exception e ) {
495- newConn .getExceptionHandler ().handleConnectionRecoveryException (c , e );
498+ RecoveryCanBeginListener starter = cause -> {
499+ try {
500+ if (shouldTriggerConnectionRecovery (cause )) {
501+ c .beginAutomaticRecovery ();
496502 }
503+ } catch (Exception e ) {
504+ newConn .getExceptionHandler ().handleConnectionRecoveryException (c , e );
497505 }
498506 };
499507 synchronized (this ) {
@@ -502,7 +510,7 @@ public void recoveryCanBegin(ShutdownSignalException cause) {
502510 }
503511
504512 protected boolean shouldTriggerConnectionRecovery (ShutdownSignalException cause ) {
505- return ! cause . isInitiatedByApplication () || ( cause . getCause () instanceof MissedHeartbeatException );
513+ return connectionRecoveryTriggeringCondition . test ( cause );
506514 }
507515
508516 /**
0 commit comments