DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Getting Started With Weaviate: Building AI-Powered Search Applications With Java
  • Getting Started With Qdrant: Building Intelligent Search Applications With Java
  • Getting Started With Milvus: Vector Search for Enterprise Java Applications
  • Pitching Kotlin: An Engineering Leader's Guide to Legacy Modernization

Trending

  • How to Submit a Post to DZone
  • Introducing TypeSafe Jev: Typed Judgments as Programming Primitives
  • Measuring AI Impact at the Team Level
  • Claude Code Superpowers: The 5-Tool Stack That Ends Limits and Context Amnesia

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. Mule Expressions Using Java

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.

By 
Varun Goyal
·
May. 12, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
19.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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"
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/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  != "" )
{
flowVars.checkflag='true';      
}
]]></expression-component>
        <logger message="#[flowVars]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>


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 

Response:    

Image title

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

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Getting Started With Weaviate: Building AI-Powered Search Applications With Java
  • Getting Started With Qdrant: Building Intelligent Search Applications With Java
  • Getting Started With Milvus: Vector Search for Enterprise Java Applications
  • Pitching Kotlin: An Engineering Leader's Guide to Legacy Modernization

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook