使用SpringAOP拦截收发消息

Use SpringAOP to intercept send and receive messages

由于某些原因,我必须拦截发送和接收消息。 (包装消息并在收到消息时解析消息)。

我知道MessagePostProcessor是拦截器的一种形式,但它会影响当前代码。所以,我正在考虑使用 Spring AOP。

对于发送消息,我可以简单地拦截RabbitTemplate的sendconvertAndSend方法,像下面的代码:

@Around("execution(* org.springframework.amqp.rabbit.core.RabbitTemplate.send(..))")

但是对于接收消息,哪种方法最好拦截?大多数情况下,RabbitListener用于接收消息。

感谢任何帮助。

Advice 添加到侦听器容器的 adviceChain。参见 https://docs.spring.io/spring-amqp/docs/2.2.10.RELEASE/reference/html/#containerAttributes

编辑

@Bean
public MethodInterceptor advice() {
    return invocation -> {
        Message message = (Message) invocation.getArguments()[0];
        try {
            // before
            invocation.proceed();
            // after
        }
        catch (Exception e) {
            // ...
            throw e;
        }
        finally {
            // ...
        }
        return null;
    };
}