Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
We can use implementations to write our own custom logic for
routing messages. To do so, we need to define a class that expects a
message, parses the message (or header), and accordingly returns the
channel name. The BigTradeRouter
shown below implements a routing logic to forward any big Trades (whose
quantity is greater than one million pounds) to a big-trades-channel.
public class BigTradeRouter {
public String bigTrade(Message<Trade> message){
Trade t = message.getPayload();
// check if the trade is a big one and if it is
// send it to a separate channel to handle them
if(t.getQuantity() > 1000000)
return "big-trades-channel";
// else send a normal channel
return "normal-trades-channel";
}
}