Mulesoft Java Connector Invoke Example

Share

Mulesoft Java Connector Invoke Example

To invoke a Java method using the MuleSoft Java Connector, you can create a custom Java class with the desired method and then call that method from a MuleSoft flow using the Java Connector. Here’s a step-by-step example:

Step 1: Create a Java Class

Create a Java class with the method you want to invoke. Let’s say you want to create a simple class that performs a calculation. Here’s an example:

java
package com.example; public class Calculator { public int add(int a, int b) { return a + b; } }

Step 2: Create a MuleSoft Project

In Anypoint Studio, create a new Mule project or open an existing one.

Step 3: Add Java Class to Your Project

Add the Java class you created in Step 1 to your Mule project. Place it in the appropriate package within your project’s src/main/java directory.

Step 4: Configure the Java Connector

In your Mule project’s XML configuration file (usually mule-config.xml), configure the Java Connector to use your custom Java class. Here’s an example:

xml
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:java="http://www.mulesoft.org/schema/mule/java" 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/java http://www.mulesoft.org/schema/mule/java/current/mule-java.xsd"> <!-- Configure the Java Connector to use your custom Java class --> <java:config name="Java_Config" doc:name="Java" class="com.example.Calculator"/> <!-- Define your Mule flow --> <flow name="invokeJavaMethodFlow"> <!-- Invoke the 'add' method from the Java class --> <java:invoke config-ref="Java_Config" method="add" doc:name="Invoke" > <java:args> <java:arg value="3" /> <java:arg value="4" /> </java:args> </java:invoke> <!-- Log the result --> <logger message="Result: #[payload]" level="INFO" doc:name="Logger"/> </flow> </mule>

In the above configuration, we configure the Java Connector to use the com.example.Calculator class. Then, in the invokeJavaMethodFlow, we invoke the add method from the Calculator class and log the result.

Step 5: Run the Mule Application

Save your Mule configuration and run the Mule application in Anypoint Studio. You should see the result of invoking the Java method in the log.

Demo Day 1 Video:

 
You can find more information about Mulesoft in this Mulesoft Docs Link

 

Conclusion:

Unogeeks is the No.1 Training Institute for Mulesoft Training. Anyone Disagree? Please drop in a comment

You can check out our other latest blogs on Mulesoft Training here – Mulesoft Blogs

You can check out our Best in Class Mulesoft Training details here – Mulesoft Training

 

💬 Follow & Connect with us:

———————————-

For Training inquiries:

Call/Whatsapp: +91 73960 33555

Mail us at: info@unogeeks.com

Our Website ➜ https://unogeeks.com

Follow us:

Instagram: https://www.instagram.com/unogeeks

Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute

Twitter: https://twitter.com/unogeeks


Share

Leave a Reply

Your email address will not be published. Required fields are marked *