Mule 4  - Simpler Way to Invoke a method from the Java class using Transform Message || Java Module

Mule 4 - Simpler Way to Invoke a method from the Java class using Transform Message || Java Module

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)


No alt text provided for this image

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

No alt text provided for this image

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.

No alt text provided for this image
No alt text provided for this image

Now pass the arguments as required in the "Args" field.

Note:
Args key names must match to that of Java method arguments
No alt text provided for this image

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.

No alt text provided for this image

Finally Output :

No alt text provided for this image

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)

}
No alt text provided for this image

You'll get final results accordingly.

No alt text provided for this image



In this way, we can invoke Java classes whenever we are not able to achieve particular functionality using Mule.

Happy Learning,
Yours
Sravan Lingam!







Lawrence S.

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?

回复
Suchismita Maity

Senior Consultant at Deloitte USI

4 年

Very useful!

Nitish Jain

MuleSoft Ambassador || MuleSoft Certified Developer/Architect || MuleSoft Meetup Leader || Blog Writer || Ex-MuleSoft Mentor || Ex- IBMer || Ex-Accenturite

4 年

Thanks for posting

Ketan Fulkar

| 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

要查看或添加评论,请登录

社区洞察

其他会员也浏览了