Class ConditionalRejectingErrorHandler
java.lang.Object
org.springframework.amqp.listener.ConditionalRejectingErrorHandler
- All Implemented Interfaces:
org.springframework.util.ErrorHandler
public class ConditionalRejectingErrorHandler
extends Object
implements org.springframework.util.ErrorHandler
ErrorHandler that conditionally wraps the Exception in an
AmqpRejectAndDontRequeueException if the configured rejection
strategy determines that the message is fatal and should not be requeued.
Such messages will be discarded or sent to a Dead Letter Exchange, depending
on broker configuration.
The default strategy will do this if the exception is a
ListenerExecutionFailedException with a cause of MessageConversionException,
MessageConversionException,
MethodArgumentNotValidException,
MethodArgumentTypeMismatchException,
NoSuchMethodException or ClassCastException.
The exception will not be wrapped if the cause chain already contains an
AmqpRejectAndDontRequeueException.
If setStopListenerOnFatal(boolean) is true, a FatalListenerExecutionException
is thrown instead of an AmqpRejectAndDontRequeueException.
The listener container must be stopped due to such a fatal state,
and the message requeued for other consumers on the destination.
- Since:
- 4.1
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreate a handler with theConditionalRejectingErrorHandler.DefaultExceptionStrategy.ConditionalRejectingErrorHandler(FatalExceptionStrategy exceptionStrategy) Create a handler with the suppliedFatalExceptionStrategyimplementation. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleancauseChainContainsARADRE(Throwable throwable) Return true if there is already anAmqpRejectAndDontRequeueExceptionpresent in the cause chain.protected FatalExceptionStrategyReturn the exception strategy.protected voidhandleDiscarded(Message failed) Called when a message with a fatal exception has anx-deathheader, prior to discarding the message.voidprotected booleanReturn the discardFatalsWithXDeath.protected booleanReturn the rejectManual.protected booleanprotected voidLog the throwable at WARN level, including stack trace.voidsetDiscardFatalsWithXDeath(boolean discardFatalsWithXDeath) Set tofalseto disable the (now) default behavior of logging and discarding messages that cause fatal exceptions and have an `x-death` header; which usually means that the message has been republished after previously being sent to a DLQ.voidsetRejectManual(boolean rejectManual) Set tofalseto NOT reject a fatal message when MANUAL ack mode is being used.voidsetStopListenerOnFatal(boolean stopListenerOnFatal) Set totrueto throw aFatalListenerExecutionExceptionon fatal error instead of aAmqpRejectAndDontRequeueException.
-
Field Details
-
logger
protected final org.apache.commons.logging.Log logger
-
-
Constructor Details
-
ConditionalRejectingErrorHandler
public ConditionalRejectingErrorHandler()Create a handler with theConditionalRejectingErrorHandler.DefaultExceptionStrategy. -
ConditionalRejectingErrorHandler
Create a handler with the suppliedFatalExceptionStrategyimplementation.- Parameters:
exceptionStrategy- The strategy implementation.
-
-
Method Details
-
isDiscardFatalsWithXDeath
protected boolean isDiscardFatalsWithXDeath()Return the discardFatalsWithXDeath.- Returns:
- the discardFatalsWithXDeath.
- See Also:
-
setDiscardFatalsWithXDeath
public void setDiscardFatalsWithXDeath(boolean discardFatalsWithXDeath) Set tofalseto disable the (now) default behavior of logging and discarding messages that cause fatal exceptions and have an `x-death` header; which usually means that the message has been republished after previously being sent to a DLQ.- Parameters:
discardFatalsWithXDeath- false to disable.
-
isRejectManual
protected boolean isRejectManual()Return the rejectManual.- Returns:
- the rejectManual.
- See Also:
-
setRejectManual
public void setRejectManual(boolean rejectManual) Set tofalseto NOT reject a fatal message when MANUAL ack mode is being used.- Parameters:
rejectManual- false to leave the message in an unack'd state.
-
getExceptionStrategy
Return the exception strategy.- Returns:
- the strategy.
-
setStopListenerOnFatal
public void setStopListenerOnFatal(boolean stopListenerOnFatal) Set totrueto throw aFatalListenerExecutionExceptionon fatal error instead of aAmqpRejectAndDontRequeueException.- Parameters:
stopListenerOnFatal- true to throw aFatalListenerExecutionException- Since:
- 4.1
-
isStopListenerOnFatal
protected boolean isStopListenerOnFatal() -
handleError
- Specified by:
handleErrorin interfaceorg.springframework.util.ErrorHandler
-
handleDiscarded
Called when a message with a fatal exception has anx-deathheader, prior to discarding the message. Subclasses can override this method to perform some action, such as sending the message to a parking queue.- Parameters:
failed- the failed message.
-
log
-
causeChainContainsARADRE
Return true if there is already anAmqpRejectAndDontRequeueExceptionpresent in the cause chain.- Parameters:
throwable- aThrowable.- Returns:
- true if the cause chain already contains an
AmqpRejectAndDontRequeueException.
-