Search code examples
springspring-bootrabbitmqspring-amqp

How to use interceptor with Spring AMQP


Is there a way to intercept messages once template.convertAndSend is called, before the message is delivered to RabbitMQ.

Also any way to intercept the message before reaching the handler?

I can handle the message using PostProcessor for publisher, but prefer to use interceptor.

public class TestPostProcessor implements MessagePostProcessor {

    @Autowired
    Tracer defaultTracer;

    @Override
    public Message postProcessMessage(Message message) throws AmqpException {
        //.....
        //.... 
        return message;
    }
}

Any suggestions?


Solution

  • MessagePostProcessor is a form of interceptor.

    There are two ways to invoke one - use one of the overloaded convertAndSend() methods that take an MPP as an argument, or add one or more to the RabbitTemplate using setBeforePublishPostProcessors().

    You can also intercept received messages using the setAfterReceivePostProcessors() which is invoked before the received message is returned from a receive() method.

    The listener container also supports MPPs after receiving and before delivery to the listener via its setAfterReceivePostProcessors() method.