Coming from a BizTalk background, discovering differences between BizTalk and Mule is really interesting. Not necessarily to find one better than the other, but to appreciate differences in design and understand philosophy behind it.
Today I tried Set Payload Transformer and was amused with how it accesses and set payload with message context using Mule expression.
This transformer sets the payload to whatever you define. The payload can be a literal string or a Mule Expression.
I was particularly interested with being able to set the payload to whatever you define.
Set Payload is basically like a creating a response message for a request-response pattern in BizTalk.
In BizTalk, you will have to create a message schema and outputs xml message(flat file schema if you want to output flat structure of message).
Mule instead provides this convenient transformer called Set Payload and from there, you can set whatever you define - doesn’t have to be xml and you could access to message’s context information using Mule expression. Pretty cool?
You can find source code for this practice here.
How does it work?
You can double click the Set Payload transformer and with the Value, set whatever you’d like to define. Example shows that combination of string, path requested and datetime.
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:file="http://www.mulesoft.org/schema/mule/file" 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:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 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/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd"> <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/> <flow name="how-to-use-set-payloadFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="*" doc:name="HTTP"/> <logger message="Current path is #[message.inboundProperties.'http.request.path']" level="INFO" doc:name="Logger"/> <set-payload value="#['Hello, ' + message.inboundProperties.'http.request.path' + '. Today is ' + server.dateTime.format('MM/dd/yy')]" doc:name="Set Payload"/> <file:outbound-endpoint path="target" outputPattern="#[message.id].txt" responseTimeout="10000" doc:name="File"/> </flow> </mule>
With this, you know you can access message’s context information as well as server’s context information. Then, how do you know what context information are available from Mule Expression?
Following is a good reference for that.
But it is important to know that there are following four contexts available since they are at the heart of most MEL expression.
- The operating system on which the message processor is running
- The Mule instance on which the application is running
- The user application within which the current flow is deployed
- The package (payload, attachments, properties) that the message processor is processing