Spring Integration一个面向多个生产者和消费者的渠道

时间:2019-02-27 09:32:06

标签: spring-integration spring-integration-dsl

我有这个直接渠道:

@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){
      .... 
    }

}

每个流的handle(...)方法处理不同的payload数据类型,但目标是相同的,即从通道读取数据并调用相关的处理程序。我想避免许多if...else在一个处理程序中检查数据类型。

其他问题:当多个线程同时调用同一通道(无论其类型为DirectPubSubQueue)时会发生什么(默认情况下,@Bean具有单例范围)?

非常感谢

1 个答案:

答案 0 :(得分:0)

具有直接渠道的消息将循环分发给消费者。

在队列通道中,只有一个消费者将收到每条消息;分布将基于各自的民意测验。

通过发布/订阅渠道,两个使用者都将收到每条消息。

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

编辑

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

.handle(myServiceBean())
相关问题