This document shows how to use a Groovy scripting component to hold a Mule flow for a period of time before processing the payload. The Groovy component uses the sleep method to pause the flow for 3000 milliseconds. This holds the file payload before writing it to the outbound location. The example flow uses a file inbound endpoint to pick up a file, passes it to the Groovy component which sleeps for 3 seconds, and then writes the file to the outbound location. This allows dynamically pausing a flow to hold the payload for a specified interval of time.
5. Let us consider we have a following Mule flow :-
Now you can see in the above flow the inbound endpoint will pic
a file from a location and put it into another location.
But what if we want to hold the flow for sometime before it put
the file in the outbound location ???
How can we hold the file for an particular interval of time ???
6. Yes .. We can do it by using sleep method in our flow
7. So, we need Groovy component to implement sleep method:-
8. So our Mule flow will be now following :-
This Groovy component will be holding the payload for some time
9. The corresponding Mule flow will be :-
<file:connector name="File_Global" autoDelete="true" outputAppend="true" streaming="true"
validateConnections="true" doc:name="File"/>
<flow name="Flow1" doc:name="Flow1">
<file:inbound-endpoint path="E:backuptest" responseTimeout="10000" doc:name="File"
connector-ref="File_Global">
<file:filename-regex-filter pattern="abc.txt" caseSensitive="false"/>
</file:inbound-endpoint>
<scripting:component doc:name="Groovy">
<scripting:script engine="Groovy">
<![CDATA[
sleep(3000);
System.out.println("Holding the flow for 3000 ms");
return message.payload;]]>
</scripting:script>
</scripting:component>
<file:outbound-endpoint path="E:backuptestnewfolder" responseTimeout="10000"
doc:name="File"/>
</flow>
10. So, if we start our application again and put the inbound file in
the location E:backuptest , we will get the following in our
console :-
11. And we will get the file in our outbound location
E:backuptestnewfolder after 3000 ms