Mule 4 introduces DataWeave 2.0 as the default expression language replacing Mule Expression Language (MEL). DataWeave 2.0 is tightly integrated with the Mule 4 runtime engine, which runs the scripts and expressions in your Mule application.
Since Dataweave 2.0 is default expression language for Mule 4, Dataweave can use almost all place within your Mule application. So, In some use-case Dataweave needs to call java method or instantiate java class to execute java complex business logic.
In my previous blog I explained usage of java within Mulesoft flow. In this blog I am explaining usage of java within Dataweave 2.0.
There are 2 ways we can use java within Dataweave code
Calling java method
Instantiate Java class
1. Calling java method — There is restriction with Dataweave when calling to java. you can only call Static methods via DataWeave (methods that belong to a Java class, not methods that belong to a specific instance of a class). Before making a method call from java class, you must import the class.
2. Instantiate Java class – Dataweave allows to instantiate a new object of any java class but you can’t call its instance method through dataweave. You can refer it as variables.
Rajnish Kumar is CTO of Vanrish Technology with Over 25 years experience in different industries and technology. He is very passionate about innovation and latest technology like APIs, IOT (Internet Of Things), Artificial Intelligence (AI) ecosystem and Cybersecurity. He present his idea in different platforms and help customer to their digital transformation journey.
MuleSoft is a lightweight integration and API platform that allows you to connect anything anywhere and enable your data through API. Mule evolved from java and spring framework. MuleSoft supports multiple language although all Mule module are developed in java.
Since Mule evolved from java it has capability to use direct java class and method in Mule flow. This capability gives flexibility to Mule developer to use java for complex business logic.
There are several ways you can use java within Mule. Here are some of Java modules available to use within MuleSoft application
There are 4 java modules are available in MuleSoft flow
New
Invoke
Invoke static
Validate type
To explain all these components and uses in Mule flow I created Utils.java and AppUtils.java classes
1. New – AppUtils.java
class instantiation can be achieved by calling constructor of this class
through MuleSoft New component within Mule flow.
AppUtils java class defined 2 contractors, So Mule constructor properties for NEW component is showing 2 options.
In above code, Instance of AppUtils class is created and placed into the “appInst” target variables to reuse same instance in Mule flow.
2. Invoke – In new java module we
instantiate AppUtils.java class and placed into “appInst” variable. Now to use
this variable set Invoke module and call one of method define in AppUtils.java
class. In AppUtils.java class, there is one non static method “generateRandomNumber”
defined with String parameter. In example we call this method through Invoke module.
3. Invoke static—Invoke static java
module enable mule flow to call java static method. This is one of the easy ways
to call any java method in Mule flow.
4. Validate type – Validate type java module
use instance of method from java. This module accepts “Accept subtypes”
parameter which indicates if the operation should accept all subclasses of a
class. By default it acceptSubtypes=“true” which means it will accept all sub
class of main class but if it will set as false acceptSubtypes=“false” then during
execution the operation throws an error (JAVA:WRONG_INSTANCE_CLASS)
Rajnish Kumar is CTO of Vanrish Technology with Over 25 years experience in different industries and technology. He is very passionate about innovation and latest technology like APIs, IOT (Internet Of Things), Artificial Intelligence (AI) ecosystem and Cybersecurity. He present his idea in different platforms and help customer to their digital transformation journey.