Mule 4 - Simpler Way to Invoke a method from the Java class using Transform Message || Java Module
Sravan Lingam
MuleSoft Ambassador ???? Solution Architect ?? Mulesoft Meetup Leader ??All Certified MuleSoft Dev & Architect | 3 times MuleSoft All Star ??Award winner ???Owner of MuleSoft TechZone
Hello Muleys,
Mule 4 made many things simpler, one of them is Invoking java methods with help of "Java" Module or Transform Message component.
First Let's create a Java class where we can have multiple methods written. Create a Java class under the src/main/java folder of your project.
Always use proper naming conventions for your package name and Java class name. If you don't create a package under src/main/resources, A default package will be created.
Below is the Java class that I have created for this article.
Package name: SampleJava
Class name : Mathematics.java
Methods :
- add(int firstNum,int secondNum) ,
- subtract(int firstNum,int secondNum)
- multiply(int firstNum,int secondNum)
- divide(int firstNum,int secondNum)
Let's see how we can invoke add method in 2 ways:
Using Java Module:
To invoke a method of a java class, first, we need to Add Java Module by clicking "Add Modules" in Mule pallet
Next Drag and drop Invoke static component
It's always best practice to search java class so that all things are automatically populated. Even the methods of that particular java class will also be populated.
Now pass the arguments as required in the "Args" field.
Note:
Args key names must match to that of Java method arguments
If the Args names are mismatched, then it will throw an error.
Now we are all good, the result of the Invoke static will be saved as payload. You can also use "target Variable" to store the output of Java class.
Finally Output :
Note:
You can only invoke one method at a time.
What if you have a requirement where you want to get result of all operations. i.e, all methods like add(),subtract(),multiply(),divide() etc?
Then we can go for a 2nd approach by calling methods using Transform Message which is a simpler way.
Using Transform Message Component:
Very simple, mention the correct package name, class name, and method names as below.
No need to mention arg key names, we can directly pass values but make sure that Data types are matching!
%dw 2.0 import java!SampleJava::Mathematics output application/json --- { addResult: Mathematics::add(60,20), subtractResult: Mathematics::subtract(60,20), multiplyResult: Mathematics::multiply(60,20), divisionResult: Mathematics::divide(60,20)
}
You'll get final results accordingly.
In this way, we can invoke Java classes whenever we are not able to achieve particular functionality using Mule.
Happy Learning,
Yours
Sravan Lingam!
Cloud Software Engineer (Secret Clearance) / Investor / Dog walker
2 个月for passing Json response from REST call to java from mulesoft flow, I have a call,? <java:new doc:name="new medicationService" doc:id="555421aa-b46d-4484-988a-488fc6c07de3" class="ca.phsa.transform.MedicationService" constructor="MedicationService()" target="medicationService"/> <java:invoke doc:name="set drugs" doc:id="fb86a9db-dfb3-4e98-8e3c-7be580bc8239" instance="#[vars.medicationService]" class="ca.phsa.transform.MedicationService" method="setMedicationJson(InputStream)" outputMimeType="application/json; streaming=true"> <java:args ><![CDATA[{ medicationJson : vars.drugs}]]></java:args> </java:invoke> the json payload is ~ 1 MB i get following error?message "The value '{ medicationJson : vars.drugs}' of type java.lang.String could not be transformed to the desired type java.util.Map [Not accessible. Value obtained from method: getDetailedDescription()]" what am i doing wrong?
Senior Consultant at Deloitte USI
4 年Very useful!
MuleSoft Ambassador || MuleSoft Certified Developer/Architect || MuleSoft Meetup Leader || Blog Writer || Ex-MuleSoft Mentor || Ex- IBMer || Ex-Accenturite
4 年Thanks for posting
| Mulesoft Lead at Wipro Technologies|Mulesoft Mentor | Mulesoft Meetup Speaker | Mulesoft Certified Integration Architect | 4x Mulesoft| Integration Developer | 8+ Years Experienced |Freelancer|
4 年Awesome stuff in simple language ???? Sravan Lingam