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”.
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:
|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=”#, #”|
|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” `|
In Anypoint Studio click File > New > Mule Project to create a new project, give it whatever name you wish and click Finish.
Drag an HTTP Connector from the palette to your empty canvas
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.
The following example creates a “Hello World” flow with an invoke component to implement a specific method in a referenced class.
Ensure that the GreetingService Java class is included in your project structure:
In our mule flow xml file lets define a spring bean by which we will be invoking the java class
<spring:bean name=”beanName” class=”invoke.ClassName”/>
<invoke object-ref=”beanName” method=”addTwoNumbers” methodArguments=”#, #” methodArgumentTypes=”java.lang.Float, java.lang.Float” name=”someName” doc:name=”Invoke”/>
As you see we have not defined anything special and above bean is just plain old spring bean.
Drag the invoke component from the mule palette.
Configure the invoke component to reference the Java class:
Using postman make a call to your endpoint
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 email@example.com.