4

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.

gmojunior
  • 292
  • 1
  • 4
  • 7

2 Answers2

2

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");
David Dossot
  • 33,403
  • 4
  • 38
  • 72
Richard Donovan
  • 435
  • 3
  • 10
  • I cannot just put in my java component: "public Object onCall(MuleEventContext eventContext, @Payload String payload)" It not works. Do I have to extend any class? – gmojunior Aug 23 '13 at 18:19
  • 1
    As I said, I was looking for a complete answer, so I have to implements Callable, to use the onCall method, and I had to change the property to PropertyScope.INVOCATION. If you debug the Mule Message (eventContext.getMessage()), you will see the Message properties, that are INVOCATION, INBOUND, OUTBOUND and SESSION scoped properties. Thanks for the replies. – gmojunior Aug 23 '13 at 19:01
1

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);
Dongho Yoo
  • 1,506
  • 16
  • 16