How to invoke java method into the flow of Mule ESB?

If Mule ESB needs to built on top of Java and Spring, the strong integration capabilities on Mule ESB helps to invoke Java method. There are many ways to invoke java method into Mule ESB flow.

Based on our expertise at Massil Technologies, we are going to discuss the most effective and most simple method know as “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. For more additional info Mulesoft Training

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.

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
  2. Configure the invoke message processor to reference the method in that object.
  • STUDIO Visual Editor
  • XML Editor or Standalone

In Anypoint Studio click File > New > Mule Project to create a new project, give it whatever name you wish and click Finish.

Step2:

Drag an HTTP Connector from the palette to your empty canvas.

Step3:

Click on the HTTP Connector to open its properties editor, then click the green arrow icon to create a new configuration for it. Leave all of the fields in default and click OK.

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/javaand click New > Class.
  2. Paste the code that follows and save your project.
  3. If you have not done so already, click RunRun As > Mule Application.

To get in-depth knowledge, enroll for a live free demo on Mulesoft Online Training

Leave a comment

Design a site like this with WordPress.com
Get started