Spring integration Spring集成为多个生产商和消费者提供一个渠道

Spring integration Spring集成为多个生产商和消费者提供一个渠道,spring-integration,spring-integration-dsl,Spring Integration,Spring Integration Dsl,我有一个直接频道: @Bean public DirectChannel emailingChannel() { return MessageChannels .direct( "emailingChannel") .get(); } 我是否可以为同一通道定义多个流,如下所示: @Bean public IntegrationFlow flow1FromEmailingChannel() { return IntegrationFlo

我有一个直接频道:

@Bean
public DirectChannel emailingChannel() {
    return MessageChannels
            .direct( "emailingChannel")
            .get();
}
我是否可以为同一通道定义多个流,如下所示:

@Bean
public IntegrationFlow flow1FromEmailingChannel() {
  return IntegrationFlows.from( "emailingChannel" )
            .handle( "myService" , "handler1")
            .get();
}

@Bean
public IntegrationFlow flow2FromEmailingChannel() {
  return IntegrationFlows.from( "emailingChannel" )
            .handle( "myService" , "handler2" )
            .get();
}
编辑

@Service
public class MyService {

    public void handler1(Message<String> message){
      .... 
    }

    public void handler2(Message<List<String>> message){
      .... 
    }

}
@服务
公共类MyService{
公共无效句柄1(消息消息){
.... 
}
公共无效句柄2(消息消息){
.... 
}
}
每个流的
句柄(…)
方法操作不同的
有效负载
数据类型,但目标是相同的,即从通道读取数据并调用相关处理程序。我希望避免使用许多
if…else
来检查一个处理程序中的数据类型

附加问题:当多个线程同时调用同一通道(无论其类型:
Direct
PubSub
Queue
)时会发生什么(默认情况下,
@Bean
具有单例作用域)


非常感谢

通过直接渠道,信息将循环分发给消费者

对于队列通道,只有一个消费者将获得每条消息;分配将以各自的民意调查者为基础

通过发布/订阅频道,两个消费者都将收到每条消息

您需要提供更多信息,但听起来您需要在流中添加负载类型路由器,以将消息定向到正确的消费者

编辑

@Service
public class MyService {

    public void handler1(Message<String> message){
      .... 
    }

    public void handler2(Message<List<String>> message){
      .... 
    }

}
当处理程序方法在同一个类中时,不需要两个流;框架将检查这些方法,并且(只要没有歧义)将调用与有效负载类型匹配的方法

.handle(myServiceBean())

感谢您的解释,我添加了更多关于我想做什么的信息,请参见编辑。目标是使用相同的通道发送不同的数据类型,并相应地调用相关的处理程序。我有几个生产者,每个生产者都有自己的数据类型,但消费者是相同的,应该处理不同的数据类型。如果我选择有效负载类型的路由器,这是否意味着我也必须创建额外的通道?在这种情况下(同一类中的两个方法),您不需要两个流;请看我答案的编辑。非常感谢,我怎么会错过这个!我的补充问题呢。当多个线程同时调用一个通道时,该通道的行为如何?这就是在一个线程中询问多个问题的问题:)线程将同时调用您的bean-它必须是线程安全的。直接通道(和发布/订阅)是无状态的;它的消费者在发送线程上被调用;发送线程的数量没有限制
QueueChannel
有一个内部队列,可以由多个消费者轮询器进行轮询;只有一个消费者将收到esch消息。