In this tutorial, I will show you how to invoke method of an object using Invoke Component in Mule ESB.
First, I will create a new project as an example:
Now, I will create a new HelloWorld object in the com.huongdanjava.muleesbinvokecomponent package.
With the following content:
1 2 3 4 5 6 7 8 |
package com.huongdanjava.muleesbinvokecomponent; public class HelloWorld { public String hello(String name) { return String.format("Hello %s", name); } } |
In order to the Invoke Component can call the hello() method in the HelloWorld class, we need to declare the object of this class as a Spring bean in Spring’s container.
So, just open the mule-esb-invoke-component.xml file and declare the HelloWorld object in the Spring container as follows:
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="UTF-8"?> <mule 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" version="EE-3.8.4" 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"> <spring:beans> <spring:bean class="com.huongdanjava.muleesbinvokecomponent.HelloWorld" name="invoke" /> </spring:beans> </mule> |
Now, we will create a new flow to see how the Invoke Component works.
Our flow will include the HTTP Listener Connector and the Invoke Component as follows:
Inside:
- The HTTP Listener Connector is configured as follows:
General Settings:
If you do not know how to configure the HTTP Listener Connector, you can refer to this tutorial!
- For the Invoke Component, we configure it as follows:
Here:
– Name is the name of Endpoint.
– Object Ref is the id of the object in the Spring’s container. In our example, our HelloWorld object has been declared with id as invoke in Spring’s container.
– Method is the name of the method we want to call. My example is the hello() method.
– Method Arguments is the value of the parameters we will pass to the method. If there are multiple parameters, each value will be separated by a comma. In my example, because the hello() method has only one parameter then I declared only one value. And this value is derived from the request’s parameter to the HTTP Listener Connector.
OK, so we have completed a simple flow using the Invoke Component.
Try running it now.
http://localhost:8081/hello?name=Khanh
Result:
Tham khảo project trên GitHub tại: https://github.com/huongdanjavacom/huongdanjava.com/tree/master/mule-esb-invoke-component