出于某些原因,我不得不拦截发送和接收消息。(将消息包装起来,并在收到消息时解析它)。
我知道MessagePostProcessor是一种拦截器,但它会影响当前的代码。因此,我正在考虑使用Spring。
为了发送消息,我可以简单地拦截RabbitTemplate的send和convertAndSend方法,如下所示:
@Around("execution(* org.springframework.amqp.rabbit.core.RabbitTemplate.send(..))")但是对于接收消息,哪种方法是最好的拦截?在大多数情况下,RabbitListener用于接收消息。
任何帮助都是非常感谢的。
发布于 2020-08-26 13:36:29
将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;
};
}https://stackoverflow.com/questions/63594259
复制相似问题