Mulesoft
Groovy Component Reference
https://docs.mulesoft.com/mule-user-guide/v/3.8/groovy-component-reference
Kumar Gaurav
k10gaurav@gmail.com
Agenda
• About
• Sample Use Case
• Demonstration
• Testing
• Conclusion
About
While working with MuleSoft, often we would like to pause
the process execution for while and then would continue.
Normally in Java, we can use the sleep(), for achieving the
same that can pause the thread execution for particular
time interval and then continue normal.
With mule, there is also a certain way of achieving the
same through Groovy Component.
Sample use case
Let’s consider a flow where file component as inbound will poll files from a
particular location and will push in to specified target location.
Now we would like to hold the file for a particular interval say 3 seconds and
then push it to target outbound.
Continue…
We can achieve the same by using Groovy Component, followed by
implementation of sleep method in it.
Demonstration
We need to write script inside Groovy component
Continue…
Corresponding mule XML configuration file would have below piece of code
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:http="http://www.mulesoft.org/schema/mule/http" 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/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<file:connector name="File" writeToDirectory="D:Mule3.8testmove" autoDelete="true" streaming="true"
validateConnections="true" doc:name="File" readFromDirectory="D:Mule3.8testoutput"/>
<flow name="apstart1Flow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/apstart" allowedMethods="get" doc:name="HTTP"/>
<set-payload value="'hello'#[message.inboundProperties.'http.query.params'.firstname]" doc:name="Set Payload"/>
</flow>
<flow name="apstart1Flow1">
<file:inbound-endpoint path="D:Mule3.8testinput" moveToDirectory="D:Mule3.8testoutput" responseTimeout="10000"
doc:name="File"/>
<scripting:component doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[sleep(300);
System.out.println("Holding for 3 secs");
return message.payload;]]></scripting:script>
</scripting:component>
<file:outbound-endpoint path="D:Mule3.8testoutput" connector-ref="File" responseTimeout="10000" doc:name="File"/>
</flow>
</mule>
Testing
By running the application, console would show below log and would move file
after 3 seconds
Holding for 3 secs
INFO 2016-07-17 16:28:36,224 [[apstart1].File.dispatcher.02] org.mule.transport.file.FileConnector: Writing file to:
D:Mule3.8testmovegroovy_mule - Copy.docx
INFO 2016-07-17 16:28:36,234 [[apstart1].File.dispatcher.02] org.mule.transport.file.FileConnector: Writing file to:
D:Mule3.8testmovegroovy_mule.docx
INFO 2016-07-17 16:28:36,904 [[apstart1].File.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file:
D:Mule3.8testoutputBPM Skills in 2016 - which are hot, which are not _ BPM tips - Copy.pdf
INFO 2016-07-17 16:28:36,904 [[apstart1].File.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file:
D:Mule3.8testoutputBPM Skills in 2016 - which are hot, which are not _ BPM tips.pdf
INFO 2016-07-17 16:28:36,904 [[apstart1].File.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file:
D:Mule3.8testoutputgroovy_mule - Copy.docx
INFO 2016-07-17 16:28:36,904 [[apstart1].File.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file:
D:Mule3.8testoutputgroovy_mule.docx
Holding for 3 secs
Holding for 3 secs
INFO 2016-07-17 16:28:37,205 [[apstart1].File.dispatcher.02] org.mule.transport.file.FileConnector: Writing file to:
D:Mule3.8testmovegroovy_mule.docx
Holding for 3 secs………..
Continue…
The file will be moved to target location specified.
Conclusion
So by this way we have seen that Groovy component is
very much useful for developers for implementing some
custom business requirements.
Thank you!

Mulesoft Using Groovy Component

  • 1.
  • 2.
    Agenda • About • SampleUse Case • Demonstration • Testing • Conclusion
  • 3.
    About While working withMuleSoft, often we would like to pause the process execution for while and then would continue. Normally in Java, we can use the sleep(), for achieving the same that can pause the thread execution for particular time interval and then continue normal. With mule, there is also a certain way of achieving the same through Groovy Component.
  • 4.
    Sample use case Let’sconsider a flow where file component as inbound will poll files from a particular location and will push in to specified target location. Now we would like to hold the file for a particular interval say 3 seconds and then push it to target outbound.
  • 5.
    Continue… We can achievethe same by using Groovy Component, followed by implementation of sleep method in it.
  • 6.
    Demonstration We need towrite script inside Groovy component
  • 7.
    Continue… Corresponding mule XMLconfiguration file would have below piece of code <?xml version="1.0" encoding="UTF-8"?> <mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:http="http://www.mulesoft.org/schema/mule/http" 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/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd"> <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/> <file:connector name="File" writeToDirectory="D:Mule3.8testmove" autoDelete="true" streaming="true" validateConnections="true" doc:name="File" readFromDirectory="D:Mule3.8testoutput"/> <flow name="apstart1Flow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/apstart" allowedMethods="get" doc:name="HTTP"/> <set-payload value="'hello'#[message.inboundProperties.'http.query.params'.firstname]" doc:name="Set Payload"/> </flow> <flow name="apstart1Flow1"> <file:inbound-endpoint path="D:Mule3.8testinput" moveToDirectory="D:Mule3.8testoutput" responseTimeout="10000" doc:name="File"/> <scripting:component doc:name="Groovy"> <scripting:script engine="Groovy"><![CDATA[sleep(300); System.out.println("Holding for 3 secs"); return message.payload;]]></scripting:script> </scripting:component> <file:outbound-endpoint path="D:Mule3.8testoutput" connector-ref="File" responseTimeout="10000" doc:name="File"/> </flow> </mule>
  • 8.
    Testing By running theapplication, console would show below log and would move file after 3 seconds Holding for 3 secs INFO 2016-07-17 16:28:36,224 [[apstart1].File.dispatcher.02] org.mule.transport.file.FileConnector: Writing file to: D:Mule3.8testmovegroovy_mule - Copy.docx INFO 2016-07-17 16:28:36,234 [[apstart1].File.dispatcher.02] org.mule.transport.file.FileConnector: Writing file to: D:Mule3.8testmovegroovy_mule.docx INFO 2016-07-17 16:28:36,904 [[apstart1].File.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file: D:Mule3.8testoutputBPM Skills in 2016 - which are hot, which are not _ BPM tips - Copy.pdf INFO 2016-07-17 16:28:36,904 [[apstart1].File.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file: D:Mule3.8testoutputBPM Skills in 2016 - which are hot, which are not _ BPM tips.pdf INFO 2016-07-17 16:28:36,904 [[apstart1].File.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file: D:Mule3.8testoutputgroovy_mule - Copy.docx INFO 2016-07-17 16:28:36,904 [[apstart1].File.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file: D:Mule3.8testoutputgroovy_mule.docx Holding for 3 secs Holding for 3 secs INFO 2016-07-17 16:28:37,205 [[apstart1].File.dispatcher.02] org.mule.transport.file.FileConnector: Writing file to: D:Mule3.8testmovegroovy_mule.docx Holding for 3 secs………..
  • 9.
    Continue… The file willbe moved to target location specified.
  • 10.
    Conclusion So by thisway we have seen that Groovy component is very much useful for developers for implementing some custom business requirements.
  • 11.