97008 48403
+91 40-27177600

Blog

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

single-blog

From the technical center of excellence of Massil Technologies, our technology leaders make it easy to understand the complex situations you come across in Mule ESB from the experience of Massil Technologies to have countered them in their experience of working on client projects in real time. This blog talks about invoking java method into the flow of Mule ESB. This blog is a part of series of blogs being authored and published by Massil Technologies for the benefit of the IT community globally.

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  Invoke component we can invokes 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 and 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 same number of arguments. Which most of us Java developer 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.

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” `

Step1:

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.

Step4:

Ensure that the GreetingService Java class is included in your project structure:

Step5:

In our mule flow xml file lets define a spring bean by which we will be invoking the java class

 

<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>

 

As you see we have not defined anything special and above bean is just plain old spring bean.

Step6:

Drag the invoke component from the mule palette.

Step7:

 

Configure the invoke component to reference the Java class:

Complete Flow:

 

Step8:

Using postman make a call to your endpoint

All your components will be invoked and processed. Below is the screenshot of the log

Hence, we have gone through the step by step process to depict the way Massil Technologies team has addressed the scenario mentioned above. Hope this article from Massil Technologies was useful for you. If you have any further queries on this topic, please reach out to info@massil.tech.

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Get in touch with us

Scroll to top