微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用SpringAOP拦截发送和接收消息

如何解决使用SpringAOP拦截发送和接收消息

由于某些原因,我不得不拦截发送和接收消息。 (包装邮件,并在收到邮件时对其进行解析)。

我知道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;
    };
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。