In this article we will see how to explicitly add a mule response to the flow. We will first see the default behaviour when there is no <response>
element.
Default Response
In the below mule context XML, we have a VM
inbound point which is the source of message. The message received is then sent to a custom processor that simply appends a string. Since the exchange-pattern
is set to request-response
, there will be an implicit response though we have not set one.
Response will be the message that is at the end of the request phase.
muleContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:spring="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.8/mule-vm.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.8/mule.xsd "> <flow name="flow1"> <vm:inbound-endpoint path="noResponse.msg" exchange-pattern="request-response"> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="A" /> </custom-processor> </vm:inbound-endpoint> <vm:outbound-endpoint path="out.msg" /> </flow> </mule>
If exchange-pattern
is set to one-way
you will get an error:
Caused by: org.mule.api.transport.NoReceiverForEndpointException: There is no receiver registered on connector "connector.VM.mule.default" for endpointUri vm://noResponse.msg
The processor will simply append a string to the end of the payload.
StringAppender:
package com.javarticles.mule; import org.mule.api.MuleEvent; import org.mule.api.MuleException; import org.mule.api.processor.MessageProcessor; public class StringAppender implements MessageProcessor { private String suffix; public MuleEvent process(MuleEvent event) throws MuleException { event.getMessage().setPayload(event.getMessage().getPayload() + suffix); return event; } public void setSuffix(String suffix) { this.suffix = suffix; } }
We will now create the mule context and post a message to the VM>
inbound endpoint.
MuleResponseExample:
package com.javarticles.mule; import org.mule.api.MuleContext; import org.mule.api.MuleMessage; 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; import org.mule.module.client.MuleClient; public class MuleResponseExample { 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 = new MuleClient(ctx); String payload = "XYZ"; System.out.println("Sending payload [" + payload + "] to vm://defaultResponse.msg"); MuleMessage message = muleClient.send("vm://noResponse.msg", payload, null); System.out.println("Response: " + message.getPayload()); } finally { ctx.dispose(); } } }
Output:
Sending payload [XYZ] to vm://defaultResponse.msg Response: XYZA
Add response element
We will now create another flow and this time add a response
element.
muleContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:spring="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.8/mule-vm.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.8/mule.xsd "> <flow name="flow1"> <vm:inbound-endpoint path="noResponse.msg" exchange-pattern="request-response"> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="A" /> </custom-processor> </vm:inbound-endpoint> <vm:outbound-endpoint path="out.msg" /> </flow> <flow name="flow2"> <vm:inbound-endpoint path="response.msg" exchange-pattern="request-response"> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="A" /> </custom-processor> <response> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="B" /> </custom-processor> </response> </vm:inbound-endpoint> <response> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="C" /> </custom-processor> </response> </flow> </mule>
We will post a new message to vm://noResponse.msg
to kick off our new flow flow2
.
MuleResponseExample:
package com.javarticles.mule; import org.mule.api.MuleContext; import org.mule.api.MuleMessage; 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; import org.mule.module.client.MuleClient; public class MuleResponseExample { 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 = new MuleClient(ctx); String payload = "XYZ"; System.out.println("Sending payload [" + payload + "] to vm://defaultResponse.msg"); MuleMessage message = muleClient.send("vm://noResponse.msg", payload, null); System.out.println("Response: " + message.getPayload()); System.out.println("Sending payload [" + payload + "] to vm://response.msg"); message = muleClient.send("vm://response.msg", payload, null); System.out.println("Response: " + message.getPayload()); } finally { ctx.dispose(); } } }
You can see from the output, the response phase comes in after the inbound endpoint’s message processor is run. If there are multiple responses, responses get added in reverse order. The response embedded in the inbound endpoint flow is called in after the main flow’s response.
Output:
Sending payload [XYZ] to vm://defaultResponse.msg Response: XYZA Sending payload [XYZ] to vm://response.msg Response: XYZACB
Multiple responses
Let’s now try out a more complex mule configuration with multiple responses.
We add two new flows to our mule context XML. We have placed response elements in the main flow and in the endpoints (inbound and outbound).
Add the below to mule context XML.
muleContext.xml:
<flow name="flow3"> <vm:inbound-endpoint path="multiResponse.msg" exchange-pattern="request-response"> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="A" /> </custom-processor> <response> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="B" /> </custom-processor> </response> </vm:inbound-endpoint> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="C" /> </custom-processor> <response> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="D" /> </custom-processor> </response> <vm:outbound-endpoint path="out.msg" exchange-pattern="request-response"> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="E" /> </custom-processor> <response> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="F" /> </custom-processor> </response> </vm:outbound-endpoint> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="H" /> </custom-processor> <response> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="I" /> </custom-processor> </response> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="J" /> </custom-processor> </flow> <flow name="flow4"> <vm:inbound-endpoint path="out.msg" exchange-pattern="request-response"> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="K" /> </custom-processor> <response> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="L" /> </custom-processor> </response> </vm:inbound-endpoint> <response> <custom-processor class="com.javarticles.mule.StringAppender"> <spring:property name="suffix" value="M" /> </custom-processor> </response> </flow>
We will now post a message to vm://multiResponse.msg
.
System.out.println("Sending payload [" + payload + "] to vm://multiResponse.msg"); message = muleClient.send("vm://multiResponse.msg", payload, null); System.out.println("Response: " + message.getPayload());
Output:
Sending payload [XYZ] to vm://multiResponse.msg Response: XYZACEKMLFHJIDB
Download the source code
This was an example about mule response.