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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Member Spotlight: Shai Almong
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Where Should a Transaction End? Designing Consistency Boundaries in Distributed Java Systems

Trending

  • Everything About HTTPS and SSL in Java
  • Embabel vs LangGraph4j: Two Agentic Philosophies for Investment and Risk Analysis in BFSI
  • Building a CI/CD Pipeline With Kubernetes: A Development Guide With Deployment Considerations for Practitioners
  • When One Docker Swarm Replica Became Unreachable Over an Overlay Network: A Production Case With Traefik

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
3.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Member Spotlight: Shai Almong
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Where Should a Transaction End? Designing Consistency Boundaries in Distributed Java Systems

Partner Resources

×

Comments

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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