Spring 使用注释代码示例的集成执行器通道

Spring Integration Executor Channel using annotations code sample

附上我的系统图

System Diagram

流程的工作原理:

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

  1. 存入数据库

  2. Notify/print 给用户

重要标准:

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

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

我浏览了一下,发现我必须使用执行程序通道将 'store into 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();
    }       

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

NOTE: I need annotaions only example.

你真的不需要 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))