In this article, i am not going to explain the basics of Spring Cloud Stream OR the process of creating publishers and subscribers. Those have been clearly described in the Part 1 and Part 2 of this article series.
It is possible to send messages with headers. In the receiving end (consumer application), there can be multiple message handlers (@StreamListener annotated methods) those accepts messages based on the headers of the message.
A copy of the message will be sent to every handler method and they will accept the message if it matches the given condition . The condition is a SpEL expression (Spring Expression Language) that performs checks on header values. The sample condition is given as follows.
@StreamListener(target = OrderSink.INPUT,condition = "headers['payment_mode']=='credit'")
(Please refer the source code the complete code)
In that way, you can use the headers to route messages (message routing) among multiple message handlers. Here we will look at, how to deliver the messages to the correct recipient based on the header.
Setting up the publisher application
The development of publisher application is almost similar to previous article (part 2) except the OrderController used in message publishing. The messages will be published with header(s) and those headers will be used in the consumer application for directing the message to the correct handler.
Please refer the below OrderController class.
As you can see that, the message will be published with a header known as “payment_mode” . The user will send the payment_mode as a request parameter and the controller method will set it as a message header when building the message.
Setting up the consumer application
The development of consumer application is also similar to the previous article except the OrderListener used in message receiving.
OrderListener will have set of message handlers for receiving messages based on SpEL condition (or rather header values)
Please refer the below OrderListener class.
As you can see that, there are three handler methods with different acceptance conditions based on header called “payment_mode“.
Testing and Running
Lets publish few messages as follows. Then we will check wether they are directed to the correct handler based on the message header.
Lets publish a message with by setting the request param “payment_mode” as cash
Lets publish a message with by setting the request param “payment_mode” as credit
Lets publish a message with by setting the request param “payment_mode” as cheque
Now, the consumer application should receive Cash, Credit and Cheque messages in respective handlers (as the published order). Please refer the below log segment of the consumer.
The Source Code
If you have any query or clarification, please feel free to drop a message.