Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Invoke Java Method in Mule Flow

DZone's Guide to

Invoke Java Method in Mule Flow

By using the Invoke component, we can invoke a specified method of an object defined in a Spring Bean. Mule has a strong ability to do this.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

Since Mule is being built on top of Java and Spring, there is a strong integration capability in Mule to invoke Java methods. There are numerous ways to invoke a Java method in Mule. Today, I will be showing you how to use one such component in Mule: Invoke Component.

Invoke Component

By using the Invoke component, we can invoke a specified method of an object defined in a Spring Bean. We can provide an array of argument expressions to map the message to the method arguments. We provide the method name, and with that, Mule determines which method to use, along with the number of argument expressions provided. Mule automatically transforms the results of the argument expressions to match the method argument type where possible. If you have some advanced objects as your input argument, you can always give the argument types along with the argument array.

Note: Mule does not support multiple methods with the same name and number of arguments, which most of us Java developers know as method overloading.

Demo

Let's create a Java class with three different methods (methodA, methodB, and methodC) with different return types and different input arguments and try to invoke them using the invoke component.

package com.demo.entrypoint;

public class InvokeSpringSample {

public String methodA() {
System.out.println("Method A of Spring bean sample activated");
return "Method A";
}

public void methodB() {
System.out.println("Method B of Spring bean sample activated");
}

public String methodC(String inputA, String inputB) {
System.out.println("Method B of Spring bean sample activated");
return "Method C got " + inputA + " and " + inputB;
}

}

Mule Flow

In our Mule flow XML file, let's define a Spring Bean by which we will be invoking the Java class:

screen-shot-2016-09-24-at-12-40-38-pm

As you see, we have not defined anything special, and the above Bean is just plain old Spring Bean.

Next, drag the HTTP connector as your flow inbound endpoint and drag the Invoke component from the Mule palette.

screen-shot-2016-09-24-at-12-46-16-pm

Below are the properties you can see in the Invoke component:

screen-shot-2016-09-24-at-12-44-26-pm

Field Description Required? Example XML

Display Name

Customize to display a unique name for the component in your application.

doc:name="Invoke"

Name

The name of the message processor for logging purposes.

name="someName"

Object Ref

Reference to the object containing the method to be invoked.

X

object-ref="beanName"

Method

The name of the method to be invoked.

X

method="addTwoNumbers"

Method Arguments

Comma-separated list of Mule expressions that, when evaluated, are the arguments for the method invocation.

methodArguments="#[1], #[2]"

Method Argument Types

Comma-separated list of argument types for the method invocation. Use when you have more than one method with the same name in your class.

`methodArgumentTypes="java.lang.Float, java.lang.Float" `

Complete Flow

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

<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>

    <spring:beans>
    <spring:bean class="com.demo.entrypoint.InvokeSpringSample" name="invokeSpringBean">

    </spring:bean>

    </spring:beans>

    <flow name="invoke-component-demoFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/invoke" doc:name="HTTP"/>
        <invoke object-ref="invokeSpringBean" method="methodA" doc:name="Invoke Spring Bean Method A"/>
        <echo-component doc:name="Echo"/>
        <invoke object-ref="invokeSpringBean" method="methodB" doc:name="Invoke Spring Bean Method B"/>
        <echo-component doc:name="Echo"/>
        <invoke object-ref="invokeSpringBean" method="methodC" doc:name="Invoke Spring Bean Method C" methodArguments="#[message.inboundProperties.'http.query.params'.['name']],#[message.inboundProperties.'http.query.params'.['age']]"/>
        <echo-component doc:name="Echo"/>
    </flow>
</mule>

Test

Using postman, make a call to your endpoint and supplying query parameter for name and age  (http://localhost:8081/invoke?name=harish&age=30).

screen-shot-2016-09-24-at-12-51-33-pm

All your components will be invoked and processed. Below is the screenshot of the log.

screen-shot-2016-09-24-at-12-51-48-pm

Video Demo

Conclusion

This is just one way of calling a Java method in Mule. In my next blog, I will be talking about Entry point resolvers in Mule ESB.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
mule ,integration ,tutorial ,invoke

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}