Saturday, July 22, 2017

JavaEE: Microservices with TomEE

Since a term Micro-Web-Services was introduced by Dr. Peter Rodgers in 2005, the concept of smaller/granular services has gained much attention from software communities.  A software developer and author Martin Fowler stated the following in 2014.

"The microservices architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API.  These services are built around business capabilities and independently deployed by fully automated deployment machinery"

Each of microservices can be deployed, updated, and redeployed independently, but continuously keeping the integrity as a whole application.  Java EE frameworks also support the microservices architecture.

In this post, let's see how the TomEE helps developers to develop and to build microservices as a stand-alone executable jar with minimal footprint.  This is nothing special other than showing how TomEE packs and deploys a microservice.

Microservice Codes

Same asynchronous RESTful API codes shown here will be used with a TomEE maven plugin defined in a pom.xml on a previous post.

Executable Jar of Everything in It
TomEE provides a maven plugin goal 'tomee:exec'.  This is a command to build an executable jar.
   mvn clean install tomee:exec

Directory Structure Before Executing the Command

Directory Structure After Executing the Command
The command creates a demo-exec.jar file under the target directory and its size is a little more 60MG. (I am using a TomEE plus version)


This is an executable jar file that contains everything you need to run the micro web service Restful API.

Run
   java -jar target/demo-exec.jar

This is the beginning of the logs when the command is run.

slkc:demo jihwan$ java -jar target/demo-exec.jar 
Extracting tomee to /Users/jihwan/workspace/MyWork/demo/.distribution
Using CATALINA_BASE:   /Users/jihwan/workspace/MyWork/demo/.distribution/apache-tomee
Using CATALINA_HOME:   /Users/jihwan/workspace/MyWork/demo/.distribution/apache-tomee
Using CATALINA_TMPDIR: /Users/jihwan/workspace/MyWork/demo/.distribution/apache-tomee/temp
Using JRE_HOME:        /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home
Using CLASSPATH:       /Users/jihwan/workspace/MyWork/demo/.distribution/apache-tomee/bin/bootstrap.jar:/Users/jihwan/workspace/MyWork/demo/.distribution/apache-tomee/bin/tomcat-juli.jar

When you run the command for the first time, it creates a directory '.distribution' under the directory where you run the command. Then, it starts your microservices on TomEE.  Under this directory, another 'apache-tomee' directory is created and the entire TomEE server is running with your microservices.  You can modify your code any time and redeploy it independently.



Running the account service shown on a previous post.

No comments:

Post a Comment

Java 9: Flow - Reactive Programming

Programming world has always been changed fast enough and many programming / design paradigms have been introduced such as object oriented p...