Loic Duros Loic Duros - 1 year ago 121
Java Question

Accessing MuleMessage and a flow variable from Java component

I have a Java component that receives a payload (JsonData), like this:

public String myMethod(JsonData payload) throws IOException {
// do things with payload.

This is working fine, but I also need to access a flow variable within the method. I understand that, to do so, I need to run myMessage.getInvocationProperty("my-variable-name");
However, since I'm only passing the payload, I don't have access to the MuleMessage. How can I change my method so I can get access to my message/property?

I tried:


but it is deprecated.

Also, I've read all sorts of answers on this issue but never found a complete answer.


Answer Source

Pass the flow variable as a second argument to myMethod via the invoke message processor.

So, assuming the new signature of myMethod is:

public String myMethod(JsonData payload, String myVariable) throws IOException {
  // do things with payload.

you would do:

<invoke object-ref="myComponent"
        methodArguments="#[message.payload],#[flowVars['my-variable-name']]" />