Adding/Removing a webapp to an embedded Jetty

℡╲_俬逩灬. 提交于 2019-12-11 03:14:48

问题


I have a Jetty embedded server started up.

I wish to be able to do a hot deploy of a webapp and be able to unload it again, all programmatically.

Once the server is started, any attempts to add a handler to it throws an error.

I tried using ContextHandlerCollection and then using .addContext() to get it up and running but not sure if that is the right way to go about it.

Can someone please point me in the right direction? thank you


回答1:


This hotswap works for me (Jetty 7) - this code is specific for swapping Web Apps defined at startup and loops through the existing handlers. To add a new Web App dynamically, you would just have to add some found flag logic. HTH.

public void updateWar(String contextPath, String warPath)
{  
Handler[] hs = handlers.getHandlers();
for(int i = 0; i < hs.length; i++)
{
    Handler h = hs[i];
    if(h != null)
    {
        if(h instanceof WebAppContext)
        {
            WebAppContext wac = (WebAppContext)h;
            String wacwar = wac.getWar();

            if(wacwar.equals( warPath ))
            {
                try
                {
                    handlers.stop();
                    wac.stop();
                    wac.destroy();                          
                    handlers.removeHandler(wac);
                    wac = new WebAppContext(); 
                    wac.setContextPath(contextPath);
                    wac.setWar(warPath);
                    handlers.addHandler(wac);
                    handlers.start();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
}
}


来源:https://stackoverflow.com/questions/7909100/adding-removing-a-webapp-to-an-embedded-jetty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!