Implementing Microservices with Java EE & Payara

For years, many companies have trusted in the Java EE platform to build their core applications because it offers them resiliency and security, among other characteristics. However, the Java EE platform has gotten the negative reputation of being a heavy-weight, complex framework, needing lots of configuration files, and being oriented towards monolithic applications. The architecture of microservices has changed the way we build our applications. The new trend is to build systems by creating many artifacts, or mini-applications. Each one focuses on a specific business logic, instead of the classic monolithic application with all the business logic in one application. 

So here is the question: Can Java EE support lightweight, resilient applications based on a microservices architecture?

The answer is YES. Java EE is a good option for implementing microservices on our systems. We are going to see how it’s possible to create a microservice application using Java EE features and Payara Micro as the application server.

Why use Java EE for Microservices?

With Java EE the application server provides us with a stack of out-of-the-box technologies that we can use at any moment, so we can focus our efforts on the business logic from the beginning of our microservice. What’s more, all of its technologies follow the principle of Convention over Configuration, therefore, minimum to no settings are required to start coding. Finally, because the application server provides us with a stack of Java EE technologies, our resulting WAR file doesn’t have external or third party libraries (maybe just a few for specific needs) and its size will be minimal, usually less than 1mb. We don’t need to download half of the internet in dependencies just to create a small application (cough, cough, spring-framework, cough, cough).

Which Java EE Application Server Should I Use?

When it comes to Java EE application servers, their reputation is far from lightweight. Big, heavy monsters like Weblogic, Websphere and JBoss EAP come to mind. Their purpose is to deploy the traditional applications (that still exist and will continue to exist), but they aren’t an effective option for microservices. Fortunately, lightweight Java EE application servers like Tomee, WildFly and (my favorite) Payara, have appeared on the scene. Payara is a Java EE Application Server derived (forked) from Glassfish Server. When Oracle decided to stop giving commercial support to Glassfish Server, Payara was born. It’s called a Glassfish Server on steroids because, in addition to continuous bug fixes, it offers other features like:

  • Automatic Clustering (via Hazelcast)
  • Caching Tools
  • Health Check Service
  • and more…

Payara provides different profiles that can be used according to our needs:

  • Payara Full: Provides the complete stack of technologies on the Java EE platform
  • Payara Web: Provides the profile for developing web applications using Java EE
  • Payara Micro: Provides a set of Java EE technologies oriented towards microservices development.

 

What does Payara Micro do? 

Payara Micro is a JAR file that enables you to run WAR files from the command line without any installation. It’s designed to run Java EE applications in a modern containerized/virtualized infrastructure in the cloud. Some of Payara Micro’s main features:

  • Less than 70mb in size and 30mb of memory when it’s running
  • It comes with a stack of Java EE technologies:
    • Servlets, JSP, Java Server Faces
    • WebSockets
    • JAX-RS (restful services), JSON-P
    • CDI
    • EJB lite, JTA, JPA
    • Bean Validation, Interceptors, JBatch, JCache, Concurrency, etc
  • Automatic clustering
  • Command line, uber/fat jars, etc

The Payara Micro server will automatically cluster with other servers in the network. Your Java EE application can be elastically scaled horizontally by adding and removing containers based on demand.

How to use it?

As mentioned above, Payara Micro allows for WAR files to be run from the command line. It’s as simple as typing and executing the next sentence:

That sentence will run our web application on the Payara Micro server using the default configuration. However, it’s usually required to indicate custom settings according to the needs of our applications, so it’s possible to configure more features via the command line, for example:

There is also the option to start and configure the WAR programmatically. WAR

Let’s Get Started

Now that we have read about some of the Java EE advantages for implementing microservices and an application server oriented towards microservices like Payara Micro, let’s make an example and see for ourselves how easy it is to build them. 1. Create a web application with Maven. We can check here that the only dependency we have is javaee-api and it’s provided. pom.xml

2. Creating the Device class. This is a model object to transfer information. Device.java

3. Creating some CDI managed bean classes to provide our business logic. SessionService.java

DeviceService.java

4. Creating a WebSocket Endpoint using Java EE specification, to publish our logic and interact with the Javascript UI and the websocket client. To create a Web-socket endpoint in Java EE it is only necessary to create a simple class, annotate it with @ServerEndpoint, and create the methods to handle the websocket operations: onOpen, onClose, onError, onMessage. DeviceWebSocket.java

5. Creating a REST Service to publish our logic. DeviceRest.java

6. The HTMLl, Javascript and CSS files used in this demo are placed in: https://github.com/adamgamboaGL/payara-micro-demo/tree/master/code/payarademo/src/main/webapp 7. Docker Images to deploy the demo application on a payara-micro server using Docker. This dockerfile inherits from a docker image that has a Linux SO and Java installed on it. Then it downloads and configures the payara-micro jar to run the demo application. Dockerfile: payara-micro

This dockerfiles inherits from the payara-micro image and only deploys the demo application. Dockerfile: devices

8. Finally, start to demo Java EE micro-service application (running in payara-micro on a Docker container). Compile the code.

Build the docker image of payara-micro and the demo application.

Run the docker instance and start the demo application in the payara-micro server by using the following command line.

 Conclusions

The Java EE platform continues to evolve as a productive framework for application development and is flexible enough to support classic monolithic applications as well as microservices applications. In cloud environments like AWS, Oracle Cloud, Azure, etc, the bandwidth and size of the files are very important when an application is deployed. Using Java EE, we can have small WAR files for our microservices. What’s more, quoting the Java EE evangelist Adam Bien: “The smaller the WAR, the faster the deployment process.” And this can be extended, too. Having smaller WAR files also means less time and bandwidth when uploading your application to the cloud. Payara and Payara Micro seem to be good options for deploying microservices in the Java EE environment because of the features like automatic clustering, health check & monitoring services. If a company using Java EE wants to introduce a micro-service architecture they DO NOT need to change to other frameworks like “spring boot” and throw away all Java EE knowledge.   References http://blog.payara.fish/ __

 

 

Subscribe to our Blog
 

 

 

Adam Gamboa
Adam Gamboa
Adam is a Java developer with 8 years of experience oriented towards Java EE, back-end and Rest API development.

Deliver off-the-chart results.

WordPress Video Lightbox Plugin