2011-07-25 84 views
2

我有一个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类?

+0

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

回答

2

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

nohup java [options] MainServer > nohup.out & 
1

我建议写一个启动脚本(找/etc/init.d/skeleton为起点)使用start-stop-daemon。采用这个标准需要一些时间,但后来会有所收获。

我们现在使用嵌入式jetty和init脚本已有多年。它从来没有让我们失望。

相关问题