Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Invoke Component Reference

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.

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"

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.


    
            
         
1
2
3
4
5
6
7
8
9
10
11
12
<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!"


         
      
1
2
3
4
5
6
7
8
9
10
11
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);
    }
     
}

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


    
            
         
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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" version="EE-3.5.0"
    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.