2
votes

How can I have 2 channels output to a single channel with spring integration without using XML. Similar to the following question Multiple channel's message comes into single channel

I have 2 PollableChannel beans in my context that I wish to route messages from both (non aggregated) to a single @ServiceActivator, i.e. accomplishing something like the following:

@Bean("Channel1") PollableChannel c1() {...}
@Bean("Channel2") PollableChannel c2() {...}

?? How to interleave/combine Channel1 and Channel2 into a single channel

...
@ServiceActivator(inputChannel = "Channel1and2")
void handle(msg: MyMessage) {...}
1

1 Answers

3
votes
@Bean("Channel1") 
@BridgeTo("Channel1and2")
PollableChannel c1() {...}

@Bean("Channel2") 
@BridgeTo("Channel1and2")
PollableChannel c2() {...}

Pay attention to te @BridgeTo annotation. From its JavaDocs:

* Messaging Annotation to mark a {@link org.springframework.context.annotation.Bean}
* method for a {@link org.springframework.messaging.MessageChannel} to produce a
* {@link org.springframework.integration.handler.BridgeHandler} and Consumer Endpoint.
* <p>
* The {@link org.springframework.messaging.MessageChannel} {@link org.springframework.context.annotation.Bean}
* marked with this annotation is used as the {@code inputChannel} for the
* {@link org.springframework.integration.endpoint.AbstractEndpoint}
* and determines the type of endpoint -
* {@link org.springframework.integration.endpoint.EventDrivenConsumer} or
* {@link org.springframework.integration.endpoint.PollingConsumer}.

You can also consider to use the @Poller on that @BridgeTo, since your input channels are PollableChannel.

The Reference Manual on the matter: http://docs.spring.io/spring-integration/docs/latest-ga/reference/html/configuration.html#_creating_a_bridge_with_annotations