Mule Invocation Scoped Property

0

In this article we will look into example of invocation scope. The scope of an inbound property ends where theĀ outbound endpoint beginsĀ whereas this is not the case with the invocation scoped property.

Add invocation scoped property

In the below mule context, we use <message-properties-transformer> to add a message property with invocation scope. We then post the message to an outbound endpoint and verify the invocation property in the second flow.

Life of invocation scope

You will see that the invocation scoped property is only available within the flow it is added. You won’t be able to access when the message traverses to another flow as a result of an outbound endpoint.

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="in"
			exchange-pattern="request-response" />
		<message-properties-transformer scope="invocation">
			<add-message-property key="invocation_property"
				value="Invocation Example" />
		</message-properties-transformer>
		<vm:outbound-endpoint path="out_res"
			exchange-pattern="request-response" />
		<custom-processor class="com.javarticles.mule.PrintInvocationProperty">
			<spring:property name="property" value="invocation_property" />
		</custom-processor>		
	</flow>

	<flow name="flow2">
		<vm:inbound-endpoint path="out_res"
			exchange-pattern="request-response" />
		<custom-processor class="com.javarticles.mule.PrintInvocationProperty">
			<spring:property name="property" value="invocation_property" />
		</custom-processor>		
	</flow>

</mule>

Access Invocation scoped property

Below is the custom processor that retrieves the invocation property and prints.

PrintInvocationProperty:

package com.javarticles.mule;

import org.mule.api.MuleEvent;
import org.mule.api.MuleException;
import org.mule.api.processor.MessageProcessor;

public class PrintInvocationProperty implements MessageProcessor {
    private String property;

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    public MuleEvent process(MuleEvent event) throws MuleException {
        Object invocationProperty = event.getMessage().getInvocationProperty(property);
        System.out.println(getProperty() + ": "+ invocationProperty);
        return event;
    }
}

Mule invocation scoped property example

We start the flow by posting a message to inbound endpoint vm://in. After the response, we verify whether the invocation property still exists.

MuleInvocationScopeExample:

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 MuleInvocationScopeExample {
    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);
            sendMsgAndVerifyProperties("vm://in", ctx, muleClient);           
        } finally {
            ctx.dispose();
        }
    }
    
    private static void sendMsgAndVerifyProperties(String endpointUri, MuleContext ctx,
            MuleClient muleClient) throws Exception {
        System.out.println("send message to " + endpointUri);
        MuleMessage response = muleClient.send(endpointUri, "Hello", null);   
        printInboundProperties(response);
    }
    
    private static void printInboundProperties(MuleMessage response) throws Exception {
        System.out.println("Verify properties after response");
        System.out.println("Response: " + response.getPayloadAsString());
        System.out.println("invocation_property: " + response.getInvocationProperty("invocation_property"));
    }
}

As you can see from the below output the invocation scoped property is simply a flow variable as it can only be accessed within a flow. The invocation property is not available when the message traverses to the second flow but is available when it comes back from the outbound endpoint to the first flow.

Use invocation properties if you need to a property that you plan to use in multiple places within the same flow.

Output:

send message to vm://in
invocation_property: null
invocation_property: Invocation Example
Verify properties after response
Response: Hello
invocation_property: null

Download the source code

This was an example of inbound scoped property.

You can download the source code here: muleInvocationScopeExample.zip

About Author

Ram's expertise lies in test driven development and re-factoring. He is passionate about open source technologies and loves blogging on various java and open-source technologies like spring. You can reach him at [email protected]

Comments are closed.