简体   繁体   中英

Passing MuleMessage to Java method with Mule invoke

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 ?

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)

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