将嵌入式 Jetty 服务器作为后台进程启动
我有一个 Spring 应用程序,并使用 Tomcat 来开发它并在服务器上运行它。我对部署->取消部署-->再次部署-->..开发过程感到非常沮丧,所以我决定切换到嵌入式Jetty。所以基本上现在我只有一个 Java 类负责启动我的服务器:
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();
}
}
}
这非常适合开发,因为它允许热交换并且似乎更快。但是,我还想稍后将此设置部署到我的服务器。启动该服务器并在后台运行它的最佳方法是什么(例如 Tomcatstartup.sh)?我应该如何调用这个 MainServer 类?
I have a Spring application and have used Tomcat to develop it and run it on the server. I was quite frustrated with deploy->undeploy-->deploy again-->.. development process, so I decided to switch to embedded Jetty. So basically now I have just a single Java class that's responsible for starting my server:
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();
}
}
}
This is perfect for developing as it allows hotswapping and seems to be faster. However, I would also like to deploy this setting to my server later on. What is the best way to start this server and running it in the background (like Tomcat startup.sh)? How should I call this MainServer class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你提到了startup.sh,所以我想你的服务器是类似unix的。然后考虑使用 nohup 命令:
You mentioned startup.sh, so I suppose your server is unix like. Then consider using nohup command:
我建议使用 启动-停止-守护进程。遵循标准需要一些时间,但稍后就会得到回报。
多年来我们一直在使用嵌入式 jetty 和 init 脚本。它从未让我们失望过。
I recommend writing an initscript (look for /etc/init.d/skeleton as a starting point) using start-stop-daemon. Going with the standard takes some time but pays off later.
We are using embedded jetty and init scripts for years now. It has never let us down.