Sunday 10 March 2019

Target Variable in Mule-4 connector

Significance of Target Variable in Mule-4 Connector:

This article explains about Target variable in Mule-4 connector. Target Variable is used to store the contents of service call. In earlier versions of Mule, Message Enricher component need to used to achieve this. This process is extremely simplified by introducing the target variable to the connector in Mule -4.

Let me explain this functionality with an example:

 


The example contains two flows Flow-1 and Flow-2. Flow-1 flow as VM publish-Consume endpoint
that invokes the VM listener of flow-2. once the Flow-2 finishes its execution, the payload return to Flow-1 and it replaces the existing payload of Flow-1, so the existing flow-1 payload vanishes.

Collecting the response from Flow-2 to a Variable rather than replacing it to the existing payload is a great idea, it simplifies the developer life to a greater extent. to achieve the similar functionality in Mule earlier versions there are 2 options.
a) Use Message Enricher scope to configure such calls
b) Save the current payload to a variable before calling Service, after the service call, the ,existing payload is replaced by service call payload, Override the service call payload with payload stored in variable.

Code without Target variable :
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">
<vm:config name="VM_Config" doc:name="VM Config" doc:id="eaa6afe4-3cbf-4de0-b904-449c354a92e9" >
<vm:queues >
<vm:queue queueName="myqueue" />
</vm:queues>
</vm:config>
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="08907f91-2a21-434c-96d0-eaa5840ba384" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="Flow-1" doc:id="6ebd4e1b-2cdb-4ad2-ba9b-08ef2663f947" >
<http:listener doc:name="Listener" doc:id="05a7dd8e-b363-4153-83bd-f33f8cac1dd3" config-ref="HTTP_Listener_config" path="/"/>
<set-payload value="payload set in main flow" doc:name="Set Payload" doc:id="a94833c0-2de2-4c18-ad89-d3ddac7ca487" />
<vm:publish-consume doc:name="Publish consume" doc:id="81ab6415-d9e0-4c74-af9f-3361c1abd485" config-ref="VM_Config" queueName="myqueue"/>
<logger level="INFO" doc:name="Logger" doc:id="32d83206-906f-493d-ba8f-3fbfc8342e53" message="After VM call  #[payload]  #[vars.flow2Value_variable]"/>
</flow>
<flow name="Flow-2" doc:id="50f343ec-bbb4-48d5-99e5-05383f3ceb06" >
<vm:listener queueName="myqueue" doc:name="Listener" doc:id="f8618bee-2005-4966-ae8b-da8ede2d367a" config-ref="VM_Config"/>
<set-payload value="payload set in VM flow" doc:name="Set Payload" doc:id="2f3a59df-2748-45e8-8ba7-2f543d2ee7ba" />
</flow>
</mule>

Logger Statement. showing the payload returned from Flow-2:
.LoggerMessageProcessor: After VM call  payload set in VM flow  null


Target Variable configuration:



<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">
<vm:config name="VM_Config" doc:name="VM Config" doc:id="eaa6afe4-3cbf-4de0-b904-449c354a92e9" >
<vm:queues >
<vm:queue queueName="myqueue" />
</vm:queues>
</vm:config>
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="08907f91-2a21-434c-96d0-eaa5840ba384" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="Flow-1" doc:id="6ebd4e1b-2cdb-4ad2-ba9b-08ef2663f947" >
<http:listener doc:name="Listener" doc:id="05a7dd8e-b363-4153-83bd-f33f8cac1dd3" config-ref="HTTP_Listener_config" path="/"/>
<set-payload value="payload set in main flow" doc:name="Set Payload" doc:id="a94833c0-2de2-4c18-ad89-d3ddac7ca487" />
<vm:publish-consume doc:name="Publish consume" doc:id="81ab6415-d9e0-4c74-af9f-3361c1abd485" config-ref="VM_Config" queueName="myqueue" target="flow2Value_variable"/>
<logger level="INFO" doc:name="Logger" doc:id="32d83206-906f-493d-ba8f-3fbfc8342e53" message="After VM call  #[payload]  #[vars.flow2Value_variable]"/>
</flow>
<flow name="Flow-2" doc:id="50f343ec-bbb4-48d5-99e5-05383f3ceb06" >
<vm:listener queueName="myqueue" doc:name="Listener" doc:id="f8618bee-2005-4966-ae8b-da8ede2d367a" config-ref="VM_Config"/>
<set-payload value="payload set in VM flow" doc:name="Set Payload" doc:id="2f3a59df-2748-45e8-8ba7-2f543d2ee7ba" />
</flow>
</mule>

Logger Statement. showing the  existing payload and  target variable
.LoggerMessageProcessor: After VM call  payload set in main flow  payload set in VM flow

1 comment:

  1. This was the best explanation for target variable. Thanks a lot.

    ReplyDelete

How to Design Mule API to process Attachments

This blog Explains , how to design Mule API to process attachments. Quite often we get requirement to design API's that process attachme...