我有一个需要异步执行多个任务的方法。我已经设法使用以下 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 testTaskChannel
和test2TaskChannel
它调用自己的异步 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/