简体   繁体   中英

Mule ESB set payload from groovy

I have manipulated data in Groovy script. I want to output of the variable of string type to be the payload. Directly from Groovy can I set the payload? If yes how. An example would help.

You can access global variable payload to access or to change payload. Also, you can return any groovy variable it will set to payload by the processor. like

<flow name="Flow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
    <set-payload value="#[15]" doc:name="Set Payload"/>
    <scripting:component doc:name="Groovy">
        <scripting:script engine="Groovy"><![CDATA[

             int b = payload + 1;
             String c = "Result: " + b;
             return c ;

]]></scripting:script>
    </scripting:component>
    <logger message="Final result :- #[payload]" level="INFO" doc:name="Logger"/>
</flow>

Hope this helps.

To set the payload from Groovy, we can just directly assign it, eg: payload = theVariable

For testing purpose we can try the following simple flow:

<flow name="simpleFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
    <scripting:component doc:name="Groovy">
        <scripting:script engine="Groovy"><![CDATA[int a = 1;
int b = a + 1;
String c = "Result: " + b;

payload = c;]]></scripting:script>
    </scripting:component>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>

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