I have a consumer (RabbitListner) in RPC mode and I would like to know if it is possible to throw exception that can be treated by the publisher.
To make more clear my explication the case is as follow :
- The publisher send a message in RPC mode
- The consumer receive the message, check the validity of the message and if the message can not be take in count, because of missing parameters, then I would like to throw Exception. The exception can be a specific business exception or a particular AmqpException but I want that the publisher can handle this exception if it is not go in timeout.
I try with the AmqpRejectAndDontRequeueException, but my publisher do not receive the exception, but just a response which is empty.
Is it possible to be done or may be it is not a good practice to implement like that ?
EDIT 1 :
After the @GaryRussel response here is the resolution of my question:
For the RabbitListner I create an error handler :
@Configuration public class RabbitErrorHandler implements RabbitListenerErrorHandler { @Override public Object handleError(Message message, org.springframework.messaging.Message<?> message1, ListenerExecutionFailedException e) { throw e; }}
Define the bean into a configuration file :
@Configuration public class RabbitConfig extends RabbitConfiguration {
@Bean public RabbitTemplate getRabbitTemplate() { Message.addWhiteListPatterns(RabbitConstants.CLASSES_TO_SEND_OVER_RABBITMQ); return new RabbitTemplate(this.connectionFactory());}
/** * Define the RabbitErrorHandle * @return Initialize RabbitErrorHandle bean */ @Bean public RabbitErrorHandler rabbitErrorHandler() { return new RabbitErrorHandler(); } }Create the @RabbitListner with parameters where rabbitErrorHandler is the bean that I defined previously :
@Override @RabbitListener(queues = "${rabbit.queue}" , errorHandler = "rabbitErrorHandler" , returnExceptions = "true") public ReturnObject receiveMessage(Message message) {For the RabbitTemplate I set this attribute :
rabbitTemplate.setMessageConverter(new RemoteInvocationAwareMessageConverterAdapter());
When the messsage threated by the consumer, but it sent an error, I obtain a RemoteInvocationResult which contains the original exception into e.getCause().getCause().