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:


  1. Isn't spring dm ( now gemini project) and osgi a better way to go ?

  2. Sorry I should have read the article you were referring to :-)

  3. Hi
    Nice to see some interesting and simple examples with JBoss Modules. I will try to make something similar to see if it works with JSF2 :)

  4. Hi warlock,

    Nice blog! Is there an email I can contact you in private?

  5. Hi Warlock thanks for a sample on Jboss-Modules, really missing everywhere.

    Hope I does not bother you, I was trying to create a very basic sample
    (quite similar to yours) but in a standalone fashion.

    Project A - creates echo impl & api
    Project B - uses module in project A

    Seems a lot of basic errors very difficult to trace or understand, perhaps you tried a similar sample ?

    Just trying to understand JbossModules by itself.
    thanks very much