Skip to content

Commit b3af29b

Browse files
committed
DefaultMessageListenerContainer invokes specified ExceptionListener for recovery exceptions as well
Also, DefaultMessageListenerContainer logs recovery failures at error level and exposes an "isRecovering()" method now. Issue: SPR-10230
1 parent 6d77f1c commit b3af29b

1 file changed

Lines changed: 35 additions & 8 deletions

File tree

‎spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -183,6 +183,8 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe
183183

184184
private int registeredWithDestination = 0;
185185

186+
private volatile boolean recovering = false;
187+
186188
private Runnable stopCallback;
187189

188190
private Object currentRecoveryMarker = new Object();
@@ -758,6 +760,9 @@ protected void establishSharedConnection() {
758760
super.establishSharedConnection();
759761
}
760762
catch (Exception ex) {
763+
if (ex instanceof JMSException) {
764+
invokeExceptionListener((JMSException) ex);
765+
}
761766
logger.debug("Could not establish shared JMS Connection - " +
762767
"leaving it up to asynchronous invokers to establish a Connection as soon as possible", ex);
763768
}
@@ -796,7 +801,7 @@ protected void stopSharedConnection() {
796801
/**
797802
* Handle the given exception that arose during setup of a listener.
798803
* Called for every such exception in every concurrent listener.
799-
* <p>The default implementation logs the exception at info level
804+
* <p>The default implementation logs the exception at warn level
800805
* if not recovered yet, and at debug level if already recovered.
801806
* Can be overridden in subclasses.
802807
* @param ex the exception to handle
@@ -837,7 +842,7 @@ protected void handleListenerSetupFailure(Throwable ex, boolean alreadyRecovered
837842

838843
/**
839844
* Recover this listener container after a listener failed to set itself up,
840-
* for example reestablishing the underlying Connection.
845+
* for example re-establishing the underlying Connection.
841846
* <p>The default implementation delegates to DefaultMessageListenerContainer's
842847
* recovery-capable {@link #refreshConnectionUntilSuccessful()} method, which will
843848
* try to re-establish a Connection to the JMS provider both for the shared
@@ -846,8 +851,14 @@ protected void handleListenerSetupFailure(Throwable ex, boolean alreadyRecovered
846851
* @see #refreshDestination()
847852
*/
848853
protected void recoverAfterListenerSetupFailure() {
849-
refreshConnectionUntilSuccessful();
850-
refreshDestination();
854+
this.recovering = true;
855+
try {
856+
refreshConnectionUntilSuccessful();
857+
refreshDestination();
858+
}
859+
finally {
860+
this.recovering = false;
861+
}
851862
}
852863

853864
/**
@@ -856,9 +867,11 @@ protected void recoverAfterListenerSetupFailure() {
856867
* Connection, so either needs to operate on the shared Connection or on a
857868
* temporary Connection that just gets established for validation purposes.
858869
* <p>The default implementation retries until it successfully established a
859-
* Connection, for as long as this message listener container is active.
870+
* Connection, for as long as this message listener container is running.
860871
* Applies the specified recovery interval between retries.
861872
* @see #setRecoveryInterval
873+
* @see #start()
874+
* @see #stop()
862875
*/
863876
protected void refreshConnectionUntilSuccessful() {
864877
while (isRunning()) {
@@ -874,16 +887,19 @@ protected void refreshConnectionUntilSuccessful() {
874887
break;
875888
}
876889
catch (Exception ex) {
890+
if (ex instanceof JMSException) {
891+
invokeExceptionListener((JMSException) ex);
892+
}
877893
StringBuilder msg = new StringBuilder();
878894
msg.append("Could not refresh JMS Connection for destination '");
879895
msg.append(getDestinationDescription()).append("' - retrying in ");
880896
msg.append(this.recoveryInterval).append(" ms. Cause: ");
881897
msg.append(ex instanceof JMSException ? JmsUtils.buildExceptionMessage((JMSException) ex) : ex.getMessage());
882898
if (logger.isDebugEnabled()) {
883-
logger.warn(msg, ex);
899+
logger.error(msg, ex);
884900
}
885901
else {
886-
logger.warn(msg);
902+
logger.error(msg);
887903
}
888904
}
889905
sleepInbetweenRecoveryAttempts();
@@ -925,6 +941,17 @@ protected void sleepInbetweenRecoveryAttempts() {
925941
}
926942
}
927943

944+
/**
945+
* Return whether this listener container is currently in a recovery attempt.
946+
* <p>May be used to detect recovery phases but also the end of a recovery phase,
947+
* with {@code isRecovering()} switching to {@code false} after having been found
948+
* to return {@code true} before.
949+
* @see #recoverAfterListenerSetupFailure()
950+
*/
951+
public final boolean isRecovering() {
952+
return this.recovering;
953+
}
954+
928955

929956
//-------------------------------------------------------------------------
930957
// Inner classes used as internal adapters

0 commit comments

Comments
 (0)