【问题标题】:how to add try catch exception for spring integration flow to achieve nested transactions如何为spring集成流添加try catch异常以实现嵌套事务
【发布时间】:2019-06-11 11:03:28
【问题描述】:

如何在 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
   }
}

【问题讨论】:

    标签: spring-integration spring-integration-dsl


    【解决方案1】:

    为此,我们提供了一个 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,包括TransactionInterceptorhttps://docs.spring.io/spring-integration/docs/current/reference/html/#tx-handle-message-advice

    拥有 try...catch 语义的最佳方法是使用ExpressionEvaluatingRequestHandlerAdvice。请参阅 Docs 及其 JavaDocs 中的描述。

    【讨论】:

    • 谢谢我会尝试提到的选项
    猜你喜欢
    • 2021-04-25
    • 1970-01-01
    • 2013-05-26
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 2012-02-01
    相关资源
    最近更新 更多