Calling Custom Java Function in Mule 4
In this tutorial we will learn how we can invoke custom java functions in mule 4. The ways by which Java functions can be called are: |
Using Java Components:
Various Java Component to call custom java class:
First we will create a java class that will be invoked in mule code. package com.mulejava; public class StringManipulation { public String result; public StringManipulation() { // TODO Auto-generated constructor stub } public void ConcatAB(String value1, String value2) { result = value1 + value2; } public static String staticFunctionTest() { return "static funtion invoked"; } }
[IMP] Then, we need to add the class package name in “mule-artifact.json” inside classLoaderModelLoaderDescriptor, so mule can identify class else “CLASS NOT FOUND” would be through while call the function. { "configs": [ "test.xml" ], "secureProperties": [], "redeploymentEnabled": true, "name": "mulejava", "minMuleVersion": "4.1.1", "requiredProduct": "MULE_EE", "classLoaderModelLoaderDescriptor": { "id": "mule", "attributes": { "exportedResources": [ "api\\test.raml" ], "exportedPackages":[ "com.mulejava" ] } }, "bundleDescriptorLoader": { "id": "mule", "attributes": {} } } |
Calling non-static function:
To call a non-static function we need to first create an instance of the class. We can do that by using “New” java component from mule palette.
Now we will use Invoke java component to call the ConcatAB function. <java:invoke doc:name="Invoke" doc:id="d864445c-6ce8-4b92-ada2-0ddbc7215ede" class="com.mulejava.StringManipulation" method="ConcatAB(String, String)" instance="#[vars.inst]"> <java:args ><![CDATA[#[{ arg0: payload.a as String, arg1: payload.b as String }]]]></java:args> </java:invoke>
|
Calling static function:
To call static function from the call we all need to use “Invoke Static” component from mule palette.
|
Calling Java Function in DW:
Only static variable can be call in DW: <ee:transform doc:name="Transform Message" doc:id="4002c2b2-0cd5-438c-87ff-500b3b999752" > <ee:message > <ee:set-payload ><![CDATA[%dw 2.0 import java!com::mulejava::StringManipulation output application/json --- { a: StringManipulation::ConcatAAB(payload.a as String, payload.b as String) }]]> </ee:set-payload> </ee:message> </ee:transform>
|
Calling Java Function in Set Payload:
First create instance to the java class that is to be invoked as we did above by using “New” java component from mule Palette. Then inside set payload use Java invoke mule function to call the respective function by passing in:
<java:new class="com.mulejava.StringManipulation" target="inst" doc:name="Create Instance" constructor="StringManipulation()"/> <set-payload value="#[Java::invoke('com.mulejava.StringManipulation', 'ConcatAB(String, String)', vars.inst, {arg0: "tutorials", arg1: "AtoZ"})]" doc:name="Set Payload" doc:id="d0d8f6fb-f406-4783-9ca3-24e7b5461d61" />
|
Wonderful description of the Mule 4 concepts, Thanks for such a good collection.
Thanks Santosh 🙂
Varun Goel,thank you ever so for you post.Much thanks again.