DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Invoke Java Method in Mule Flow

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.

Harish Kumar user avatar by
Harish Kumar
·
Mar. 03, 17 · Tutorial
Like (7)
Save
Tweet
Share
35.25K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Java (programming language) Flow (web browser) Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Real-Time Stream Processing With Hazelcast and StreamNative
  • What Is Policy-as-Code? An Introduction to Open Policy Agent
  • Key Considerations When Implementing Virtual Kubernetes Clusters
  • The Role of Data Governance in Data Strategy: Part II

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: