Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mule: prevent groovy component to overwrite payload

Tags:

java

groovy

mule

I just realized that when I define a variable in my groovy script component Mule (3.4) overwrites the message payload with this variable.

def variable = "bar";

After the flow reaches a Groovy Component with this code, the payload is changed to "bar".

How can I prevent this behaviour?

To place the Groovy Component inside an enricher seems to be a solution, but the enricher is kind of a "transport barrier" and I have to update manually every single flow variable and session variable I change in the Component. This is error prone.

complete flow code:

<flow name="test-groovyFlow" doc:name="test-groovyFlow">
    <http:inbound-endpoint exchange-pattern="request-response" host="0.0.0.0" port="8081" doc:name="HTTP" path="test_groovy"/>
    <set-payload value="foo" doc:name="payload = foo"/>
    <scripting:component doc:name="do something">
        <scripting:script engine="Groovy"><![CDATA[
           def variable = "bar";
        ]]></scripting:script>
    </scripting:component>
    <scripting:transformer doc:name="Create Response">
        <scripting:script engine="Groovy"><![CDATA[
          new java.lang.String(
               "payload: " + message.payload + "\n"
          )
        ]]></scripting:script>
    </scripting:transformer>
</flow>
like image 273
Frank Olschewski Avatar asked Feb 14 '23 00:02

Frank Olschewski


1 Answers

Add return payload or return message (depending on whether you want to let the message or the payload go through your Groovy script) as the last line of the script.

The behavior you are seeing is because the assignment result is taken as the return result for the script.

like image 192
Seba Avatar answered Feb 17 '23 03:02

Seba