Back to Blog
3 min. read

How to use Java constructor in Mule 4

Java constructors of any Java class can be called from the mule configuration xml file using the “New” operation of the Java module. For this tutorial, let’s consider the  below java class which has two constructors, i.e., one default and one constructor that takes input parameters to initiate class/type/course.

Initiate the default constructor

Initiate the default constructor using the “New” operation of the Java module. Specify the Java class name and constructor name in the connector configuration.

										<java:new constructor="Person()" class="com.me.Person"/>
									

Initiate the constructor that takes arguments

Initiate the constructor that takes input arguments using the “New” operation of the Java module. Specify the Java class name and constructor name and the arguments/ideas/statements/discussions in the connector configuration.

										<java:new constructor="Person(java.lang.String,java.lang.Integer)"
class="com.me.Person"> 

   <java:args><![CDATA[#[{ 
      name: "Priyabrata Dash", 
      age: 30 
   }]]]></java:args> 

</java:new> 
									

Below Maven compiler plugin must be configured in the pom.xml to compile Java classes with the -parameters flag:

										<plugin>
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-compiler-plugin</artifactId> 
    <version>x.x.x</version> 
    <configuration> 
        <compilerArgs> 
            <arg>-parameters</arg> 
        </compilerArgs> 
    </configuration> 
</plugin> 
									

 

How to use Java Collection in Mule4

Java collections can be initialized and used in mule code using New and Invoke operations of Java module:

  1. Initialize Java collection class using the above mentioned New operation;
  2. Add data to the collection using Java Invoke.

 

In this article, I am taking an example of initializing an ArrayList type of collection and storing values.

 

Initialize the ArrayList with java New operation:

  • Initiate the ArrayList using the “New” operation of the Java module. Specify the Java class name as “java.util.ArrayList” and constructor name as “ArrayList()” in the connector configuration
  • Define the target variable to store it
										<java:new doc:name="Initate ArrayList" doc:id="e3c787f1-daa0-4ff5-b0f7-2913aa056156" 
class="java.util.ArrayList" constructor="ArrayList()" target="arrayListVar"> 

</java:new> 
									

Use Java Invoke component for adding data to the collection:

  • Drag the Java Invoke to the mule flow
  • Configure the target mentioned above
  • Variable ‘’arrayListVar’’ in the Instance section
  • In the argument, pass the values that need to be stored in the ArrayList. Input to this argument field should be the number of parameters required to invoke a particular method. In this example, I have selected the method addAll(Collection arg), which takes only one argument of type Collection as below.
										{ 
    arg0: [10,20,30] 
} 
									
  • A/The Class should be java.util.ArrayList and Method, as mentioned above, is  addAll(Collection arg)
										<java:invoke method="addAll(java.util.Collection)" doc:name="Invoke" 
doc:id="0708d2c3-3325-476c-9b0c-ebe7949ed76d" class="java.util.ArrayList" 
instance="#[vars.arrayListVar]"> 

    <java:args ><![CDATA[#[{ 
       arg0: [10,20,30] 
    }]]]></java:args> 

</java:invoke> 
									

The Output of the above Java invokes: The ‘arrayListVar’ will look like below, containing 3 numbers.

Conclusion

To summarize, this article shows how easy it is to initialize and use Java constructors and Java collections in mule 4 applications. Using the steps mentioned above, any Java classes/ collections can be used in mule applications.

Contact us