Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 420
basu76 Avatar asked Oct 21 '16 19:10

basu76


People also ask

What is SpringFramework AMQP?

Group: SpringFramework AMQP It provides high-level abstractions for sending and receiving messages. Last Release on Sep 19, 2022.

What is RabbitTemplate?

Helper class that simplifies synchronous RabbitMQ access (sending and receiving messages). The default settings are for non-transactional messaging, which reduces the amount of data exchanged with the broker. To use a new transaction for every send or receive set the channelTransacted flag.

What is AMQP listener?

AMQP stands for Advances Messaging Queing Protocol. The AMQP listener can be used to read from an AMQP destination. This destination can be a queue or topic in an AMQP environment. The connection factory determines which AMQP environment will be used.

What is @EnableRabbit?

@EnableRabbit enables detection of RabbitListener annotations on any Spring-managed bean in the container.


1 Answers

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.

like image 174
Gary Russell Avatar answered Sep 28 '22 02:09

Gary Russell