我有一个Spring应用程序,并使用Tomcat开发它并在服务器上运行它。我非常沮丧的部署 - >取消部署 - >再次部署 - > ..开发过程中,所以我决定切换到嵌入式Jetty。所以基本上现在我只是一个单一的Java类,这是负责启动在我的服务器:启动嵌入式Jetty服务器作为后台进程
public class MainServer {
private Server start() throws Exception {
Server jetty = new Server();
String[] configFiles = { "WebContent/WEB-INF/jetty.xml" };
for (String configFile : configFiles) {
XmlConfiguration configuration = new XmlConfiguration(new File(configFile).toURI().toURL());
configuration.configure(jetty);
}
WebAppContext appContext = new WebAppContext();
File warPath = new File("WebContent");
appContext.setWar(warPath.getAbsolutePath());
appContext.setClassLoader(Thread.currentThread().getContextClassLoader());
appContext.setContextPath("/4d");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { appContext, new DefaultHandler() });
jetty.setHandler(handlers);
jetty.start();
jetty.join();
return jetty;
}
public static void main(String[] args) {
try {
new MainServer().start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是完美的发展,因为它允许热交换进行,似乎更快。不过,我也想稍后将此设置部署到我的服务器。启动此服务器并在后台运行它的最佳方式是什么(如Tomcat startup.sh)?我应该如何调用这个MainServer类?
好的,所以我最终做的就是放弃通过代码自己启动服务器,而是设置Maven来处理我的所有包装和构建。所以至少现在将应用程序部署到生产服务器并不是那么痛苦。我可能会在后面看看那个nohup,因为已经有很多话题了。 – semonte