Login
Pass
Always remember me
Categories

Programmatic hot deploy with Jetty

Hot deployment – which consists in deploying an application without stopping the application server – can save a lot of time and of course, avoid interruption of service.

You may already know it, but Bonita Open Solution embeds Jetty when it comes to deploying your process-driven application. I tried to programmatically hot deploy an application with Jetty a few days ago but unfortunately didn’t find any documentation on how to do this. After a lot of trial and error, I finally did it! I was mainly inspired from the depths of a mailing-list archive (BTW when will we have a Jetty forum?). Here is the results of my research…

I found several ways to use hot deployment with Jetty, including:

Context deployer. But this requires XML file configuration and replacing a war in a directory… and then waiting for a scan of the directory!!

OSGI service. This seems to be a great and easy solution but it also seems to be only available for Jetty 7.

And now it’s high time to show you how to use hot deployment programmatically:

Note: As Bonita Open Solution is based on Eclipse 3.5, we are using Jetty version 6.

  1. Create the server:
    1
    Server jetty = new Server();
  2. Create a handlerCollection with a ContextHandlerCollection in it:
    1
    2
    3
    4
    HandlerCollection hc = new HandlerCollection ();
    ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection();
    hc.setHandlers(new Handler[]{contextHandlerCollection});
    jetty.setHandler(hc);
  3. Start the server:
    1
    server.start();
  4. Hot deploy your web application:
    1
    2
    3
    4
    WebAppContext warWebappContext = new WebAppContext();
    //configure it ...
    contextHandlerCollection.addHandler(warWebappContext);
    warWebappContext.start();
  5. Hot undeploy your application:
    1
    2
    3
    4
    5
    6
    //get the WebAppContext to undeploy...
    WebAppContext toUndeploy = getWebAppContextToUndeploy();
    //stop it
    toUndeploy.stop();
    //and remove it from the handler collection
    contextHandlerCollection.removeHandler(toUndeploy);

In a nutshell, the trick is to use a HandlerCollection for your WebAppContextHandler.

I hope that this article will help others to hot deploy with Jetty. You’re welcome to  suggest better ways or to provide links to good documentation on this subject!

7 Responses

  1. #1 by Christopher Johnson on May 28th, 2010

    Quote

    I have solved the problem of true hot deployment during a debug session using Eclipse, Jetty, and Maven. You can find instructions here:

    http://www.clickonchris.com/2010/05/configuring-jetty-maven-and-eclipse-together-with-hot-deploy/

  2. #2 by Aurelien Pupier on May 29th, 2010

    Quote

    Hi,

    Thanks for your pointer.
    But you are doing an other thing. You are just running the Jetty server in remote debug mode. And not hot-deploying web application.

    Regards

  3. #3 by Sasha on July 9th, 2010

    Quote

    I try same thing but I found one problem…
    After redeploy I see increasing total loaded classes for approximation same count as my application and some additional threads…

  4. #4 by Aurelien Pupier on July 9th, 2010

    Quote

    Hi,

    take care to release objects of your web applications when they stop.(I think in particular to classloader).

    I can’t tell you more with no more precise information.

    BTW, if you encounter memory issue, I can suggest you to use MAT (Memory Analyzer Tool): http://www.eclipse.org/mat/

    Regards

  5. #5 by Sasha on July 10th, 2010

    Quote

    How I can know that it’s stop only application can release in correct way own objects

    mmm….

    Can I use destroy method of servlet for this job?..

  6. #6 by Aurelien Pupier on July 13th, 2010

    Quote

    Sasha :

    How I can know that it’s stop only application can release in correct way own objects

    You no more have “increasing total loaded classes for approximation same count as my application and some additional threads…” for instance :)

    Sasha :

    Can I use destroy method of servlet for this job?..

    It might be a good place.

  7. #7 by aqua on October 29th, 2010

    Quote

    Pls let me know how am I supposed to deploy a project in eclipse to knopflerfish osgi which contains jsp pages. I have included html,jsp n servlets but jsps r just not working. when I open my page from the web browser, the jsp functionality fails to work out. html n servlet running fine.

    regards