MuleSoft is a lightweight integration and API platform that allows you to connect anything anywhere and enable your data through API. Mule evolved from java and spring framework. MuleSoft supports multiple language although all Mule module are developed in java.
Since Mule evolved from java it has capability to use direct java class and method in Mule flow. This capability gives flexibility to Mule developer to use java for complex business logic.
There are several ways you can use java within Mule. Here are some of Java modules available to use within MuleSoft application
There are 4 java modules are available in MuleSoft flow
- New
- Invoke
- Invoke static
- Validate type

To explain all these components and uses in Mule flow I created Utils.java and AppUtils.java classes
1. New – AppUtils.java class instantiation can be achieved by calling constructor of this class through MuleSoft New component within Mule flow.AppUtils java class defined 2 contractors, So Mule constructor properties for NEW component is showing 2 options.
New module without parameter
<java:new doc:name="Instantiate appUtils" doc:id="22ddcb7e-82ed-40f8-bc11-b779ceedd1a1"
constructor="AppUtils()" class="com.vanrish.AppUtils" target="appInst">
</java:new>
New module with parameter
<java:new doc:name="Instantiate appUtils" doc:id="22ddcb7e-82ed-40f8-bc11-b779ceedd1a1"
constructor="AppUtils(String)" class="com.vanrish.AppUtils" target="appInst">
<java:args ><![CDATA[#[{paramVal:"Hello world}]]]>
</java:new>
In above code, Instance of AppUtils class is created and placed into the “appInst” target variables to reuse same instance in Mule flow.

<java:invoke doc:name="Invoke" doc:id="9348e2cf-87fe-4ff7-958c-f430d0421702"
instance="#[vars.appInst]" class="com.vanrish.AppUtils" method="generateRandomNumber(String)">
<java:args ><![CDATA[#[{numVal:”100”}]]]></java:args>
</java:invoke>

Mule code is calling to java static method
<java:invoke-static doc:name="Invoke static" doc:id="bc3e110c-d970-47ef-891e-93fb3ffb61bd"
class="com.vanrish.AppUtils" method="encode(String)">
<java:args ><![CDATA[#[{plainString:"mystringval"}]]]></java:args>
</java:invoke-static>

<java:validate-type doc:name="Validate type" doc:id="288c791c-50eb-4be0-b924-56481dfdc023"
class="com.vanrish.Utils" instance="#[vars.appInst]" acceptSubtypes="false"/>

Java in Mule flow diagram

Utils.java
package com.vanrish;
public class Utils{
}
AppUtils.java
package com.vanrish;
import java.util.Random;
import sun.misc.BASE64Encoder;
/**
* @author rajnish
*/
public class AppUtils extends Utils {
public static final BASE64Encoder encoder = new BASE64Encoder();
//Constructor without Parameter
public AppUtils(){
System.out.println("Constructor with no parameter");
}
//Constructor with Parameter
public AppUtils(String paramVal){
System.out.println("Constructor with parameter value="+paramVal);
}
/**
* @param String
* @return
*/
public String generateRandomNumber(String numVal) {
Integer numNoRange = null;
Random rand = new Random();
if(numVal !=null) {
numNoRange = rand.nextInt(new Integer(numVal));
}else {
numNoRange = rand.nextInt();
}
return numNoRange.toString();
}
/**
* @param plainString
* @return
*/
public static String encode(String plainString)
{
String encodedString = encoder.encodeBuffer(plainString.getBytes());
return encodedString;
}
}
Mulesoft Code
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:java="http://www.mulesoft.org/schema/mule/java" xmlns:db="http://www.mulesoft.org/schema/mule/db"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" 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:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/hl7 http://www.mulesoft.org/schema/mule/hl7/current/mule-hl7.xsd
http://www.mulesoft.org/schema/mule/db
http://www.mulesoft.org/schema/mule/db/current/mule-db.xsdhttp://www.mulesoft.org/schema/mule/java http://www.mulesoft.org/schema/mule/java/current/mule-java.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="af3ce281-bf68-4c7b-83fb-52b2d2506677" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="helloworldFlow" doc:id="a13826dc-67a1-4cda-8133-bc16a59ddba2" >
<http:listener doc:name="Listener" doc:id="82522c77-5c33-4003-820a-7a04b51c3001" config-ref="HTTP_Listener_config" path="helloworld"/>
<logger level="INFO" doc:name="Logger" doc:id="b40bc107-1ab5-4a3f-8f20-d7dfb63e5acb" message="entering flow"/>
<java:new doc:name="Instantiate appUtils" doc:id="c1854580-f4d4-4e5c-a34d-7ca185152d02"
constructor="AppUtils(String)" class="com.vanrish.AppUtils" target="appInst"
>
<java:args ><!
[CDATA[#[{
paramVal:"Hello world"
}]]]></java:args>
</java:new>
<java:invoke doc:name="Invoke" doc:id="9348e2cf-87fe-4ff7-958c-f430d0421702" instance="#[vars.appInst]" class="com.vanrish.AppUtils" method="generateRandomNumber(String)">
<java:args ><![CDATA[#[{
numVal:null
}]]]></java:args>
</java:invoke>
<java:invoke-static doc:name="Invoke static" doc:id="bc3e110c-d970-47ef-891e-93fb3ffb61bd" class="com.vanrish.AppUtils" method="encode(String)">
<java:args ><![CDATA[#[{
plainString:"mystringval"
}]]]></java:args>
</java:invoke-static>
<java:validate-type doc:name="Validate type" doc:id="288c791c-50eb-4be0-b924-56481dfdc023" class="com.vanrish.Utils" instance="#[vars.appInst]"/>
<set-payload value="Success" doc:name="Set Payload" doc:id="8b6c9c0b-07c8-4e17-b649-2c24d4da8bea" />
</flow>
</mule>

Rajnish Kumar is CTO of Vanrish Technology with Over 25 years experience in different industries and technology. He is very passionate about innovation and latest technology like APIs, IOT (Internet Of Things), Artificial Intelligence (AI) ecosystem and Cybersecurity. He present his idea in different platforms and help customer to their digital transformation journey.