DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java

Trending

  • DevOps and Platform Engineering Readiness Checklist: Everything Needed for a Scalable, Secure, High-Velocity Delivery Platform
  • Architecting an Embedded Efficiency Layer: A Platform Deep Dive into Day-Two Operational Tuning
  • Building Enterprise-Grade Real-Time IoT Dashboards with Vue 3, MQTT, and Kafka
  • The Agentic Agile Office: Streamlining Enterprise Agile With Autonomous AI Agents
  1. DZone
  2. Coding
  3. Java
  4. How to Use Java Constructor and Collection in Mule 4

How to Use Java Constructor and Collection 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.

By 
Priyabrata Dash user avatar
Priyabrata Dash
·
May. 17, 22 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
3.2K Views

Join the DZone community and get the full member experience.

Join For Free

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 the class.

Java class

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

“New” operation

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



Specify the Java class name and constructor name and the arguments in the connector configuration.


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

Java collections can be initialized and used in mule code using New and Invoke operations of the 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 into it.

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 

Define the target variable to store it

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 the Java Invoke component for adding data to the collection:

  • Drag the Java Invoke to the mule flow
  • Configure the target as 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]

}


Class should be java.util.ArrayList and Method, as mentioned above, is addAll(Collection arg).

 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>


Output of the above Java invoke: The ‘arrayListVar’ will look like below which contains 3 numbers.

Output of the above Java invoke


Java (programming language)

Published at DZone with permission of Priyabrata Dash. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook