【问题标题】:Starting an embedded Jetty server as a background process将嵌入式 Jetty 服务器作为后台进程启动
【发布时间】:2011-07-27 14:46:16
【问题描述】:

我有一个 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();
    }
}

}

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

【问题讨论】:

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

标签: java embedded-jetty


【解决方案1】:

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

nohup java [options] MainServer > nohup.out &

【讨论】:

    【解决方案2】:

    我建议使用start-stop-daemon 编写一个初始化脚本(查找 /etc/init.d/skeleton 作为起点)。遵循标准需要一些时间,但稍后会得到回报。

    多年来,我们一直在使用嵌入式码头和初始化脚本。它从未让我们失望。

    【讨论】:

      猜你喜欢
      • 2020-05-18
      • 1970-01-01
      • 2010-11-04
      • 2023-03-20
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      相关资源
      最近更新 更多