Spring Integration Java DSL - Set RecepientListRouter recipients dynamically?

青春壹個敷衍的年華 提交于 2019-12-11 09:15:09

问题


I have a method which needs to execute multiple tasks async'ly.. I've managed to achieve that using the following 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();
}

The above flow is basically as follows:

TaskRoutingChannel calls a serviceActivator method executeTasks

executeTasks returns something like Message<List<myTask>>

this Message is routed to channels testTaskChannel and test2TaskChannel which calls their own async serviceActivator methods.

Now, the issue is that I don't want to hardcode the recipient channels. I could avoid hardcoding with normal router by setting the destination channel as a header. However, recepientListRouters don't seem to have the capability to get recipient channel name using expressions.

Is there any way to set the recipient channels dynamically?


回答1:


Sorry for delay first of all.

Actually the regular .route() can do that for you, because it has exactly has this option:

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

So, if your header extraction returns the list of channels, the HeaderValueRouter will be able to the message to all of them.



来源:https://stackoverflow.com/questions/31959480/spring-integration-java-dsl-set-recepientlistrouter-recipients-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!