Tuesday, September 13, 2011

Modular Web Application based on JBoss Modules

Recently I read Why there is no standard for developing real modular web applications? by Patroklos Papapetrou. Inspired by this article I decided to check JBoss Modules in action. This post describes my experiment step by step.

I've started with following goal in mind - create web application using some service defined by my own JBoss module. Service prepared by me was pretty simple,  I named it Echo Service:

and placed into separate jar file, named echo-api. Then I implemented this service:

and placed the implementation in new jar file, named echo-module. Having in mind that my web application should have knowledge about the service API only, not the specific implementation, I decided to go the way described in Creating Extensible Applications With the Java Platform - that choice required adding to echo-module jar special file under: META-INF/services/warlock.echo.EchoService, holding the "pointer" to the service implementation (fully qualified name of implementing class).

At this point, I've retrieved and unpacked JBoss Application Server 7, stepped into unpacked JBoss, and then into modules directory. In this directory I've added following structure:

Two jar files visible here are mentioned above, module.xml file is the definition of my JBoss Module - named 'warlock.echo', and has following content:

After completion of JBoss Module definition, I've prepared simple Spring Framework based application (which uses echo-api jar only during the compilation of project and doesn't use the echo-module jar at all) with only one Controller:

As you see the Controller returns as response body the result of Echo Service call for some string. Now to the most important part - Echo Service definition in Web Application:

I know, one thing is bothering you :) - how the hell will Echo Service implementation be found if we don't add either echo-api and echo-module jars to the Web Application?!

Well, this is the beauty itself ;) - We just need one more thing - WEB-INF/jboss-deployment-structure.xml file:

This way we tell JBoss - this application depends on the 'warlock.echo' module and the services defined in this module. The rest is pure JBoss Module magic ;)

Lectures for the dessert: