Over a million developers have joined DZone.

Mule Expressions Using Java

DZone's Guide to

Mule Expressions Using Java

Read this short and simple tutorial in order to learn how to use Java code to perform validation for variables in the Mule flow.

· Integration Zone ·
Free Resource

How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

When we use variables in a Mule flow, we often come across the scenario for the validation. Mule provides us an expression component where we can use Java code to perform the validation that not only increases the time but also increases efficiency in the complex flows.

The component invokes the expression written, and based on the conditions, evaluates the result that could be either user-defined or a referenced one. The format involves display name, Expression, File.

Here in the flow, there are two variables defined namely: ID and Name as shown in the flow. An Expression component consists of Java Code and a logger to log the result from the request URI.

Image title

Here is the code that gives the customized information about the use of Expression component using Java Code.

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:validation="http://www.mulesoft.org/schema/mule/validation" 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"
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/validation http://www.mulesoft.org/schema/mule/validation/current/mule-validation.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="expressioninjavaFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/Test" doc:name="HTTP" allowedMethods="GET"/>
        <set-variable variableName="Name" value="#[message.inboundProperties.'http.query.params'.name]" doc:name="Name"/>
        <set-variable variableName="Id" value="#[message.inboundProperties.'http.query.params'.id]" doc:name="Id"/>
        <expression-component doc:name="Expression"><![CDATA[flowVars.checkflag='false';      

if(flowVars.id != "" &&  flowVars.name  != "" )
        <logger message="#[flowVars]" level="INFO" doc:name="Logger"/>

The conditions are checked to see whether name and ID are empty, and a flag named check flag is initially set to True. If both the conditions satisfy, we set the check flag value to true or else it will return false.

The configuration of HTTP Listener can be set as per the port availability and being set in the code.

Request URI: http://localhost:8081/Test?id=1&name=Varun 


Image title

So, it is an easier way to use Java code within the Mule Expression Component, increasing the efficiency of results.

Build and deploy API integrations 7x faster. Try the Cloud Elements 100% RESTful platform for 30 days free. Access your trial here.

mule ,java code ,tutorial

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}