Message exchange patterns (MEP) defines the way a client interacts with an endpoint. There are two MEPs – one-way and request-response.
When a client sends a message to an point defined as one-way, it doesn’t expect any response in return whereas in case of request-response a synchronous response is expected.
In this example we will see an example of request-response.
Inbound-point using request-response
The message received by the inbound endpoint is passed on to a transformer which simply appends a string to the payload.
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" xmlns:test="http://www.mulesoft.org/schema/mule/test" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/schema/mule/test/current/mule-test.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="in1" exchange-pattern="request-response" /> <custom-transformer class="com.javarticles.mule.CustomTransformer"> <spring:property name="append" value="flow1(append_to_in1)" /> </custom-transformer> </flow> </mule>
Here is the message transformer.
CustomTransformer:
package com.javarticles.mule; import org.mule.api.MuleMessage; import org.mule.transformer.AbstractMessageTransformer; public class CustomTransformer extends AbstractMessageTransformer { private String append; @Override public Object transformMessage(MuleMessage message, String encoding) { message.setPayload(message.getPayload() + append); return message; } public String getAppend() { return append; } public void setAppend(String append) { this.append = append; } }
Let’s now send a message to the inbound endpoint to see its behavior.
MuleMessageExchangeExample:
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 MuleMessageExchangeExample { 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-"; MuleMessage message = muleClient.send("vm://in1", payload, null); System.out.println("Response: " + message.getPayloadAsString()); } finally { ctx.dispose(); } } }
Output:
Response: XYZ-flow1(append_to_in1)
Outbound endpoint using request-response
We now add an outbound endpoint with request-response as the MEP.
muleContext.xml:
<flow name="flow2"> <vm:inbound-endpoint path="in2" exchange-pattern="request-response" /> <custom-transformer class="com.javarticles.mule.CustomTransformer"> <spring:property name="append" value="flow2(append_to_in2)" /> </custom-transformer> <vm:outbound-endpoint path="out" exchange-pattern="request-response" /> <custom-transformer class="com.javarticles.mule.CustomTransformer"> <spring:property name="append" value="flow2(append_to_out_reponse)" /> </custom-transformer> </flow> <flow name="flow3"> <vm:inbound-endpoint path="out" exchange-pattern="request-response" /> <custom-transformer class="com.javarticles.mule.CustomTransformer"> <spring:property name="append" value="flow3(append_to_out)" /> </custom-transformer> </flow>
We now send a message to the inbound-endpoint vm://in2
.
message = muleClient.send("vm://in2", payload, null); System.out.println("Response: " + message.getPayloadAsString());
As you can see the message sent to the inbound endpoint is transformed and then sent to the outbound endpoint vm://out
. This kicks off the flow3
which is listening on the inbound endpoint vm://out
. Since this endpoint is defined with request-response
, the response created is sent back to the flow2
.
Output:
Response: XYZ-flow2(append_to_in2)flow3(append_to_out)flow2(append_to_out_reponse)
Outbound endpoint with response element
We now create a new flow flow4
similar to the above flow with a new response element.
muleContext.xml:
<flow name="flow3"> <vm:inbound-endpoint path="out" exchange-pattern="request-response" /> <custom-transformer class="com.javarticles.mule.CustomTransformer"> <spring:property name="append" value="flow3(append_to_out)" /> </custom-transformer> </flow> <flow name="flow4"> <vm:inbound-endpoint path="in3" exchange-pattern="request-response" /> <custom-transformer class="com.javarticles.mule.CustomTransformer"> <spring:property name="append" value="flow4(append_to_in3)" /> </custom-transformer> <response> <custom-transformer class="com.javarticles.mule.CustomTransformer"> <spring:property name="append" value="flow4(append_to_in3_reponse)" /> </custom-transformer> </response> <vm:outbound-endpoint path="out" exchange-pattern="request-response" /> <custom-transformer class="com.javarticles.mule.CustomTransformer"> <spring:property name="append" value="flow4(append_to_out_reponse)" /> </custom-transformer> </flow>
We now send a message to the inbound endpoint vm://in3
.
message = muleClient.send("vm://in3", payload, null); System.out.println("Response: " + message.getPayloadAsString());
Output:
Response: XYZ-flow4(append_to_in3)flow3(append_to_out)flow4(append_to_out_reponse)flow4(append_to_in3_reponse)
Download the source code
This article is about mule’s request-response
.
You can download the source code here: muleRequestResponseExample.zip