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

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>

You can call Thread.sleep from a Java component, a MEL component or even a Groovy component.

However, this is tipically a design flaw unless you are testing something. If this is for production (and a delay is realy-realy-realy needed) consider other solutions like delayed messages using JMS.


In Mule 4 you should use the Runtime "wait" function. Any other alternative will block all your threads. https://docs.mulesoft.com/mule-runtime/4.1/dw-runtime-functions-wait