[英]Spring Integration one channel for multiple producers and consumers
我有這個直接渠道:
@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
在一個處理程序中檢查數據類型。
附加問題:當多個線程同時調用同一通道(無論其類型是Direct
, PubSub
還是Queue
)時(默認情況下, @Bean
具有單例作用域),會發生什么?
非常感謝
具有直接信道的消息將循環分發給消費者。
在隊列通道中,只有一個使用者將收到每條消息。 分配將基於各自的投票者。
通過發布/訂閱頻道,兩個使用者都將收到每條消息。
您需要提供更多信息,但是聽起來您需要在流中添加有效負載類型路由器,以將消息定向到正確的使用者。
編輯
當處理程序方法在同一個類中時,您不需要兩個流程。 框架將檢查這些方法,只要沒有歧義,就會調用與有效負載類型匹配的方法。
.handle(myServiceBean())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.