Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Integration Splitter Map Keys to different channels

I have a transformer which returns a Map as a result. This result is then put on to the output-channel. What I want to do is to go to different channel for each KEY in the map. How can I configure this in Spring Integration?

e.g.

Transformer -- produces --> Map

Map contains {(Key1, "some data"), (Key2, "some data")}

So for Key1 --> go to channel 1 So for Key2 --> go to channel 2 etc..

Code examples would be helpful.

Thanks in advance GM

like image 413
user2279337 Avatar asked Oct 22 '22 06:10

user2279337


1 Answers

Your processing should consist of two steps:

  1. Partitioning message into separate parts that will be processed independently,
  2. Routing separate messages (the result of split) into appropriate channels.

For the first task you have to use splitter and for the second one - router (header value router fits best here).

Please find a sample Spring Integration configuration below. You may want to use an aggregator at the end of a chain in order to combine messages - I leave it at your discretion.

<channel id="inputChannel">

<!-- splitting message into separate parts -->
<splitter id="messageSplitter" input-channel="inputChannel" method="split"
                output-channel="routingChannel">
  <beans:bean class="com.stackoverflow.MapSplitter"/>
</spliter>

<channel id="routingChannel">

<!-- routing messages into appropriate channels basis on header value -->

<header-value-router input-channel="routingChannel" header-name="routingHeader">
  <mapping value="someHeaderValue1" channel="someChannel1" />
  <mapping value="someHeaderValue2" channel="someChannel2" />    
</header-value-router>

<channel id="someChannel1" />
<channel id="someChannel2" />

And the splitter:

public final class MapSplitter {

  public static final String ROUTING_HEADER_NAME = "routingHeader";

  public List<Message<SomeData>> split(final Message<Map<Key, SomeData>> map) {
    List<Message<SomeData>> result = new LinkedList<>();

    for(Entry<Key, SomeData> entry : map.entrySet()) {
      final Message<SomeData> message = new MessageBuilder()
          .withPayload(entry.getValue())
          .setHeader(ROUTING_HEADER_NAME, entry.getKey())
          .build(); 
      result.add(message); 
    }

    return result;
  } 
}
like image 159
omnomnom Avatar answered Nov 04 '22 00:11

omnomnom