Overview
Java Messaging Service is often used for asynchronous communication between client and server. Mule ESB allows JMS for synchronous communication wherein the client and server can be be completely decoupled. The JMS synchronous communication is also referred to as “JMS Back-channel”. The JMS back-channel is usually not provided by the JMS protocol or any JMS brokers and has to be implemented by the applications. In this blog, a simple use case of the synchronous JMS communication using Mule ESB and Active MQ is described.
Implementation Details
In the below example, a string message is sent and received in order to demonstrate JMS back-channel. The Active MQ is used as a message broker for the demo. The JMS back-channel can be implemented by Mule ESB in two ways which are described in the following sections.
<jms:activemq-connector name="Active_MQ"
username="username" password="password"
brokerURL="tcp://localhost:63636"
validateConnections="true" doc:name="Active MQ" />
Approach 1: using request-response JMS endpoints
<flow name="jmsmessagepublisher" doc:name="jmsmessagepublisher">
<http:inbound-endpoint exchange-pattern="request-response"
host="localhost" port="1111" path="backchannel" doc:name="HTTP" />
<set-payload value="Hello World from Sender" doc:name="Set Payload" />
<response>
<logger message="Final Response: #[payload]" level="INFO" doc:name="Logger" />
</response>
<logger message="Sending Message: #[payload]" level="INFO" doc:name="Logger" />
<jms:outbound-endpoint exchange-pattern="request-response"
queue="myQueue" connector-ref="Active_MQ" doc:name="JMS" />
</flow>
<flow name="jmsmessagereceiver" doc:name="jmsmessagereceiver">
<jms:inbound-endpoint queue="myQueue" connector-ref="Active_MQ" doc:name="JMS"
exchange-pattern="request-response"/>
<logger message="Received Message: #[payload]" level="INFO" doc:name="Logger"/>
<set-payload value="Hello World from Receiver" doc:name="Set Payload"/>
<logger message="New Message Back to Sender: #[payload]" level="INFO" doc:name="Logger"/>
</flow>
Approach 2: using request-reply message processor
<flow name="jmsmessagepublisher" doc:name="jmsmessagepublisher">
<http:inbound-endpoint exchange-pattern="request-response"
host="localhost" port="1111" path="backchannel" doc:name="HTTP" />
<set-payload value="Hello World from Sender" doc:name="Set Payload"/>
<logger message="Sending Message: #[payload]" level="INFO" doc:name="Logger"/>
<request-reply doc:name="Request-Reply">
<jms:outbound-endpoint queue="reqQueue" connector-ref="Active_MQ" doc:name="JMS"/>
<jms:inbound-endpoint queue="replyQueue" connector-ref="Active_MQ" doc:name="JMS"/>
</request-reply>
<logger message="Final Response: #[payload]" level="INFO" doc:name="Logger"/>
</flow>
<flow name="jmsmessagereceiver" doc:name="jmsmessagereceiver">
<jms:inbound-endpoint queue="reqQueue" connector-ref="Active_MQ" doc:name="JMS"/>
<logger message="Received Message: #[payload]" level="INFO" doc:name="Logger"/>
<set-payload value="Hello World from Receiver" doc:name="Set Payload"/>
<logger message="New Message Back to Sender: #[payload]" level="INFO" doc:name="Logger"/>
<jms:outbound-endpoint queue="replyQueue" connector-ref="Active_MQ" doc:name="JMS"/>
</flow>
How it works
Testing and observation
http://localhost:1111/backchannel
Approach 1 vs Approach 2
Conclusion
JMS synchronous communication has an advantage of complete decoupling between the client and service. As described in this blog, the above mentioned approaches for JMS synchronous messaging using Mule ESB has their own set of advantages and disadvantages.
The implementation approach can be chosen on the basis of use case for the project.
If you would like to find out more about how Systems Integration could help you make the most out of your current infrastructure while enabling you to open your digital horizons, do give us a call at +44 (0)203 475 7980 or email us at Salesforce@coforge.com
Other useful links:
API Recipes with MuleSoft Anypoint Platform