Java 启动嵌入式Jetty服务器作为后台进程

Java 启动嵌入式Jetty服务器作为后台进程,java,embedded-jetty,Java,Embedded Jetty,我有一个Spring应用程序,使用Tomcat开发并在服务器上运行它。我对deploy->UndeDeploy-->再次部署-->感到非常失望。。开发过程中,所以我决定切换到嵌入式Jetty。所以基本上现在我只有一个Java类负责启动我的服务器: public class MainServer { private Server start() throws Exception { Server jetty = new Server(); String[] configFiles

我有一个Spring应用程序,使用Tomcat开发并在服务器上运行它。我对deploy->UndeDeploy-->再次部署-->感到非常失望。。开发过程中,所以我决定切换到嵌入式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();
    }
}

}

这非常适合开发,因为它允许热交换,而且似乎更快。但是,我也希望稍后将此设置部署到我的服务器。启动此服务器并在后台运行它(如Tomcat startup.sh)的最佳方式是什么?我应该如何称呼这个MainServer类?

您提到startup.sh,所以我认为您的服务器类似于unix。然后考虑使用命令:


我建议使用编写initscript(以/etc/init.d/skeleton为起点)。遵循标准需要一段时间,但稍后会有回报


我们使用嵌入式jetty和init脚本已有多年了。它从未让我们失望。

好吧,所以我最终做的是放弃自己编写代码启动服务器,而是设置Maven来处理我所有的打包和构建。因此,至少现在将应用程序部署到生产服务器并没有那么痛苦。稍后我可能会研究这个nohup,因为已经有很多关于它的话题了。
nohup java [options] MainServer > nohup.out &