Microservices with OpenEJB

I’ve been looking into the OpenEJB JEE container and microservices in the recent weeks. And I found that apart from Spring Boot, theOpenEJB container is a good microservice platform as well.

What Is like about JEE is that it really has become very easy since version 6 to create applications. No lengthy deployment descriptors required anymore. At least not for small applications or functions such as above mentioned microservcies.
Yet if required JEE offers the whole range of powerful container serives like distributed transaction management, persistence, rest services and so on. So I was looking at a way to create microservices on this platform. The two major non-functional requirements for these microservices are:

  • Easy and quick to develop and test.
  • The software should be easy to deploy. Ideally as a runnable standalone jar (java -jar… / java -cp…)

Developing a microservice

The first requirement is very easy to fulfill with OpenEJB since it integrates seamlessly with maven. I’ve made a small restful service example here derived from the OpenEJB examples.
To implement a session bean as a restful service, you only need to add the required annotations:

Testing is likewise easy with OpenEJB:

In the example above which can be found here the RunWith annotations tells JUnit to use the OpenEJB test runner. Rest is enabled using the EnableServices annotation. The web app containing the rest service is created in the method app(). And finally the CXF web clients are used to access the rest services deployed.

Another way to test is to create an ejb container as shown here:

In the method annotated with BeforeClass the container is created which automatically deploys all annotated EJBs. The properties tell OpenEJB to enable rest services. Without this configuration the ejbs would not be accessible via rest. The test case itself looks very similar to the one above.

The test case can be run from within the ide as well as using maven and is rather fast considering that a full-fledged ejb container is created.

So the first requirement of easy development and testing is reasonably fulfilled by this approach I’d say.

Embedding openejb

In order to meet the second requirement I chose to embed OpenEJB in a simple Main-class:

This code is similar to the one in the test case. Of course it is missing shutdown hooks as well as waiting for termination as this main immediately exits. This code can as well be run from within an IDE like Eclipse.

For creating a single executable jar I first chose the Maven Shade Plugin. It offers a way to create an “Uber Jar”. But I couldn’t get it working in a reasonable amount of time because of class loading problems. So I opted for the Maven Dependency Plugin. The plugin is used to put all depenencies into a folder:

Now after doing a

I am able to run the example with the command:

So no single jar but nevertheless a very convenient way to package, deploy and run the microservice.

Conclusion

OpenEJB is a suitable choice for microservices developed with JEE. Although assembling the jar into one single jar might not always work. On the other hands microservices based on JEE can take advantage of all the powerful JEE mechanisms as described above.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">