Mulesoft Script Components

The scripting modules facilitate users to use scripting language in Mule. In simple words, the scripting module can exchange custom logic written in scripting language. Scripts can be used as Implementations or transformers. They can be used for expression evaluation, i.e., for controlling message routing.

Mule has the following supported scripting languages −

  • Groovy
  • Python
  • JavaScript
  • Ruby

How to Install Scripting Modules?

Actually, Anypoint Studio comes with the scripting modules. If you do not find the module in Mule Palette, then it can be added by using +Add Module. After adding, we can use the scripting module operations in our Mule application.

 

Interested in learning the Mulesoft Course ? Enroll in our Mulesoft raining program now!

Implementing Exampl

As discussed, we need to drag and drop the module into canvas for creating workspace and use it in our application. Following is an example of it −

Install Scripting Modules

We already know how to configure the HTTP Listener component; hence we are going to discuss about configuring the Scripting Modules. We need to follow the steps written below to configure scripting module −

Step 1

Search for the Scripting module from Mule Palette and drag the EXECUTE operation of the scripting module into your flow as shown above.

Step 2

Now, open the Execute configuration tab by double clicking on the same.

Step 3

Under the General tab, we need to provide the code in the Code text window as shown below −

Code Text Window

Step 4

At last, we need to choose the Engine from the execute component. The list of engines is as below −

  • Groovy
  • Nashorn(javaScript)
  • jython(Python)
  • jRuby(Ruby)

The XML of the above execution example in the Configuration XML editor is as follows −

<scripting:execute engine="jython" doc:name = "Script">
   <scripting:code>
      def factorial(n):
         if n == 0: return 1
      return n * factorial(n-1)
      result = factorial(10)
   </scripting:code>
</scripting:execute>

Message Sources

Mule 4 has a simplified model than Mule 3 message making it easier to work with data in a consistent way across connectors without overwriting information. In Mule 4 message model, each Mule event consists of two things: a message and variables associated with it.

A Mule message is having payload and its attributes, where attribute is mainly metadata such as file size.

And a variable holds the arbitrary user information such as operation result, auxiliary values, etc.

Inbound

The inbound properties in Mule 3 now becomes Attributes in Mule 4. As we know that inbound properties store additional information about the payload obtained through a message source, but this is now, in Mule 4, done with the help of attributes. Attributes have the following advantages −

  • With the help of attributes, we can easily see which data is available, because attributes are strongly typed.

  • We can easily access information contained in attributes.

Following is the example of a typical message in Mule 4 −

Inbound

Outbound

The outbound properties in Mule 3 must be explicitly specified by Mule connectors and transports in order to send additional data. But in Mule 4, each of those can be set separately, using a DataWeave expression for each one of them. It does not produce any side effect in the main flow.

For example, below DataWeave expression will perform a HTTP request and generates headers and query parameters without a need to set message properties. This is shown in the below code −

<http:request path = "M_issue" config-ref="http" method = "GET">
   <http:headers>#[{'path':'input/issues-list.json'}]</http:headers>
   <http:query-params>#[{'provider':'memory-provider'}]</http:query-params>
</http:request>

Message Processor

Once Mule receives a message from a message source, the work of message processor starts. The Mule uses one or more message processors to process the message through a flow. The main task of message processor is to transform, filter, enrich and process the message as it passes through the Mule flow.

Categorization of Mule Processor

Following are the categories of Mule Processor, based on functions −

  • Connectors − These message processors send and receive data. They also plug data into external data sources via standard protocols or third-party APIs.

  • Components − These message processors are flexible in nature and perform business logic implemented in various languages like Java, JavaScript, Groovy, Python or Ruby.

  • Filters − They filter the messages and allow only specific messages to continue to be processed in a flow, based on specific criteria.

  • Routers − This message processor is used to control the flow of message to route, resequencing or split.

  • Scopes − hey basically wrap snippets of code for the purpose of defining fine-grained behavior within a flow.

  • Transformers − The role of transformers is to convert message payload type and data format to facilitate communication between systems.

  • Business Events − They basically capture data associated with key performance indicators.

  • Exception strategies − These message processors handle errors of any type that occur during message processing.