java 如何在 Mulesoft 中将睡眠设置为流程而不丢失消息有效负载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31115684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set sleep into the flow in Mulesoft without losing the message payload
提问by Stefano
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?
我想插入脚本来延迟 Mulesoft 中的处理流程。我试图在 groovy 中插入脚本,但我丢失了消息有效负载,因此当我必须获取消息有效负载时,收到空指针。我怎样才能不丢失消息有效负载?
Thanks
谢谢
回答by Anirban Sen Chowdhary
If you are using Groovycomponent in you flow,then you can define sleep() as follow :-
如果您在流程中使用Groovy组件,则可以将 sleep() 定义如下:-
<scripting:component doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[
sleep(10000);
return message.payload;]]>
</scripting:script>
</scripting:component>
And remembertoreturnmessage.payload in Groovyso that you can get the payloadat the end or else you will get nullpayload
而且记得要返回在message.payload Groovy中,这样就可以得到有效载荷在年底否则你会得到空负载
Groovyhas an issue of loosing payload if you don't return at the end, so, in Groovyyou need to return the payload at end, and that's the reason you are receiving nullpayload
如果最后不返回,Groovy会丢失有效负载的问题,因此,在Groovy 中,您需要在最后返回有效负载,这就是您收到空有效负载的原因
Alternately you can use expression-componentas follow:-
或者,您可以使用表达式组件如下:-
<expression-component>
Thread.sleep(10000);
</expression-component>
回答by Víctor Romero
You can call Thread.sleep from a Java component, a MEL component or even a Groovy component.
您可以从 Java 组件、MEL 组件甚至 Groovy 组件调用 Thread.sleep。
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.
但是,除非您正在测试某些内容,否则这通常是一个设计缺陷。如果这是用于生产(并且确实需要延迟),请考虑其他解决方案,例如使用 JMS 的延迟消息。
回答by Jorge Luis Garcia Perez
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
在 Mule 4 中,您应该使用运行时“等待”功能。任何其他替代方案都会阻止您的所有线程。 https://docs.mulesoft.com/mule-runtime/4.1/dw-runtime-functions-wait
回答by Ajeeta Koundle
You can use groovy code like below:
您可以使用如下 groovy 代码:
<?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>
回答by Saikumar R
You can use groovy code here, like below you can.
您可以在此处使用 groovy 代码,如下所示。
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");
回答by veda
You can make use of Groovy component to add delay.
您可以使用 Groovy 组件来添加延迟。
sleep(20000)