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 arguments. Mule determines which method to use via the method name, 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.

Note: 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-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" `


    
            
         
1
2
3
4
5
6
7
8
9
10
11
12
<spring:beans>
        <spring:bean name="beanName" class="invoke.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>

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.

Attributes:

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-separated list of Mule expressions that, when evaluated, provide 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="invoke.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

The following example creates a "Hello World" flow with an invoke component to implement a specific method in a referenced class.

  1. Right-click src/main/java and click New > Class.

  2. Paste the code that follows and save your project.

  3. If you have not done so already, click Run > Run As > Mule Application

  4. Browse to  http://localhost:8081/greeting?name=Ivette  which returns "Hello, Ivette!" - You can change the name at the end of the URL in your browser to greet other people.

  5. The browser appears like this:

    InvokeGreeting

    
                
             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    package invoke;
    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);
        }
    
    }
  1. Create a simple flow using an HTTP connector and an invoke component:

    invoke

  2. Configure the invoke component to reference the Java class:

    invoke-pe

  3. Ensure that the GreetingService 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
<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.6.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="invoke.GreetingService"/>
  </spring:beans>
  <http:listener-config name="listener-config" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
  <flow name="greetingFlow" >
     <http:listener config-ref="listener-config" path="greeting" doc:name="HTTP Connector"/>
     <invoke object-ref="greetingService" method="sayHello" methodArguments="#[message.inboundProperties.'http.query.params'.name]" doc:name="Invoke"/>
  </flow>
</mule>

See Also