RC
Technology

Invoke Java Method in Mule Flow

Since Mule is being built on top of Java and Spring their is strong integration capability in mule to invoke Java methods. Their are numerous ways to invoke a java method in Mule. Today I will be showing how to… Continue Reading →

Harish Kumar

Since Mule is being built on top of Java and Spring their is strong integration capability in mule to invoke Java methods. Their are numerous ways to invoke a java method in Mule. Today I will be showing how to use one such component in mule i.e. called Invoke Component .

Invoke Component

By using  Invoke component we can invokes 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 and 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 same number of arguments. Which most of us Java developer 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 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 lets 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 above bean is just plain old spring bean.

Next Drag the HTTP connector as your flow inbound endpoint and next 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 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 component will be invoked with arguments or no argument and will be processed below is the screen shot of the log.

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

 

Video Demo

Youtube video here https://youtu.be/-T3AGGBdqTA

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.

I hope you liked the video and the blog, please do comment and share and subscribe to give me more motivation to keep bringing good stuff to you all.

Thank you.

 

Related reading