问题
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