@Router in Spring Integration with annotations (re

2019-07-16 05:08发布

问题:

Could you provide any example for routing messages in Spring Integration?. Filter by payload message, header or something like the following:

<int:payload-type-router input-channel="routingChannel">
<int:mapping type="java.lang.String"  channel="channel1" />
<int:mapping type="java.lang.Integer" channel="channel2" />
</int:payload-type-router>

How the response works? I mean, if I send:

channel -> router -> transformer -> gateway

Simple but I am looking something similar to this example:

<int:router input-channel="inChannel" expression="payload.paymentType">
<int:mapping value="CASH" channel="cashPaymentChannel"/>
<int:mapping value="CREDIT" channel="authorizePaymentChannel"/>
<int:mapping value="DEBIT" channel="authorizePaymentChannel"/>
</int:router>

When my gateway receives a message and sends the response to reply channel. How can I inform in my router what is the correct reply channel?

回答1:

@Router methods simply return the channel(s) or channel name(s) to which the message will be routed.

@Router(inputChannel="routingChannel")
public String route(Object payload) {
    if (payload instanceof String() {
        return "channel1";
    }
    ...
}

For your second question, you need to show more of your flow; what is "upstream" of channel?

You can omit the reply-channel from the gateway (or omit the output-channel from other types of, ultimate reply-producing consumer) and the reply will be sent to the channel that's in the message's reply-channel header; framework components that start request/reply scenarios (inbound gateways, messaging gateways, sendAndReceive methods in the MessagingTemplate) set up this header automatically so there's nothing for you to do; it just works.