In this article we will look into the mule property “replyTo”. A JMS inbound endpoint with exchange pattern set to request-response indicates that a response is expected from the JMS inbound endpoint . Mule achieves this by creating a temporary queue to which the response data is sent. The queue name is set in the MULE_REPLYTO property of the responding JMS message.
Mule ReplyTo Example
In the below example, we have a flow where a response is expected from the JMS inbound endpoint “out1”.
muleContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns:test="http://www.mulesoft.org/schema/mule/test" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/schema/mule/test/current/mule-test.xsd http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd"> <jms:activemq-connector name="jmsConnector" /> <jms:endpoint name="out1" queue="out1" exchange-pattern="request-response" /> <flow name="replyToFlow"> <jms:inbound-endpoint ref="out1"> <response> <append-string-transformer message=" - response from jms:out1" /> </response> </jms:inbound-endpoint> <message-properties-transformer> <add-message-property key="someProp" value="somePropValue" /> </message-properties-transformer> </flow> </mule>
We set the replyTo property to specify the temporary queue.
String replyToUri = "jms://replyFromOut"; messageProperties.put(MuleProperties.MULE_REPLY_TO_PROPERTY, replyToUri);
We next post a message to JMS inbound endpoint and then fetch the reply from the temporary queue.
muleClient.dispatch("jms://out1", "Message to jms:out1", messageProperties); MuleMessage response = muleClient.request(replyToUri, 2000);
MuleReplyToPropertyExample:
package com.javarticles.mule; import java.util.HashMap; import java.util.Map; import org.mule.api.MuleContext; import org.mule.api.MuleMessage; import org.mule.api.client.MuleClient; import org.mule.api.config.MuleProperties; import org.mule.api.context.MuleContextBuilder; import org.mule.api.context.MuleContextFactory; import org.mule.config.DefaultMuleConfiguration; import org.mule.config.spring.SpringXmlConfigurationBuilder; import org.mule.context.DefaultMuleContextBuilder; import org.mule.context.DefaultMuleContextFactory; public class MuleReplyToPropertyExample { public static void main(String[] args) throws Exception { DefaultMuleConfiguration dmc = new DefaultMuleConfiguration(); dmc.setId("muleexample"); dmc.setWorkingDirectory("/esb/mule"); SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder( "muleContext.xml"); MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder(); contextBuilder.setMuleConfiguration(dmc); MuleContextFactory contextFactory = new DefaultMuleContextFactory(); MuleContext ctx = contextFactory.createMuleContext(configBuilder, contextBuilder); ctx.start(); try { MuleClient muleClient = ctx.getClient(); Map<String, Object> messageProperties = new HashMap<String, Object>(); String replyToUri = "jms://replyFromOut"; messageProperties.put(MuleProperties.MULE_REPLY_TO_PROPERTY, replyToUri); muleClient.dispatch("jms://out1", "Message to jms:out1", messageProperties); MuleMessage response = muleClient.request(replyToUri, 2000); System.out.println("Reply from jms:out[" + response.getPayloadAsString() + "]"); System.out.println("Inbound property[someProp]=" + response.getInboundProperty("someProp")); } finally { ctx.dispose(); } } }
Output:
[INFO ] 2018-05-13 21:32:35.485 [ActiveMQ Session Task-1] JmsReplyToHandler - Reply Message sent to: queue://replyFromOut with correlationID:0d263ad0-56c7-11e8-b736-a0afbd832eee Reply from jms:out[Message to jms:out1 - response from jms:out1] Inbound property[test]=somePropValue
Disabling Mule ReplyTo Response
When an incoming message has the replyTo property set, the flow in context can disable the reply by setting the below variable anywhere in the flow:
<set-variable variableName="MULE_REPLYTO_STOP" value="true" name="Variable"/>
Here is the flow for disabling “replyTo”:
muleContext.xml:
<flow name="disableReplyToFlow"> <jms:inbound-endpoint ref="out2"> <response> <append-string-transformer message=" - response from jms:out" /> </response> </jms:inbound-endpoint> <message-properties-transformer> <add-message-property key="someProp" value="somePropValue" /> </message-properties-transformer> <set-variable variableName="MULE_REPLYTO_STOP" value="true" name="Variable"/> </flow>
In order to kick off the flow, we do exactly same as our previous example, expectation is that we don’t get any response.
System.out.println("Post message to JMS inbound flow jms:out2 with replyTo disabled"); muleClient.dispatch("jms://out2", "Message to jms:out2", messageProperties); response = muleClient.request(replyToUri, 2000); if (response == null) { System.out.println("Response is null as replyTo is disabled"); }
Output:
Response is null as replyTo is disabled
Download the source code
This was an example about Mule ReplyTo property and how we can use it to fetch response.