Mule message-properties-transformer example

0

In this article we will see how to explicitly add an outbound propertie. Before we start we must first understand the definition of a message property and how it fits in the overall message structure.

  1. A Mule message contains the following parts:
  2. Metadata – Scope based properties
  3. Payload – Main data
  4. Named attachments (inbound, outbound)- Multipart message
  5. Exception Payload – Holds any exception during the processing of the event

Next we need to know the difference between an inbound property and an outbound property.

Inbound properties come into the flow as part of the inbound message and they are immutable. As soon the message passes out of the flow, either into another flow or application, the inbound properties ceases to exist.
In contrast outbound properties are mutable, they are set during the course of a flow or in the response phase. As soon as the message leaves the current flow and enters into a new flow or returns to the application, the outbound properties of previous flow become inbound properties of the message.

In this article, we will show you how to manually set an outbound property manually using a message properties transformer in the flow.

A Message Properties Transformer lets you add, rename, or delete message properties. In this example, we will add a new outbound property, of course you can apply it in any scope (invocation, outbound, session or application) you want.

<message-properties-transformer scope="outbound">
    <add-message-property key="test" value="test"/>
</message-properties-transformer>

You can also use it to propagate an inbound property to an outbound.

<add-message-property key="foo" value="#[header:INBOUND:someInboundProp]"/>

Below we are setting an http outbound property:

<add-message-property key="Content-Type" value="text/plain;charset=UTF-8"/>

Delete a message property:

<delete-message-property key="someProperty"/>

Rename a message property:

<rename-message-property key="someProperty1" value="someProperty2"/>

In the below flow, the message sent to vm://in is the passed on to an outbound point vm://out which takes it to next flow out-flow. A static message “Response from out” is appended to the input. The message returns to the first flow in-flow where message ” – appended to out response” is appended and returned as the response. In the response phase we add a new outbound property. Since the exchange pattern used is “request-response”, the flow response is returned to the application. The property added in outbound scope is now accessible in inbound scope.

System.out.println("Inbound property value of 'test':" + response.getProperty("test", PropertyScope.INBOUND));

muleContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<mule 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"
      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/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">

    <flow name="in-flow">
        <vm:inbound-endpoint path="in" exchange-pattern="request-response"/>
        <vm:outbound-endpoint path="out" exchange-pattern="request-response">
            <response>
                <message-properties-transformer scope="outbound">
                    <add-message-property key="test" value="test"/>
                </message-properties-transformer>
                <append-string-transformer message=" - appended to out response" />
            </response>
        </vm:outbound-endpoint>
    </flow>

    <flow name="out-flow">
        <vm:inbound-endpoint path="out" exchange-pattern="request-response"/>
        <static-component>
            <return-data>Response from out</return-data>
        </static-component>
    </flow>

</mule>

MuleMessagePropertiesTransformerExample:

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.api.transport.PropertyScope;
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 MuleMessagePropertiesTransformerExample {
    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);
            MuleMessage response = muleClient.send("vm://in", "some message", null);        
            System.out.println("Response: " + response.getPayloadAsString());
            System.out.println("Inbound property value of 'test':" + response.getProperty("test", PropertyScope.INBOUND));
        } finally {
            ctx.dispose();
        }
    }
}

Output:

Response: Response from out - appended to out response
Inbound property value of 'test':test            

Download the source code

This was an example about mule adding properties to the message.

You can download the source code here: muleMessagePropertiesExample.zip
Share.

Comments are closed.