gmojunior
gmojunior

Reputation: 292

Access flow variable in java code mule

I stored some information in a variable, but I do not know how to access it in my java code...

Example:

<sub-flow name="EnrichMessage" doc:name="EnrichMessage">
    <component doc:name="Scenario01" class="Class01"/>
    <set-variable variableName="Parameters" value="#[payload]" doc:name="Variable"/>
    <flow-ref name="SubFlow01" doc:name="SubFlow01"/>
    <component doc:name="Scenario02" class="Class02"/>
</sub-flow>

I already saw some incomplete answers, but still don't know how to do that. Anyone can post a complete answer?

Thanks.

Upvotes: 4

Views: 12149

Answers (2)

Dongho Yoo
Dongho Yoo

Reputation: 1516

Add new Java Component to your flow and create new Java class implement Callable interface.

public Object onCall(MuleEventContext eventContext) throws Exception {
    MuleMessage msg = eventContext.getMessage();

    // you can access MuleMessage here

    return msg;
}

Then, you can access your MuleMessage.

String method = msg.getProperty("http.method", PropertyScope.INBOUND);        

If you want to add new property

msg.setProperty("foo", "bar", PropertyScope.INVOCATION);

Upvotes: 1

Richard Donovan
Richard Donovan

Reputation: 425

In java there are a few ways to access variables depending the type of java class you are using:

onCall event class

 public Object onCall(MuleEventContext eventContext, @Payload String payload)
 throws Exception {
    String returnPath = eventContext.getMessage().getProperty("myReturnPath", PropertyScope.OUTBOUND);

If the MuleMessage is passed:

 public void process(@Payload MuleMessage payload ){
 String returnPath = messge.getProperty("myReturnPath", PropertyScope.OUTBOUND);

Using an OutboundHeader annotation

  public void process(@Payload String payload, @OutboundHeaders Map headers ){
    String prop = headers.get("propname");

Upvotes: 2

Related Questions