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

主宰稳场 提交于 2019-12-07 03:06:17

问题


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


回答1:


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>



回答2:


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.




回答3:


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




回答4:


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>



回答5:


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");     



回答6:


You can make use of Groovy component to add delay.

sleep(20000)


来源:https://stackoverflow.com/questions/31115684/how-to-set-sleep-into-the-flow-in-mulesoft-without-losing-the-message-payload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!