如何为 spring 集成流程添加 try catch 异常以实现嵌套事务

how to add try catch exception for spring integration flow to achieve nested transactions

如何处理 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 以注入 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, including TransactionInterceptor: https://docs.spring.io/spring-integration/docs/current/reference/html/#tx-handle-message-advice

获得 try...catch 语义的最佳方法是使用 ExpressionEvaluatingRequestHandlerAdvice。请参阅文档及其 JavaDocs 中的描述。