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

6

6

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

Kroo answered 29/6, 2015 at 12:25 Comment(2)
Could you share what you have tried so far? Your flow including the groovy script?Cardboard
I put the component "Script", in the flow, and into window to the section General-->Script Text, I put sleep(10000)Kroo
A
19

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>
Argos answered 29/6, 2015 at 14:3 Comment(1)
This method works, although there should be a warning about the runtime deployment. While sleeping the application seems to be reluctant about undeploying itself.Patronize
B
3

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.

Burbage answered 29/6, 2015 at 13:46 Comment(0)
M
2

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

Mouser answered 30/5, 2019 at 11:47 Comment(1)
Hey where did you see in the docs that it wont block all the threads?Sitka
M
-1

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>
Mcroberts answered 2/10, 2017 at 12:30 Comment(0)
A
-2

You can use groovy code here, like below you can.

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");     
Alverta answered 5/5, 2017 at 6:57 Comment(0)
H
-3

You can make use of Groovy component to add delay.

sleep(20000)
Hypercorrect answered 9/5, 2017 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.