Microservices for Java EE/Jakarta EE Developers

Nowadays, the concept of microservices is more than a simple novelty. With the advent of DevOps and the boom of container technologies and deployment automation tools, microservices are changing the way developers structure their applications. With this article, our intention is to illustrate that microservices are a valid option for Java/Jakarta EE developers and how Payara Micro is a robust platform to reach that goal. 

Advantages of Microservices  

The main purpose of a microservice architecture is to break down an application into smaller standalone components that are easier to handle, deploy, scale and maintain in the long term. Sounds familiar? Yes, this is something that many developers have been doing since a long time ago! Encapsulation, cohesion and a good understanding of service-oriented architectures have helped them apply this “divide and conquer” strategy to software architecture for many years and will do so in the future as well. 

So, what’s the advantage of a microservice architecture, then? Microservices are better understood when compared with their traditional counterparts: monolithic applications. Monolithic applications, or monoliths, are usually big enterprise applications structured into a single deployable package. If you want to introduce a new component or update an existing one, the entire monolith must be updated at once. This is the way enterprise applications were usually developed, focusing on the concept of tiers (UI, middleware-services, persistence, data) and the segregation of concerns across these tiers. 

So, while a monolith is just a normal application developed with traditional means, the microservice architecture approach would be to break down such an application into its separate component units (or just services), so that each unit would fulfill the following criteria: 

Monoliths vs. Microservices 

So, does that mean that microservices should be used instead of the traditional monolith in all cases? Not necessarily. Microservices not only define a new architectural style, but also require that development teams build applications in a different manner. 

There are also some advantages and disadvantages regarding when to use a monolith and when to use microservices:  (Considered advantages are green, disadvantages are redorange is for variable benefits depending on the software requirements or the organization’s environment.) 

Monoliths Microservices
Cost of changes across all tiers is high. A change of a single feature means a redeployment of the whole unit.  Changes are easier to implement and not costly as they are targeted to specific services only. 
All components belong to a single unit, there’s no communication overhead.  Remote calls are needed for communication between services, so this overhead factors in the performance of the overall application. 
The entire development team must be familiarized with the design and composition of the entire application.  Each service can be handled by a separate team, so this favors separation of concerns and responsibilities. 
Developed with a technological stack in mind, using one or two languages of choice. A main language/framework is chosen to govern the software architecture.  Each service can be developed using a different framework based on its requirements and needs. Standards are discarded in favor of improving each service with the right tools.  
Easier to deploy.  The complexity of deployment increases with the number of services and the communication routes between them. 
Clustering can be as easy or difficult depending on all the features implemented across the monolith.  Since each micro-service is small, clustering is generally easy, with scalability being a primary drive. 
Failure of a component can cause the entire application to fail and hinder the user experience.  Failure of a service will only bring that specific service down, leaving other services untouched 
There’s a focus on tiers and integration across tiers.   There’s a focus on business needs and communication concerns between teams.  

Microservices can help an organization develop their applications in such structured way. But sometimes, when applications are not critical to their business, or their quality attributes (performance, availability, scalability, security, etc.) are not specifically demanding, the traditional monolithic approach is more than enough. 

Microservices and Java EE 

The Java EE 8 set of specifications allows the creation of monolithic applications with ease. The main benefit of being a Java EE developer is that you don’t have to worry about handling technical concerns like network handling, transaction management or a resource’s lifecycle when the specific container service does that for you. This simplifies the developers' work allowing them to focus on business concerns instead.  

In the case of Java EE, a monolithic application’s example could be an e-commerce application with the following characteristics: 

Sounds complex? Yes. The previous scenario was a common occurrence in earlier Java EE days, but nowadays, with tools like Maven or OSGi, modularizing Java EE application has simplified the development of monoliths on Java EE, meaning that your entire application can be deployed in a single WAR file. However, if you would like to implement this e-commerce application as a set of microservices, a simple WAR will not be enough. 

There are no technology restrictions that prevent you to create microservices using the Java EE APIs via an application server like GlassFish (and Payara Server by extension). However, there are some considerations to have in mind: 

Addressing the concerns 

Payara Micro was created with these concerns in mind: it’s relatively smaller in size, packaged as a JAR, and allows developers to easily run a microservice with a simple command: 

java -jar payara-5.191.jar --deploy user-service-1.0.war --contextRoot / 

Run this command for each service and you don’t have to worry about a long startup time for your service or having to repeat the installation of a server multiple times! Since Payara Micro is based on the Java EE Web Profile, this means that complex or legacy APIs won’t be available (like JMS, JCA, JAX-WS, EJB Remote, etc.) 

One of the main advantages of using Payara Micro to provision a microservices architecture is its ability to automatically form a cluster with other Payara Micro instances that are living in the same network. Here’s a the most basic sample of launching a cluster of 2 instances: 

java -jar payara-micro-5.191.jar --autoBindHttp --clusterName user-service --name us-instance-1 

java -jar payara-micro-5.191.jar --autoBindHttp --clusterName user-service --name us-instance-2 

Once a new instance joins the cluster, every instance will report the current status of the Domain Data Grid (the conceptual equivalent of a modern cluster of server instances), letting you know that the cluster is scaling: 

 [INFO] fish.payara.nucleus.cluster.PayaraCluster] [tid: _ThreadID=61 _ThreadName=hz._hzInstance_1_user-service.event-3] [timeMillis: 1553274856105] [levelValue: 800] [[ 

Data Grid Status 

 

Payara Data Grid State: DG Version: 35 DG Name: user-service DG Size: 2 

 

Instances: { 

 DataGrid: user-service Instance Group: MicroShoal Name: us-instance-1 Lite: false This: true UUID: 634c290e-d972-45f0-93c8-241deb279f0d Address: /192.168.1.70:6900 

 DataGrid: user-service Lite: false This: false UUID: e5718197-6c83-4225-8aac-9d7892ef5bec Address: /192.168.1.70:6901 

 

}]]  


This is in thanks to Hazelcast IMDG, which is an open source distributed In-Memory Data Grid solution for Java applications that powers Payara Micro’s (and Server) clustering mechanisms. By default, Payara Micro instances cluster with each other using the multicast protocol, but in network environments where multicast is not supported (like Docker or certain cloud providers like AWS) other network communication mechanisms are supported as well. Additionally, Payara Services Ltd is working hard at implementing native support for a multitude of topologies and cloud providers, so keep and eye on what’s to come! 

Jakarta EE – Bringing Java EE to the Future 

At Java One 2017, Oracle made the huge announcement to open source the Java EE technology in its entirety, surprising the Java community. The new home of the Java EE technology would be the Eclipse foundation under the new name Jakarta EE (chosen in a democratic vote by the community after the Apache Foundation kindly “donated” the name).  

As of March 2019, the process of transferring the entire set of specifications, reference implementations and TCKs to the Eclipse Foundation is still underway but a major milestone was reached at the end of January: Eclipse GlassFish 5.1 was released to the public! This version of GlassFish is technically compatible with Java EE 8 and will pave the way for the first release of the Jakarta EE specification: Jakarta EE 8 later in the year. Once that milestone is reached, the community will move forward to a new version of the specification which will include a lot of new improvements the existing APIs. This blog post from Arjan Tijms introduces the most interesting ideas that at the moment are being implemented in the existing projects that have been transferred to the Eclipse Foundation. 

Having said all of this, what are the advantages of Jakarta EE over Java EE for microservice architectures then? Well, the answer is simple: None, since even after Jakarta EE 8 is released it won’t be any different in the scope of its features from Java EE 8. However, now that the technology has been open sourced and will be moved forward by the concerted efforts of the community, the existing APIs can be evolved with microservices patterns in mind, which will lead to a greater simplification of the development and deployment needed for these architectures! Even better, new APIs can be implemented in order to bridge the gap in the current set of features (like an API to modularize deployments or a standard API to orchestrate deployment units in a distributed arrangement). There are high expectations on the future of the Platform now that Oracle is no longer calling the shots with a renewed focus on modernizing the technology. 

Eclipse MicroProfile 

Although Jakarta EE is looking up good for providing a complete technology for microservices based on Enterprise Java, it is not the only technology that compatible Java EE servers can use. The Eclipse MicroProfile API is a set of standardized APIs aligned with the Java EE 8 specification that vendors can implement in order to develop microservices. 

The goal of MicroProfile is to provide components that build upon the core features of Java EE allowing a more intuitive development experience when implementing microservices, granting more flexible options and reduce risks or over-designing and re-inventing the same patterns. 

The current version of Eclipse MicroProfile is v3.3 and it is composed of the following set of APIs: 

Both Payara Server and Micro are compatible and certified implementations of Eclipse MicroProfile (version 5.191 is compatible up to MicroProfile v2.1 however). This means that you can use all MicroProfile’s APIs in conjunction with other Java EE APIs, and this is not limited for the development of microservices only. If you want to develop a traditional monolith while benefiting of the features and facilities introduced by MicroProfile, then you can just do that! Flexibility is a key concern of the Payara Platform. 

Good Practices and Recommendations 

You still need to go the extra mile and guarantee that your services fulfill the criteria described previously to develop enterprise-ready microservices, since the standard practices for developing Java EE applications will not be enoughThe following is a list of my personal good practices and recommendations that you should follow when building micro services with Java EE and Eclipse MicroProfile: 

Packaging 

Service Communication 

Components 

In most cases these 3 sources are more than enough for production needs (environment variables are fairly used in conjunction with containers), but the API allows for customizable configuration sources as well (like reading from a database or a configuration server too). 

Security and Data Management 

Infrastructure 

Start your Microservices Journey 

Microservices are here to stay, so any enterprise organization should consider implementing a microservice architecture when there are enough benefits to reap. For this reason alone, Java EE cannot be left behind and must move on in order to help developers transition to this style, which is goal that is no longer a dream thanks to both Jakarta EE and MicroProfile. Payara Micro is the ideal place to start, especially if you’re already familiar with the development of Java EE applications 

Keep in mind that microservices are not a silver bullet, and this should be common knowledge: use the traditional monolithic approach whenever possible, and switch to microservices when you think that their benefits suit your needs and justify their use. This transition not only means having a good grasp of the technical challenges you will be facing, but also understanding that there can be organizational and business constraints to be considered as well. 

 

 

 

 

 

Top