Integration & IT Modernization

How to Use Groovy in a Mule Application

Mule ESB has been arguably the most popular and efficient open source Enterprise Service Bus (ESB) available in the market and its rich options of connectors, routers, filters, error handlers, transformers supporting multiple languages like java, JavaScript, groovy, python and ruby has elevated it to the indisputable ESB of the digital era. In September 2016, Forbes released its first ever top 100 private cloud companies in the world and Mulesoft was ranked 20th in the report. Mulesoft was the only company to be featured in both magic quadrants for Application Services Governance and Enterprise Integration Platform as a Service (iPaaS) in a Gartner report released in April 2015 and March 2016 respectively. This highlights Mulesoft’s rapid growth and its stronghold in integration division, helping organisations for the digital transformation.

Apache Groovy is a powerful object oriented programming language for Java platform. Its simplicity and easy to learn syntax had made it the preferred scripting language for developers. We will implement groovy script in a Mule flow to demonstrate the smooth integration of groovy script in a Mule application.


  1. Basic working knowledge on Mule ESB development using Anypoint Studio.
  2. Basic knowledge on any object-oriented programming language is an added advantage.

In this simple example, we will build a Mule application with an HTTP listener which will be inputted with the below query parameters:

  • Name
  • Parameter1
  • Parameter2

The Mule application will be configured with a groovy component which will print the sum of parameter 1 and parameter 2 as the response.

Mule Flow:

Mule flow Groovy

HTTP Inbound Endpoint:

An HTTP inbound endpoint is created by dragging the HTTP connector component from the palette to the canvas. An HTTP connector configuration is created which will listen to the localhost on port 8081 and the path in which the resource can be accessed will be “groovy”.

http inbound endpoint

Session Variables:

We will have 3 session variable components to extract the query parameters from the input url and save it to the session variables configured in the flow.

Mule Session Variable

Mule Session variable

Covid 19
COVID-19: Digital Insights For Enterprise Action

Access Perficient’s latest insights into how you can leverage digital technologies to not only respond to the pandemic, but drive your operations forward and deliver experiences your customers need.

Get Informed

Mule session variable


A groovy component is placed next to the session variable components in which we sum up the values of parameters passed.

Mule groovy

Note: We need to use the toInteger() function to convert the string into integer. When the query parameter is assigned to the session variable its data type is string. In order to convert the string to integer we use the toInterger() function.


We will use a logger to log the values of the session variables in the console.

Mule logger

Set Payload:

We will place a Set Payload component to complete the Mule flow. The Set Payload component will print the message in the following format as output.

Hello “Name” !!! The sum of “Parameter1” and “Parameter2” is “Sum” !!!

Mule set payload

Testing the Mule flow:

It’s time to test the Mule application we have built. We can use any browser or tools such as SOAP UI or the Postman Chrome plug-in which is capable of posting a HTTP request. Using the chrome browser, try to post the following: localhost:8081/groovy?name=Karthik&parameter1=4&parameter2=65

Mule testing

Mule logger-output


About the Author

More from this Author

Thoughts on “How to Use Groovy in a Mule Application”

  1. Karthik
    Many thanks for this, as it finally answered my search for how to work with Groovy scripts.
    There was just one problem… “sessionVars.parameter1” returned null.
    I eventually discovered two solutions:
    1. Using your code, add: import org.mule.api.transport.*;
    2. Use: def x = sessionVars[‘parameter1’].toInteger(); (without the ‘import’)

    What I am really hoping for is to be able to defne Groovy with code assist, as for Java, and to then test it using JUnit. The only reason for using Groovy is for a complex operation. In which case, revertng to ‘coding using Notepad’ is like going back to the Ark.
    Anyway, thanks for giving me a stat. I will now go in search of my goal.
    Regards, John

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up