akuma8
akuma8

Reputation: 4691

Spring Integration one channel for multiple producers and consumers

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

Upvotes: 0

Views: 1621

Answers (1)

Gary Russell
Gary Russell

Reputation: 174554

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())

Upvotes: 1

Related Questions