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

Stefano
  • 1,439
  • 4
  • 23
  • 38

6 Answers6

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>
Anirban Sen Chowdhary
  • 8,233
  • 6
  • 39
  • 81
  • This method works, although there should be a warning about the runtime deployment. While sleeping the application seems to be reluctant about undeploying itself. – Yuri Nov 23 '17 at 09:56
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.

Víctor Romero
  • 5,107
  • 2
  • 22
  • 32
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

Jorge Garcia
  • 1,313
  • 8
  • 14
-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>
-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");     
Saikumar R
  • 9
  • 1
  • 3
-3

You can make use of Groovy component to add delay.

sleep(20000)
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
veda
  • 10
  • 1
  • 4