Negative acknowledgment for RabbitMQ queue for re-message queue using Spring AMQP

I have an application that uses spring AMQP to consume and create messages in another application. I have a scenario in which some exception occurred, I need to queue again on RabbitMQ. With some exceptions, I need to ignore (basically I need to ignore the message, no need to require)

Currently in the lower code, I set the configuration as

factory.setDefaultRequeueRejected (false);

But my requirement is to dynamically reject some messages and return a queue 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; } 
0
source share
1 answer

You cannot do it like this (false by default).

To do this selectively, you need to set defaultRequeueRejected to true and throw AmqpRejectAndDontRequeueRejected for any that you want to drop.

You can encapsulate the desired logic in the ErrorHandler .

The default error handler does just that for a specific list of exceptions, as it is documented here - you can introduce a custom FatalExceptionStrategy .

But for conditional rejection, defaultRequeueRejected must be true .

EDIT

 factory.setErrorHandler(new ConditionalRejectingErrorHandler(t -> { Throwable cause = t.getCause(); return cause instanceof MessageConversionException || cause instanceof org.springframework.messaging.converter.MessageConversionException || cause instanceof MethodArgumentNotValidException || cause instanceof MethodArgumentTypeMismatchException || cause instanceof NoSuchMethodException || cause instanceof ClassCastException || cause instanceof MyBadXMLException; })); 

This adds MyBadXMLException to the standard list.

If you are not using Java 8, use new FatalExceptionStrategy() {...} .

+3
source

All Articles