spring-amqp - 使用SpringAOP拦截发送和接收消息
问题描述
由于某些原因,我必须拦截发送和接收消息。(包装消息并在收到消息时解析消息)。
我知道MessagePostProcessor
是拦截器的一种形式,但它会影响当前代码。所以,我正在考虑使用 Spring AOP。
对于发送消息,我可以简单的拦截 RabbitTemplate 的send
和convertAndSend
方法,如下代码:</p>
@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;
};
}
推荐阅读
- react-native - Expo React-Native Youtube 视频上传使用 Fetch()
- java - @JsonIgnoreProperties 不适用于招摇的一代
- total.js - 更改了 total4 模式中的更新值
- windows - Windows 中错误表示的小数字
- sonarqube - 放置什么 yaml 构建命令来为 github 存储库运行声纳云?
- java - 按 id 或按类查找 WebElement?
- typescript - 如何在初始化 Angular-slickgrid 树时直接显示来自 Db 的数据,而不是在单击行标题之后
- kubernetes - Kubernetes 在副本之间共享存储
- python - T-SNE Python 应用到实验数据
- database - 小部件不显示真实数据源的数据库值