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

Friday 8 March 2019

Mule 4 Event Model




Mule Event and Mule Message:
When a request reaches the Event source of a flow (such as an HTTP request
 or a change to a database or file).  A mule event will be generated it creates mule event object. Mule event object primarily consists of information required to process by the Mule runtime. Mule event object travels through all the components configured in mule flow.

Mule event object is immutable, so every change to an instance of a Mule event object results in the creation of a new instance.
A Mule Event object is composed of these objects:
  1. A Mule Message contains a message payload and its associated attributes.
  2.  Variables are Mule event metadata that you use in your flow.

The sequence of the activities happens when event source receives the trigger
  • A trigger reaches the event source.    
  • The event source produces a Mule event.
  •  The Mule event travels sequentially through the components of a flow.Each component interacts in a pre-defined manner with the Mule event




How the Mule Event object get created from the incoming request ?
Standard message structure consists of Header and Payload parts, where Payload is core business information that need to processed and Header contains details about payload like encryption schemes, security credentials etc. Header content also called metadata. 

When a standard message reaches the Mule Event source (in other words its called Inbound Endpoint), Event source converts that message to Mule Event Object as Mule Event processors can only understand only Mule Event object. and handovers to next event processor.

Mule Event source creates Mule Event object by coping Header contents to Attributes section and Payload contents to Payload section



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

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:file="http://www.mulesoft.org/schema/mule/file"
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/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<file:config name="File_Config" doc:name="File Config" doc:id="ce7b6ee8-e9f2-4370-b17f-2adf413d9826" >
</file:config>
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="825c4787-27c3-4595-baa3-5fa66d3ca97f" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="mule_event_demoFlow" doc:id="1bbf3a21-775f-49eb-9d06-48bb9f9a51dd" >
<http:listener doc:name="Listener" doc:id="a287d233-9054-423c-818d-e73a1fa67b51" config-ref="HTTP_Listener_config" path="/"/>
<logger level="INFO" doc:name="Logger" doc:id="cb41a866-d0a9-4a06-b7f4-5f75b3c62272" message="The City from source is #[message.attributes.headers.City]"/>
<set-payload value="This payload is returned to client" doc:name="Set Payload" doc:id="2ab7bb3e-3943-4da1-9c54-c76907348924" />
</flow>
</mule>


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...