java - Spring Integration Java DSL - 动态设置 RecepientListRouter 收件人?

标签 java asynchronous spring-integration router dsl

我有一个需要异步执行多个任务的方法。我已经设法使用以下 IntegrationFlow 来实现这一目标:

@Bean
public IntegrationFlow startJobTask() {
    return IntegrationFlows.from("TaskRoutingChannel")
            .handle("jobService", "executeTasks")
            .routeToRecipients(r -> r
                .recipient("testTaskChannel")
                .recipient("test2TaskChannel"))
            .get();
}

@Bean ExecutorChannel testTaskChannel(){
    return new ExecutorChannel(this.getAsyncExecutor());
}

@Bean ExecutorChannel test2TaskChannel(){
    return new ExecutorChannel(this.getAsyncExecutor());
}

@Bean
public Executor getAsyncExecutor() {
    SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor();
    return executor;
}

@Bean
public IntegrationFlow testTaskFlow() {
    return IntegrationFlows.from("testTaskChannel")
            .handle("testTaskService", "executeAsync")
            .get();
}

@Bean
public IntegrationFlow test2TaskFlow() {
    return IntegrationFlows.from("test2TaskChannel")
            .handle("test2TaskService", "executeAsync")
            .get();
}

上面的流程基本如下:

TaskRoutingChannel调用 serviceActivator 方法 executeTasks

executeTasks返回类似 Message<List<myTask>> 的内容

这个Message被路由到 channel testTaskChanneltest2TaskChannel 它调用自己的异步 serviceActivator 方法。

现在的问题是我不想对收件人 channel 进行硬编码。 我可以通过将目标 channel 设置为 header 来避免使用普通路由器进行硬编码。但是,recepientListRouters 似乎无法使用表达式获取收件人 channel 名称。

有没有办法动态设置接收 channel ?

最佳答案

首先对延迟表示歉意。

实际上常规的 .route() 可以为你做到这一点,因为它确实有这个选项:

protected abstract Collection<MessageChannel> determineTargetChannels(Message<?> message);

因此,如果您的 header 提取返回 channel 列表,则 HeaderValueRouter 将能够将消息发送给所有 channel 。

关于java - Spring Integration Java DSL - 动态设置 RecepientListRouter 收件人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31959480/

相关文章:

java - 如何从一个对象创建/引用另一个对象?

asp.net-mvc-3 - 您如何使用 ASP.net MVC 的 AsyncController 处理异常?

java - SpyBean 没有被注入(inject)到任何地方

java - 如何配置 Spring 从套接字字节流中获取数据?

java - spring-integration-aws java配置

java - 使用 jframe 时找不到符号错误

java - 出现错误 - 线程 "AWT-EventQueue-0"java.lang.IllegalArgumentException 异常 : text cannot be null or empty

java - 检查字符串是否由某些字符组成的方法

reactjs - 如何在异步函数中使用 setState

javascript - 为什么 Sonarqube 告诉我 await 在异步函数中是多余的