0

I have this direct channel:

@Bean
public DirectChannel emailingChannel() {
    return MessageChannels
            .direct( "emailingChannel")
            .get();
}

Can I define multiple flows for the same channel like this:

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

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

EDIT

@Service
public class MyService {

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

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

}

Each flow's handle(...) method manipulates different payload data types but the goal is the same, i-e reading data from the channel and call the relevant handler. I would like to avoid many if...else to check the data type in one handler.

Additional question: What happens when multiple threads call the same channel (no matter its type: Direct, PubSub or Queue) at the same time (as per default a @Bean has singleton scope)?

Thanks a lot

akuma8
  • 4,160
  • 5
  • 46
  • 82

1 Answers1

1

With a direct channel messages will be round-robin distributed to the consumers.

With a queue channel only one consumer will get each message; the distribution will be based on their respective pollers.

With a pub/sub channel both consumers will get each message.

You need to provide more information but it sounds like you need to add a payload type router to your flow to direct the messages to the right consumer.

EDIT

When the handler methods are in the same class you don't need two flows; the framework will examine the methods and, as long as there is no ambiguity) will call the method that matches the payload type.

.handle(myServiceBean())
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Thanks for this explanation, I added more information about what I would like to do, see the EDIT. The goal is to use the same channel to send different data types and call the relevant handler accordingly. I have several producers, each one has its own data type but the consumer is the same and should handle different datatypes. If I go for a payload type router, does that mean I have to create extra channels too? – akuma8 Feb 27 '19 at 16:05
  • In that case (both methods in the same class), you don't need two flows; see the edit to my answer. – Gary Russell Feb 27 '19 at 16:27
  • Thanks a lot, how could I have missed this! What about my additional question. How a channel behaves when multiple threads invoke it at the same time? – akuma8 Feb 27 '19 at 16:33
  • That's the problem with asking multiple question in one :) The threads will concurrently invoke your bean - it must be thread-safe. – Gary Russell Feb 27 '19 at 17:13
  • Sorry ^^. My question was more about the message publishing for each thread. Will the channel be able to publish each thread's message? – akuma8 Feb 28 '19 at 21:46
  • 1
    Direct channel (and pub/sub) is stateless; its consumers are called on the sending thread; there is no limit to the number of sending threads. `QueueChannel` has an internal queue and can be polled by multiple consumer pollers; only one consumer will get esch message. – Gary Russell Feb 28 '19 at 21:54