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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • High-Performance Java Serialization to Different Formats
  • Java Memory Management
  • Protecting Your Domain-Driven Design from Anemia
  • The Foundations for Building an Apache Flink Application

Trending

  • The 4 R’s of Pipeline Reliability: Designing Data Systems That Last
  • Java Virtual Threads and Scaling
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Evolution of Cloud Services for MCP/A2A Protocols in AI Agents
  1. DZone
  2. Coding
  3. Languages
  4. Java in Mule for Java Programmers

Java in Mule for Java Programmers

You have been a Java programmer for many years now and you want to re-use your technical skills and knowledge in Mule applications.

By 
Sebastien Colas user avatar
Sebastien Colas
·
Dec. 10, 20 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
10.4K Views

Join the DZone community and get the full member experience.

Join For Free

You have been a Java programmer for many years now, and you want to re-use your technical skills and knowledge in Mule applications. In this article, you will learn the typical use cases where you can directly include Java code in your Mule application. If you are interested to know more about Java and Mule, please join the Boost your Java career with Mule Integration meetup, where you have a chance to receive free professional training and certification.

Introduction

In this article, you will see three Mule use cases where you can directly code in Java:

  • Java data types.

  • Java call in DataWeave.

  • Java call thanks to the Java module.

Here are the two Java classes you will use in this article:

  • Customer class: a classical class, also known as POJO (Plain Old Java Object).

  • CustomerUtil class: a utility class to manipulate the Customer class.

Java
 




xxxxxxxxxx
1
21


 
1
package com.mulesoft.training;
2

          
3
public class Customer {
4
    
5
    private String lastName;
6
    private String firstName;
7
    private int age;
8
    
9
    public Customer(String lastName, String firstName, int age) {
10
        setLastName(lastName);
11
        setFirstName(firstName);
12
        setAge(age);
13
    }   
14

          
15
    // Getters and setters removed for brevity
16

          
17
    public String toString() {
18
        return(getLastName()+" "+getFirstName()+" ("+getAge()+")");
19
    }   
20
}



Java
 




xxxxxxxxxx
1
19


 
1
package com.mulesoft.training;
2

          
3
import java.util.ArrayList;
4
import com.mulesoft.training.Customer;
5

          
6
public class CustomerUtil {
7

          
8
    public static ArrayList<Customer> customerTestList()
9
    {   
10
        ArrayList<Customer> array = new ArrayList<Customer>();
11
         
12
        array.add(new Customer("Simpson","Homer",39));
13
        array.add(new Customer("Simpson","Lisa",8));
14
        array.add(new Customer("Simpson","Bart",10));
15
        array.add(new Customer("Simpson","Marge",36));
16
        array.add(new Customer("Simpson","Maggie",1));
17
        return array;
18
    }
19
}


java-mule directory example

Remember: In a Mule Project developed with Anypoint Studio, you have to put those files into the src/main/java directory.

Define a Data Type with a POJO

Into a Mule Application, you can use a lot of data types like RAML (for JSON) or XSD (for XML). Unfortunately, most of those data types are not very strict in terms of data typing. That’s why when you want to be sure of a type, you can use a Java object. In Mule applications, it is very common to use Java for the internal data type.

metadata types dropdown menu


To use a Java class as a data type in your Mule application, you have to declare it as a type within Metadata Types.metadata types interface

In the menu, you have to provide a name to your datatype: Customer-pojo, select the type Object and specify the class to use: com.mulesoft.training.Customer

Now you can use the Customer-pojo Metadata type everywhere in your application. In general, it can be used inside a transform processor or directly in a DataWeave expression in a processor.

If you are familiar with Java Streams and lambdas and want to see how to replicate them in a DataWeave script, I suggest you read the DataWeave function chaining for Java programmers and DataWeave lambdas for Java programmers blogs, which cover method chaining, the Java Stream API and lambdas.

Call a Java Method from DataWeave

DataWeave is a powerful transformation language that will satisfy the vast majority of your needs; however, you may find it necessary (or prefer) to implement a transformation as a static method in a Java class.  You can use that static method directly in a DataWeave script.

Here is a table that shows the importation and use of a our CustomerUtil class in Java and DataWeave:

Java code

Dataweave corresponding code

Java
 




xxxxxxxxxx
1


 
1
import com.mulesoft.training.CustomerUtil


Scala
 




xxxxxxxxxx
1


 
1
import java!com::mulesoft::training::CustomerUtil


Java
 




xxxxxxxxxx
1


 
1
CustomerUtil.customerTestList()


Scala
 




xxxxxxxxxx
1


 
1
CustomerUtil::customerTestList()


The DataWeave script will be:

Scala
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
output application/json
3
import java!com::mulesoft::training::CustomerUtil
4
---
5

          
6
CustomerUtil::customerTestList()


In Anypoint Studio, there is a nice feature called preview that allows you to display the result of your DataWeave expression without starting the Mule engine.

transform message tab interface

This is a simple example that returns an array of JSON objects, but you can imagine passing data as a parameter to the static class and applying a complex transformation to it, and returning the result of that transformation.

Call a Java Method Using the Java Module

Even though most logic can be implemented in DataWeave, you sometimes need or want to instantiate a Java object and invoke its methods. To do this, you add the MuleSoft Java module displayed as Java in the list of available modules.

To install the Java module in the Mule Palette view, click on Add Modules, and then on the right-hand side, drag-and-drop the Java module.

mule palette interface

The Java module allows you to:

  • Invoke a method on an already instantiated object.

  • Invoke a static method on a class.

  • Create a New object.

  • Validate an object type.

The next step is to instantiate with the New processor and invoke with the Invoke processor the Java class into a Flow.

moduleFlow

In the New processor, you have to select the Java class you want to instantiate by selecting its constructor and pass any parameters necessary. Arguments need to be written in JSON format, and the key must match the name of the Java constructor argument.

Java Method

JSON arguments

Java
 




xxxxxxxxxx
1


 
1
Customer(
2
  String lastName, 
3
  String firstName, 
4
  int age
5
)


JSON
 




xxxxxxxxxx
1


 
1
{
2
  "lastName":"Simpson",
3
  "firstName":"Homer",
4
  "Age":39
5
}


new processor interface

By default, the newly instantiated object is stored in the Mule payload, although you can configure it to be stored in a variable and therefore reference it via vars.variableName

Invoke processor takes by default the input payload as an input and stores the result of the method in the output payload.

Invoke processor interface

Conclusion

Now we have seen three different ways in which you can leverage Java in Mule Applications. MuleSoft strongly advocates, endorses, and makes it easy to adopt a reuse approach to development, so if you already have Java classes developed, you can easily reuse all those classes in your Mule application.

Java (programming language) Programmer (hardware) application Data (computing) Object (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • High-Performance Java Serialization to Different Formats
  • Java Memory Management
  • Protecting Your Domain-Driven Design from Anemia
  • The Foundations for Building an Apache Flink Application

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!