简体   繁体   中英

Mule: prevent groovy component to overwrite payload

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>

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.

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