jueves, septiembre 18, 2014

Apache TomEE + Shrinkwrap == JavaEE Boot. (Not yet Spring Boot killer but on the way)

WARNING: I am not an expert of Spring Boot. There are a lot of things that I find really interesting about it and of course that can really improve your day-to-day work. Moreover I don't have anything against Spring Boot nor people who develop it or use it. But I think that community are overestimating/overvenerating this product.

A year ago I started to receive a lot links about blogposts, tweets, information about Spring Boot. From his website you can read:

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that can you can "just run". 

And it seems that this thing has just revolutionized the Java world.

For example a Spring MVC (RESTful as well) application in Spring Boot looks like:

As you can see the magic happens inside SpringApplication class which starts an embedded Tomcat or Jetty and using Spring thing it registers this controller. Pretty impressive I know with a few lines you can have an endpoint ready to be used.

But I wonder to myself if it is possible to use the same approach in JavaEE world and with the same low-level and light requirements. And the answer is absolutely. I have just created a really small protoype/proof-of-concept to prove that it is possible.

Also please don't misunderstand me, Spring boot offers a lot of more things apart from self-contained application like monitoring, actuators, or artifact dependency resolution. But these things are only integration with other technologies, my example has been developed from zero in 1 hour and a half so don't expect to have a Spring boot ready to be used.

The first thing to choose is the application server to be used, and in this case there is no doubt that the best one for this task is Apache TomEE. It is a certified web profile Java EE server which takes 1 second to start up and works with default Java memory parameters.

So I added tomee dependencies in my pom.xml file.

In used tomee embedded version (1.7.1) you can only deploy applications contained inside a file, you cannot add for example a Servlet programmatically like it is done in Tomcat. This may change in near future of embedded tomee API, but for now we are going to use ShrinkWrap to create these deployment files in a programmatic way.

This is what we want to do:

Notice that we are only importing JavaEE classes and it is as reduced as Spring Boot one. In only 2 seconds the application is ready to be used. Keep in mind that you can use any feature provided by web profile spec as well as JAX-RS or JMS. So for example you can use JPA, Bean Validation, EJBs, CDI, ...

And what's inside TomEEApplication? You will be surprised a class with only 70 lines:

As you may see it is really simple piece of code and for example configuration or name of the application is hardcoded, but see that with several small easy changes you may start configuring server, application and so on.

In summary, of course Spring Boot is cool, but with really simple steps you can start having the same in JavaEE world. We (Apache TomEE contributors) are going to start to work on this and expand this idea. 

So don't underestimate Java EE because of Spring Boot.

We keep learning,
Do you believe in life after love, I can feel something inside me say, I really don't think you're strong enough, Now (Believe - Cher)

9 comentarios:

Zmirc dijo...

That's really cool. Thanks!

jlmonteiro dijo...

Gracias Alex.

LG OptimusV dijo...

The tomcat7-maven-plugin allows us to build an executable war/jar with an embedded Apache Tomcat7. So maybe the tomee-maven-plugin will have the same feature?

George Gastaldi dijo...

Have you seen JBoss Forge -http://forge.jboss.org ? It allows you to develop pure JavaEE apps out-of-box. Plus, it integrates with your IDE, so you can run commands as Wizard GUIs if you like

Unknown dijo...

yes I know JBoss Forge but the point here is to have a jar file light enough to be self container/executable. So the distribution of the whole application is only a matter of java -jar blablabla and that's all. Then you can adapt the microservices approach really fast.

tomee-maven-plugin does something similar but note that you cannot debug from your IDE nor test some minimal parts without running the maven plugin.

Moreover now this is only a proof of concept, currently we are working with something like tomee.deployClasspathAsWar() and this will effectively start a tomee and then use all loaded classpath as WAR file. So because tomee is a CDI/EJB/... container all classes will be scanned and registered correctly in required container.

Unknown dijo...

Great idea! That's definitely something I'd like to blog about. However, there seems to be a bit missing in your example. The REST service works fine, but how to run a servlet? The @WebServlet("/hello") annotation doesn't seem to be recognized.

Just in case you're interested in my code (well, basically it's your code :), it's here: https://github.com/stephanrauh/TomEE-Boot-Example

Unknown dijo...

Hi Stephan, thanks for reading the blog, well yes actually it is a proof of concept which means that a lot of required features are missing or not running as expected. But the good news is that from Apache TomEE we are working on this direction as well, we have just created a base classes which will allows us to start growing in the right direction (for example you will not need to depend on Shrinkwarp anymore). We have got a test where we have implemented using this approach a WebSocket example, EJB, REST, Servlet, JPA, ...

This was only one quick example nothing to be used in production, only some ideas, now we are working on the real thing :) Of course when we publish the code on github feel free to contribute, you are more than welcome.

Unknown dijo...

Hi Alex,

contributing to TomEE, and JavaEE Boot in particular? Sounds great. I'm game! I'll drop you a mail.


bobbyrhodes dijo...

This is the right blog for anyone who wants to find out about this topic. You realize so much its almost hard to argue with you (not that I actually would want…HaHa). You definitely put a new spin on a topic thats been written about for years. Gretuff, just great!

Click Here
Visit Web