简体   繁体   中英

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:

org.mule.RequestContext.getEvent().getMessage()

but it is deprecated.

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

Thanks,

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"
        method="myMethod"
        methodArguments="#[message.payload],#[flowVars['my-variable-name']]" />

Use the message.getInvocationProperty method.

Setting variable:

<set-variable variableName="lastname" value="#[payload.lastname]" />

Retrieve variable from invocation scope:

String lastname = message.getInvocationProperty("lastname");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM