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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Spring Transaction Propagation in a Nutshell
  • How Spring and Hibernate Simplify Web and Database Management
  • Graceful Shutdown: Spring Framework vs Golang Web Services
  • Enabling Behavior-Driven Service Discovery: A Lightweight Approach to Augment Java Factory Design Pattern

Trending

  • Why Database Migrations Take Months and How to Speed Them Up
  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • Agentic AI for Automated Application Security and Vulnerability Management
  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.

By 
Harish Kumar user avatar
Harish Kumar
·
Mar. 03, 17 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
36.6K 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.

Related

  • Spring Transaction Propagation in a Nutshell
  • How Spring and Hibernate Simplify Web and Database Management
  • Graceful Shutdown: Spring Framework vs Golang Web Services
  • Enabling Behavior-Driven Service Discovery: A Lightweight Approach to Augment Java Factory Design Pattern

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: