2010年5月18日 星期二

Programmatic hot deploy with Jetty

  1. Create the server:
    Server jetty = new Server();

     

  2. Create a handlerCollection with a ContextHandlerCollection in it:
    HandlerCollection hc = new HandlerCollection ();
    ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection();
    hc.setHandlers(new Handler[]{contextHandlerCollection});
    jetty.setHandler(hc);

     

  3. Start the server:
    server.start();

     

  4. Hot deploy your web application:
    WebAppContext warWebappContext = new WebAppContext();
    //configure it ...
    contextHandlerCollection.addHandler(warWebappContext);
    warWebappContext.start();

     

  5. Hot undeploy your application:
    //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.
    source: http://www.bonitasoft.org/blog/tutorial/programmatic-hot-deploy-with-jetty/