1

I'm using spring router to decide which of 2 channels to go through. I wan't to pass to the service that gives back an answer one of the values in the payload and not the payload itself. How can I do that? I didn't really find a use case like this, I thought maybe it could be done by using enricher. Any suggestions?

My code :

<int:router id="aggregationRouter"
            input-channel="aggregationPathRouterInputChannel"
            ref="decideAggregationByName"
        >
    <int:mapping value="true" channel="SumAggregatorChannel" />
    <int:mapping value="false" channel="AvgAggregatorChannel"/>
</int:router>

the decideAggregationByName gets an input which is a string (name) and returns a boolean. I don't know how to pass the string argument that is a member inside the payload I get in this step

3 Answers 3

1

Most Spring Integration components provide an expression option in contradiction to the ref and method. So, if your payload is a Map (for example), but your decideAggregationByName requires String as an argument and you know which key represents a value for that arg you can do that like this:

<int:router id="aggregationRouter"
        input-channel="aggregationPathRouterInputChannel"
        expression="@decideAggregationByName.doDecision(payload['key'])"
    >

From other side you can rework that target method to accept whole payload object and extract desired String from the code manually.

Otherwise I'm not sure what is the issue is in front of you.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this way. We can use expression to decide channel:

<int:router input-channel="aggregationPathRouterInputChannel"
   expression="payload ? 'SumAggregatorChannel' : 'AvgAggregatorChannel'" />

Comments

0

If i am not wrong, what you want to achieve is, select respective channel based on some key in the payload and then pass only that part of the payload to the service not the complete payload. The solution by bilan will still pass the complete payload and then at your service end you stil have to take out that value (which does not sound a good pattern, since that is not your serive methods job.) what i would suggest is: Step1: Write a splitter, where you will create MessageObjects list for different values in the payload.

public List<Message<?>> spitPayload(your payload) {

List<Message<?>> result = new ArrayList<Message<?>>();

 final Message<?> message1 = MessageBuilder
                    .withPayload("values from payload for sum")
                    .setHeader('routerId',
                            "sum").build();
final Message<?> message2 = MessageBuilder
                    .withPayload("values from payload for avg")
                    .setHeader('routerId',
                            "avg").build();

result.add(message1);result.add(message2);
return message;

}

Step2: Router to judge the respective channel based on the paylod key.

<si:header-value-router header-name="routerid" 
        input-channel="aggregationPathRouterInputChannel">
    <si:mapping value="sum" channel="sumchannel" />
    <si:mapping value="avg" channel="avgchannel" />
</si:header-value-router>

Once the key is matched,, only the required part of the paylod will be transfered to the channel endpoint.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.