Spring Integration为多个生产者和消费者提供一个渠道

问题描述 投票:0回答:1

我有这个直接渠道:

@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具有单例范围)?

非常感谢

spring-integration spring-integration-dsl
1个回答
1
投票

通过直接通道消息将循环分发给消费者。

使用队列通道,只有一个消费者会收到每条消息;分发将基于各自的投票人。

通过发布/订阅频道,消费者将获得每条消息。

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

编辑

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

.handle(myServiceBean())
© www.soinside.com 2019 - 2024. All rights reserved.