将Webapp添加/删除到嵌入式Jetty [英] Adding/Removing a webapp to an embedded Jetty
问题描述
我已经启动了Jetty嵌入式服务器。
I have a Jetty embedded server started up.
我希望能够对Web应用程序进行热部署,并能够再次以编程方式将其卸载
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尝试使用ContextHandlerCollection,然后使用.addContext()使其启动并运行,但不确定这样做是否正确。
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
推荐答案
此热交换对我有用(Jetty 7)-此代码专用于交换在启动和循环时定义的Web Apps通过现有的处理程序。要动态添加新的Web App,您只需添加一些找到的标志逻辑。
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();
}
}
}
}
}
}
这篇关于将Webapp添加/删除到嵌入式Jetty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!