Over a million developers have joined DZone.

MuleSoft: Using a Groovy Component

Looking for some time to let your code catch its breath? Adding a Groovy component to your Mule can give you the pause you're looking for.

· Integration Zone

Build APIs from SQL and NoSQL or Salesforce data sources in seconds. Read the Creating REST APIs white paper, brought to you in partnership with CA Technologies.

While working with MuleSoft, we would often like to pause process execution for while and continue later. Normally in Java, we can use the sleep() method to achieve that. But with Mule, the Groovy component has the answer.

Sample Use Case

Let's consider a flow where a file component will poll files from a particular location and will push them to specified target location. We would like to hold the file for a particular interval (say three seconds) and then push it outbound.
Image title


We can achieve that by using the Groovy component, followed by implementating the sleep method within it. 

1. First, create a Mule project followed by a flow-apstartFlow1.

2. Add and configure the file component as inbound and outbound. Add a Groovy component between the two file components:

Using groovy component

3. Add a script in the Groovy configuration:

Image title

4. Save and run the application.

5. The console log will contain the message specified in the script:

Image title

Note: I have used Mule runtime 3.8 while taking the screenshots.

So, with that, we have seen that the Groovy component is useful for implementing some custom business requirements.

The Integration Zone is brought to you in partnership with CA Technologies.  Use CA Live API Creator to quickly create complete application backends, with secure APIs and robust application logic, in an easy to use interface.


Published at DZone with permission of Kumar Gaurav. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}