Invoke in Mule 4 | Handling Java in Mule 4
Java Module in Mule 4 is to handle java code in mule.
1. New component is used to create instance of class.
2. Invoke component is used to invoke a java method of
a class. So before Invoke we have to use New component to create instance of
the class.
4. Invoke static is used to invoke a static method of
a class.
5. It is used to validate that given instance is
instance of class.
Flow:
New component configuration:
Advanced setting:
Invoke component settings:
Validate Type settings:
Java class located in src/main/java:
Flow of invoke static:
Static invoke component configuration:
Method: GET
Output:
Static invoke URL: http://localhost:8085/static
Method: GET
Output:
XML project code:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:java="http://www.mulesoft.org/schema/mule/java"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="b9b13759-df20-4d08-89d1-3b26e3b35294" >
<http:listener-connection host="0.0.0.0" port="8085" />
</http:listener-config>
<flow name="test_javaFlow" doc:id="231601ea-098e-4a3e-bc79-ede33f33af53" >
<http:listener doc:name="Listener" doc:id="a822823b-d8cb-40bf-af45-672d140ed582" config-ref="HTTP_Listener_config" path="/invoke"/>
<java:new constructor="Test()" doc:name="New" doc:id="4cb81f7c-3d50-4343-9a5d-3383f448d33d" class="test_java.Test" target="test"/>
<java:invoke doc:name="Invoke" doc:id="bdcd5fcd-cd43-4948-bdf2-2b3f816ca893" class="test_java.Test" method="a()" instance="#[vars.test]"/>
<java:validate-type doc:name="Validate type" doc:id="b162a9e6-e549-48a3-b9b9-8a2f938a8c29" class="test_java.Test" instance="#[vars.test]"/>
<set-payload value="#[payload]" doc:name="Set Payload" doc:id="81bd6430-f64a-4b7d-b45c-095a9ab43e24" />
</flow>
<flow name="test_javaFlow1" doc:id="48fd0e80-8507-4490-a7e2-dae441188936" >
<http:listener doc:name="Listener" doc:id="43eb7c2a-9656-438d-9db2-8147f17454b9" config-ref="HTTP_Listener_config" path="/static"/>
<java:invoke-static doc:name="Invoke static" doc:id="6686f6dc-487d-44a0-9f83-6b6ac3645117" class="test_java.Test" method="b()"/>
</flow>
</mule>
No comments:
Post a Comment