Groovy example in mule

Preview:

Citation preview

GROOVY EXAMPLE IN MULE

WE OFTEN WANT TO STOP OUR FLOW FOR SOMETIME AND THEN WANT TO PROCESS IT AFTER AN INTERVAL OF TIME.IN JAVA WE CAN USE SLEEP METHOD TO HOLD OUR THREAD FOR A SPECIFIC PERIOD OF TIME

BUT HOW CAN WE USE SLEEP METHOD IN MULE??

.

Here I will show you how ……

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 ???

Yes .. We can do it by using sleep method in our flow

So, we need Groovy component to implement sleep method:-

So our Mule flow will be now following :-

This Groovy component will be holding the payload for some time

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:\backup\test" 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:\backup\test\newfolder" responseTimeout="10000" doc:name="File"/> </flow>

So, if we start our application again and put the inbound file in the location E:\backup\test , we will get the following in our console :-

And we will get the file in our outbound location E:\backup\test\newfolder after 3000 ms

Hope you enjoyed the simple yet an amazing trick in Mule

Thank You

Recommended