Contact Us 1-800-596-4880

Invoke Component Reference

Mule Runtime Engine versions 3.5, 3.6, and 3.7 reached End of Life on or before January 25, 2020. For more information, contact your Customer Success Manager to determine how you can migrate to the latest Mule version.

The invoke component invokes a specified method of an object defined in a Spring bean. You can provide an array of argument expressions to map the message to the method argument. Mule determines which method to use via the method name, along with the number of argument expression provided. Mule automatically transforms the results of the argument expressions to match the method argument type, where possible. Note that Mule does not support multiple methods with the same name and same number of arguments.

Configuring the Invoke Component

Use the invoke component when you have an existing method defined in custom Java code that you wish to use in processing a message. Configuring an invoke message processor involves two steps:

  1. Include the object that contains the method in the application’s src/main/java directory.

  2. Configure the invoke message processor to reference the method in that object.

Studio Visual Editor

invoke pe blank
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-seperated list of Mule expressions that, when evaluated, are the arguments for the method invocation.

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

Method Argument Types

Comma-seperated 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"

XML Editor or Standalone

Include the invoke method in your flow, and define a Spring bean as a global element with the reference to the object containing the method.

Element Description

invoke

Invokes a method in a specified object using method arguments determined by Mule expressions.

Attribute Description Required?

doc:name

Customize to display a unique name for the component in your application. Not required for Mule Standalone.

name

The name of the message processor for logging purposes.

object-ref

Reference to the object containing the method to be invoked.

X

method

The name of the method to be invoked.

X

methodArguments

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

methodArgumentTypes

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.

<spring:beans>
        <spring:bean name="beanName" class="org.mule.example.ClassName"/>
    </spring:beans>

    <flow

...
        <invoke object-ref="beanName" method="addTwoNumbers" methodArguments="#[1], #[2]" methodArgumentTypes="java.lang.Float, java.lang.Float" name="someName" doc:name="Invoke"/>

...

    </flow>

Example

In this example, a "Hello World" flow uses an invoke component to implement a specific method in referenced class. The Java Class referenced is provided below and must be included in the application’s src/main/java directory.

Hitting http://localhost:8081/greeting?name=Bob in a browser returns "Hello Bob!"

package org.mule.example;
public class GreetingService {
    public String sayHello(String name) {
        return String.format("Hello %s!", name);
    }

    public String sayGoodbye(String name) {
        return String.format("Goodbye %s!", name);
    }

}

Studio Visual Editor

Create a simple flow using HTTP connector and an invoke component.

invokeflow

Configure the invoke component to reference the Java class.

invoke pe

Ensure the Java class is included in your project structure.

invokejava

XML Editor or Standalone

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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">
    <spring:beans>
        <spring:bean name="greetingService" class="org.mule.example.GreetingService"/>
    </spring:beans>
    <flow name="greetingFlow" doc:name="greetingFlow">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="greeting" doc:name="HTTP"/>
        <invoke object-ref="greetingService" method="sayHello" methodArguments="#[message.inboundProperties.'http.query.params'.name]" doc:name="Invoke"/>
    </flow>
</mule>

See Also

  • Learn more about [other components] available in Mule.