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.
Join the DZone community and get the full member experience.
Join For FreeYou 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.
xxxxxxxxxx
package com.mulesoft.training;
public class Customer {
private String lastName;
private String firstName;
private int age;
public Customer(String lastName, String firstName, int age) {
setLastName(lastName);
setFirstName(firstName);
setAge(age);
}
// Getters and setters removed for brevity
public String toString() {
return(getLastName()+" "+getFirstName()+" ("+getAge()+")");
}
}
xxxxxxxxxx
package com.mulesoft.training;
import java.util.ArrayList;
import com.mulesoft.training.Customer;
public class CustomerUtil {
public static ArrayList<Customer> customerTestList()
{
ArrayList<Customer> array = new ArrayList<Customer>();
array.add(new Customer("Simpson","Homer",39));
array.add(new Customer("Simpson","Lisa",8));
array.add(new Customer("Simpson","Bart",10));
array.add(new Customer("Simpson","Marge",36));
array.add(new Customer("Simpson","Maggie",1));
return array;
}
}
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.
To use a Java class as a data type in your Mule application, you have to declare it as a type within Metadata Types.
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:
xxxxxxxxxx
%dw 2.0
output application/json
import java!com::mulesoft::training::CustomerUtil
---
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.
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.
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.
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
}
|
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.
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.
Opinions expressed by DZone contributors are their own.
Comments