Java Component in Mule | Execute Java Class in Mule




Java Component in Mule | Execute Java Class in Mule

Java component delegates to a java class. It is invoked by Mule when a message is received.

flow of java component :



java class is present in src/main/java named as Demo.java

package testjava;

import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;

public class Demo implements Callable {


@Override
public Object onCall(MuleEventContext eventContext) throws Exception {

String str = eventContext.getMessageAsString();
System.out.println(str);

return str;
}

}

URL : http://localhost:8085/api/java
Method : POST


Input :




Output :




XML project code :


<?xml version="1.0" encoding="UTF-8"?>

<mule 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: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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8085" doc:name="HTTP Listener Configuration" basePath="/api"/>
    <flow name="testjavaFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/java" doc:name="HTTP"/>
        <component  doc:name="Java" class="testjava.Demo">
        </component>
        <set-payload value="#[payload]" doc:name="Set Payload"/>
    </flow>
</mule>