Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing MuleMessage to Java method with Mule invoke

Tags:

java

mule

I have a Java method that will read some flow variables, do some aggregation and append the result to a list that already exists in another flow variable.

For this reason the method expects a MuleMessage. I thought it's obviously possible to pass a MuleMessage to a java method, using the message MEL variable like:

<invoke object-ref="Aggregator" method="aggregateSingle" methodArguments="#[message]" doc:name="Invoke"/>

But it turned out that this passes a MessageContext object instead. But I need to set InvocationVariable so this class is not useful. I know I've set variables in groovy, so perhaps this would work (I thought):

<invoke object-ref="Aggregator" method="aggregateSingle" methodArguments="#[groovy:message]" doc:name="Invoke"/>

But no, this somehow passes the payload instead of the MuleMessage.

So the only way I've found to do this is to actually call it in a Groovy component as below. Which means I have to create a new Aggregator object every time, instead of using the spring:bean as I'd planned.

                <scripting:transformer doc:name="aggregateSingle">
                    <scripting:script engine="Groovy"><![CDATA[
                        new com.example.Aggregator().aggregateSingle(message);
                        message
                    ]]></scripting:script>
                </scripting:transformer> 

Is there no way to pass a MuleMessage object to the Java method using invoke?

like image 847
rewolf Avatar asked Jul 07 '15 00:07

rewolf


1 Answers

By default mel uses message context to refer to the message variable, this in fact is just for helping out with common expressions such as message.payload != null (in previous versions of mule you would have to check for null payload). I have debugged the invoke element and unfortunately no expressions would cut it, but this is an equivalent method that you can use

<expression-transformer expression="#[groovy: Aggregator.aggregateSingle(message)]" />

This works as expected because groovy has direct access to the bean registry so you can reference the bean name and keep it as a spring bean (as I imagine you want to have)

like image 120
Juan Alberto López Cavallotti Avatar answered Oct 03 '22 00:10

Juan Alberto López Cavallotti