1

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,

Loic Duros
  • 5,472
  • 10
  • 43
  • 56

2 Answers2

3

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']]" />
David Dossot
  • 33,403
  • 4
  • 38
  • 72
  • Thanks for the pointers, but this is not a complete solution, yet. I have very little experience with either Java or Mule, and so I'm still a little puzzled by what you mean with 'via the invoke' message processor. What would it look like concretely? – Loic Duros Apr 04 '14 at 13:35
  • For sure, I've extended my answer. – David Dossot Apr 04 '14 at 15:32
  • 1
    @DavidDossot Hi, David. What I need within Java component is the whole MuleMesage(Want to enrich it in Java). I tried pass #[message], but got error 'Could not find a transformer to transform \"SimpleDataType{type=org.mule.el.context.MessageContext, mimeType='*/*', encoding='null'}\" to \"SimpleDataType{type=org.mule.api.MuleMessage, mimeType='*/*', encoding='null'}\" Also, with annotations @Expr(expression="#[message]"), seems it is trying to cast result to String, so doesn't work either. – Kymair Wu Dec 06 '16 at 15:53
  • Same here. I am trying to pass #[message] as method argument. I get the same exception – RuntimeException Aug 24 '17 at 16:41
  • @DavidDossot I have created a java object using the new component and I am trying to pass it in the java function. Can you please look into this question ? https://stackoverflow.com/questions/59558707/how-to-pass-java-object-to-a-java-function-in-mule Thanks for the help . – HMT Jan 02 '20 at 05:58
0

Use the message.getInvocationProperty method.

Setting variable:

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

Retrieve variable from invocation scope:

String lastname = message.getInvocationProperty("lastname");
Ben Asmussen
  • 964
  • 11
  • 15