I have an application which uses spring AMQP for consuming and producing messages to other application. I have a scenario where some of the exception occurred i need to re-queue back to RabbitMQ. For some exceptions i need to ignore (Basically i need to ignore the message no need to requeue)
Currently in below code, i have set the configuration as
factory.setDefaultRequeueRejected(false);
But my requirement is to dynamically reject for some messages and re-queue back to RabbitMQ for some messages.
Please suggest
@Bean(name="rabbitListenerContainerFactory")
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter();
DefaultClassMapper classMapper = new DefaultClassMapper();
Map<String, Class<?>> idClassMapping = new HashMap<String, Class<?>>();
idClassMapping.put(Constants.JOB_TYPE_ID_, JobListenerDTO.class);
classMapper.setIdClassMapping(idClassMapping);
messageConverter.setClassMapper(classMapper);
factory.setMessageConverter(messageConverter);
factory.setDefaultRequeueRejected(false);
factory.setReceiveTimeout(10L);
return factory;
}
You can't do it that way around (default false).
To do it selectively you have to set
defaultRequeueRejected
totrue
and throwAmqpRejectAndDontRequeueRejected
for any you want discarded.You can encapsulate your desired logic in an
ErrorHandler
.The default error handler does exactly that for a certain list of exceptions, as documented here - you can inject a custom
FatalExceptionStrategy
.But for conditional rejection, the
defaultRequeueRejected
must betrue
.EDIT
This adds
MyBadXMLException
to the standard list.If you are not using Java 8, use
new FatalExceptionStrategy() {...}
.