【问题标题】:Spring Integration Executor Channel using annotations code sampleSpring Integration Executor Channel 使用注解代码示例
【发布时间】:2018-11-05 05:22:56
【问题描述】:

附上我的系统图。

System Diagram

流程是如何工作的:

spring 集成流程从 C:\ 上的 json 文件读取输入并执行 2 个操作:

  1. 存储到数据库中

  2. 通知/打印给用户

重要标准:

我希望存储到 DB 流中独立于业务逻辑(打印到/通知用户),即 DB exception/DB 成功不应影响通知用户。

同样通知用户不应该影响数据库流。

我浏览并发现我必须使用执行器通道将“存储到数据库”委托给另一个线程。

我找不到执行通道的代码示例。 我只需要基于注释的代码,因为所有其他类都是基于注释的

我需要什么:ExecutorChannel 的代码示例 - 基于注释。

2 内的代码(从文件中读取 json 并将其发送到数据库,自定义业务逻辑)

@Bean
public IntegrationFlow readFromJSONFile() {
    return IntegrationFlows
            .from("/path/to/File")
            .transform("Transformer to convert to Bean")
            .wireTap(
                    flow -> flow.handle(msg -> logger
                            .info("Message sent to common channel: " + msg.getPayload())))
                            .channel("Common Channel Name")
                            .get();
    }

4 内的代码:

@Bean
    public IntegrationFlow sendToDb() {

    return IntegrationFlows
            .from("Common Channel Name")
            .handle("DAO Impl to store into DB") // I THINK THE MESSAGE SHOULD BE SENT TO AN EXECUTOR CHANNEL TO PROCESS ON A SEPARATE THREAD
            .get();
    }

5 内的代码:

@Bean
public IntegrationFlow sendToBusinessLogictoNotifyUser() {

    return IntegrationFlows
            .from("Common Channel Name")
            .handle("Business Logic Class name")
                            .get();
    }       

当前行为:如果有数据库异常,通知用户也会失败。相反,我希望它安静地记录下来。

注意:我只需要注释示例。

【问题讨论】:

    标签: spring spring-integration executor


    【解决方案1】:

    您实际上并不需要 ExecutorChannel - 只需在 pub/sub 频道上将 ignoreFailures 设置为 true...

    /**
     * Specify whether failures for one or more of the handlers should be
     * ignored. By default this is <code>false</code> meaning that an Exception
     * will be thrown whenever a handler fails. To override this and suppress
     * Exceptions, set the value to <code>true</code>.
     * @param ignoreFailures true if failures should be ignored.
     */
    public void setIgnoreFailures(boolean ignoreFailures) {
    

    如果您想在数据库存储中记录或以其他方式处理异常,可以将ExpressionEvaluatingRequesthandlerAdvice 添加到执行数据库存储的组件中。

    如果你真的想要ExecutorChannelDSL section of the reference manual has an example

    .channel(MessageChannels.executor("executorChannel", this.taskExecutor))
    

    【讨论】:

    • 谢谢加里。我会试试看。不过,根据github.com/spring-projects/spring-integration/blob/master/src/…,快速提问,abject 是使用 ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice(); 创建的。我可以使用@Autowired 代替吗,因为我不想在类中创建对象。
    • 我不明白建议是 bean 的问题:@Bean public Advice expressionAdvice() 所以,它当然可以自动装配。
    猜你喜欢
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多