How to access Mule flow variables in java code?

Ajv2324 picture Ajv2324 · Jun 11, 2015 · Viewed 8.5k times · Source

Browsing around, I found something that look like below:

import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;
import java.util.Scanner;

public class javaTest extends AbstractMessageTransformer{
    public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException{

        message.getInvocationProperty("test");
        return message;
    }
}

I am using this code, where message is a Mule flow variable that I just declared as "test". The problem is, when I send the payload, I get "{NullPayload}", which is not quite right. I just want it to return string "test".

What am I doing wrong here? My Mule code looks like so:

<?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:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.0"
    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:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
    <http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="hooks.slack.com" doc:name="HTTP Request Configuration"/>
    <flow name="testFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <set-variable variableName="test" value="Test" doc:name="Variable"/>
        <custom-transformer class="javaTest" doc:name="HelloWorld"/>
        <set-payload value="{&quot;channel&quot;: &quot;#testing&quot;, &quot;username&quot;: &quot;Testing&quot;, &quot;text&quot;: &quot;#[payload]&quot;}" doc:name="Set Payload"/>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        <http:request config-ref="HTTP_Request_Configuration" path="/services/slackURL" method="POST" doc:name="HTTP"/>
    </flow>
</mule>

Answer

Ajv2324 picture Ajv2324 · Jun 11, 2015

Whether or not this is the proper fix I am unsure, but what I've done to fix this problem is set a variable in the java code to the value of the message.getInvocationProperty, then return that java variable, like so:

import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;

public class javaTest extends AbstractMessageTransformer{
    public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException{
        String testS = message.getInvocationProperty("test");
        return testS;
    }
}