spring-integration - 如何为spring集成流添加try catch异常以实现嵌套事务
问题描述
如何在 Spring 集成流程中处理嵌套事务。基本上我有一个从数据库中获取所有订单并按订单处理的进程,如果在单个订单上抛出异常,所有处理的订单都会回滚。
IntegrationFlows.from("perOrder")
.filter(Order.class, order -> order.getItems().size() > 0)
.handle(orderHandler, "handle") /*someway i way want to add try/catch for this method here so that
if handle method throws exception, want to suppress for that order and mark as failure only for that order */
.get();
public class OrderHandler {
@Transactional(propagation = Propagation.NESTED)
public handle() {
processing code
throw exception in case of any validation failure
}
}
解决方案
为此,我们提供了一个adviceChain来注入到 that 的端点handle()
:
.handle((GenericHandler<?>) (p, h) -> {
throw new RuntimeException("intentional");
}, e -> e.advice(retryAdvice()))
您可以在那里注入任何可用的Advice
实现:https ://docs.spring.io/spring-integration/docs/current/reference/html/#message-handler-advice-chain ,包括TransactionInterceptor
:https ://docs.spring.io /spring-integration/docs/current/reference/html/#tx-handle-message-advice
拥有try...catch语义的最好方法是使用ExpressionEvaluatingRequestHandlerAdvice
. 请参阅 Docs 及其 JavaDocs 中的描述。
推荐阅读
- prometheus - 从 Alertmanager 获取警报
- python - PyInstaller 中缺少 FFMPEG 的库文件
- rendering - SDL2中的Y轴深度排序
- node.js - Discord.Js - 获取所有用户已发送消息的列表?
- c# - MVC 复杂模型绑定到列表
- r - 错误:$ 运算符对原子向量无效 - ggtern
- typescript - 替换数组中对象的子字符串?
- excel - 在远程 MSVS 调试器下启动 Microsoft Excel 2016 时崩溃
- python - sklearn 管道在应用于测试集时不记得其状态
- node.js - 从 Google 云函数创建 Google 函数