Tuesday, April 10, 2018

Invoke static custom Java methods in Mule 4


This example demonstrates how to use Mule 4's Java method to invoke a custom static method with 2 arguments. The custom class is reused from this post: Define and consume custom Java methods using Dataweave 2.0 in Mule 4.

Tips when working with custom java classes in Mule 4:
  1. Include your custom namespace in mule-artifact.json
  2. If you are making references to third party libraries, include them as maven dependencies in pom.xml

This is a simple example using a HTTP Listener to listen for POST requests to http://localhost:8081/list-files2



It takes in a parameter named path which could be a path to local folder, or a network folder.
After dragging the HTTP Listener from the design palette, drag a Java Invoke static component by typing Java in the search bar.


To configure the Java Invoke static method, take note that the arguments need to be passed in, Dataweave-style by enclosing them within #[].

If you are passing in more than 1 argument, enclose them within a map {}.
The argument format will be arg0:xxx, arg1:xxx and so on. Attempting to name the arguments otherwise results in errors during runtime.

I am going to invoke a static method called listOldFiles from my previous example.
public static ArrayList listOldFiles(String dir, Integer daysToRemainFiles) {}.

In the example below, I hardcoded 10 as the second argument to demonstrate how to assign values directly. This method will display files older than 10 days in the path specified from the client.


Finally, I include a Dataweave component to return the results in json as below:


Test request in POSTMAN:


Results:
Flow XML example:
      <flow name="invokeListFilesJavaFlow" doc:id="55ad8daf-b880-4a24-995b-0279f7ad1d6e" >  
           <http:listener doc:name="Listener" doc:id="62ca0aec-4d0c-44c2-8d98-1c4a28a3ba55" config-ref="HTTP_Listener_config" path="list-files2" allowedMethods="POST"/>  
           <java:invoke-static doc:name="Retrieve file list" doc:id="5e6f24c9-3921-405e-8af6-6f2adda8d607" class="customUtils.CustomFileUtil" method="listOldFiles(String,Integer)">  
                <java:args ><![CDATA[#[{ arg0:payload.path as String, arg1: 10 as Number }]]]></java:args>  
           </java:invoke-static>  
           <ee:transform doc:name="Transform Message" doc:id="26b5503b-6a1b-48d5-9411-de499f626ee6" >  
                <ee:message >  
                     <ee:set-payload ><![CDATA[%dw 2.0  
 output application/json  
 ---  
 {  
      results: payload  
 }]]></ee:set-payload>  
                </ee:message>  
           </ee:transform>  
      </flow>  

3 comments:

Unknown said...

This information is really awesome thanks for sharing most valuable information.
Mulesoft Online Training

goformule said...

Very informative post for mulesoft developers.You can also visit goformule.com for mulesoft stuff.

Priya said...

Excellent!! You have provided very useful information in this article. I have read many articles in various sites but this article gives detailed explanation.
Mulesoft Online Training
Mulesoft Training in Hyderabad

Related Posts Plugin for WordPress, Blogger...