Reputation: 507
I am trying to implement the recovery in spring amqp. I have used below code to implement the same
RetryOperationsInterceptor retryInterceptorBuilder =RetryInterceptorBuilder.stateless()
.maxAttempts(5)
.recoverer(new CustomRejectAndDontRequeueRecoverer())
.build();
container.setAdviceChain(new RetryOperationsInterceptor[]{retryInterceptorBuilder});
The above container is the instance of SimpleMessageListenerContainer
. Now in one of my receivers I am throwing ClassCastException
.
public class CustomRejectAndDontRequeueRecoverer implements MessageRecoverer {
private static Logger logger = //created some logger instance.
//Overriding the method to do custom task when the retries are exhausted, like insert in database.
@Override
public void recover(Message message, Throwable cause) {
logger.error("The recovery method is called","","");
throw new RuntimeException(cause);
}
}
Please point me in the right direction.
Update :
Some exception is thrown in my CustomMessagingPostProcessor. My RetryOperationsInterceptor only retries the messages if the exception is thrown in onMessage() method. Adding definition of CustomMessagingPostProcessor:-
public class MTMessagingPostProcessor implements MessagePostProcessor{
/**
* {@inheritDoc}
*/
@Override
public Message postProcessMessage(Message message) {
logger.xdebug("Inside MTMessagingPostProcessor",
//Throwing exception to show that some exception can be thrown in original code and I want to requeue messages to come here for n number of times.
throw new RuntimeException("TEST");
//return message;
}
public void setTenantProvider(TenantProvider tenantProvider) {
this.tenantProvider = tenantProvider;
}
}
I want to requeue messages n number of times if exception is thrown in MTMessagingPostProcessor, this is not achieved by interceptor as it retries messages in onMessage() method of the listener.
Upvotes: 2
Views: 897
Reputation: 174554
If you mean you are providing a MessagePostProcessor
in the afterReceiveMessagePostProcessors
container property, those post processors are called outside the scope of the advice chain (retry interceptors).
You can't "retry" exceptions thrown by such post processors.
The retry interceptor applies only to the listener itself.
Upvotes: 2