How to set sleep into the flow in Mulesoft without losing the message payload

Stefano picture Stefano · Jun 29, 2015 · Viewed 15.7k times · Source

I'd like insert script to delay processing flow in Mulesoft. I have tried to insert script in groovy but I lost the messagge payload, so when I have to get message payload recived null pointer. How can I to do not lose the message payload?

Thanks

Answer

Anirban Sen Chowdhary picture Anirban Sen Chowdhary · Jun 29, 2015

If you are using Groovy component in you flow,then you can define sleep() as follow :-

<scripting:component doc:name="Groovy">
  <scripting:script engine="Groovy"><![CDATA[
    sleep(10000);
    return message.payload;]]>
  </scripting:script>
</scripting:component>

And remember to return message.payload in Groovy so that you can get the payload at the end or else you will get null payload

Groovy has an issue of loosing payload if you don't return at the end, so, in Groovy you need to return the payload at end, and that's the reason you are receiving null payload

Alternately you can use expression-component as follow:-

<expression-component>
    Thread.sleep(10000);
</expression-component>