使用注释的 Spring Integration Executor Channel 代码示例

2023-12-02

附上我的系统图。

系统图

流程如何运作:

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

  1. 存入数据库

  2. 通知/打印给用户

重要标准:

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

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

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

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

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

2里面的代码(从文件中读取json并发送给DB,自定义业务逻辑)

@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:我只需要注释示例。


你真的不需要ExecutorChannel- 只需设置ignoreFailures to 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到执行数据库存储的组件。

如果你真的想要一个ExecutorChannel, the 参考手册的 DSL 部分有一个示例.

.channel(MessageChannels.executor("executorChannel", this.taskExecutor))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用注释的 Spring Integration Executor Channel 代码示例 的相关文章

随机推荐