I am new in Spring integration and working on a SI project. I am doing a simple job of getting message from a channel (fromAdapter), calling a transformer and sending the output to another channel (toQueue). The below code is used in the SI configuration file —-
<int:channel id="fromAdapter"></int:channel>
<int:channel id="toQueue">
</int:channel>
<bean id="trans" class="src.MyTransformer"></bean>
<int:transformer input-channel="fromAdapter" output-channel="toQueue" ref="trans"></int:transformer>
However, now I have a slightly complex requirement. Instead of always sending the message to one transformer,based on some value of the message, I want to send the message to any one of 6 transformers. How can this be implemented?
You can declare those 6 transformers as subscribers to a single point-to-point channel and by default it will use a round robin dispatching strategy (it will only invoke a single transformer for each message, but it will always pick the next transformer in the list and then cycle).
In your case, you should simply declare all those transformers to use the exact same input and output channels and the above will automagically happen.
To pick the transformer based on some attribute of your message, you can use a
recipient-list-routerand define aselector-expressionfor eachrecipientin the list in order to match a particular kind of message. Also, for each recipient you should use a different channel name. Then each of those channels will be used as input by the desired transformer:Keep in mind that with this approach, a message could match more than one selector expression so it’s up to you to provide mutually exclusive expressions.
Or, if you are willing to write some infrastructure code, you can write your own implementation of
LoadBalancingStrategyand provide that to your point-to-point channel. Your strategy will then be responsible for picking the right handler for each message.