How to set sleep to thread in Mulesoft without losing message payload
If you are using Groovy component in your composition , then you can define sleep () like this: -
<scripting:component doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[
sleep(10000);
return message.payload;]]>
</scripting:script>
</scripting:component>
And remember in return message.payload in Groovy so you can get the payload at the end, otherwise you will get null payload
Groovy has the issue of losing payload if you don't return at the end, so in Groovy you need to return the payload at the end, and that's the reason why you are getting null payload
Alternatively, you can use component expression as shown below: -
<expression-component>
Thread.sleep(10000);
</expression-component>
source to share
You can call Thread.sleep from a Java component, a MEL component, or even a Groovy component.
However, this is typically a design flaw if you don't check anything. If this is for production (and latency is really-really-really necessary), consider other solutions such as delayed messages using JMS.
source to share
Here you can use groovy code like below.
def name = sessionVars.username;
def a = sessionVars.int1.toInteger()+1;
def b = sessionVars.int2.toInteger();
def c = a+b;
sessionVars.sum = c;
sessionVars.int1 = a;
if(name != null){
name = name
}
else{
name = '';
}
sleep(3000);
System.out.println("Holding the flow for 3000 ms");
source to share
You can use groovy code like below:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http=http://www.mulesoft.org/schema/mule/http
xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc=http://www.mulesoft.org/schema/mule/documentation
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/scripting
http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd">
<flow name="groovyFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/groovy" allowedMethods="POST" doc:name="HTTP"/>
<set-payload value="#[payload]" doc:name="Set Payload"/>
<scripting:transformer doc:name="Groovy">
<scripting:script engine="Groovy">
<![CDATA[sleep(10000);
System.out.println("Holding for 10 seconds");
return message.payload;]]></scripting:script>
</scripting:transformer>
</flow>
</mule>
source to share