Heroku和Java –从新手到初学者,第2部分

问题

所以过了几天,我可以回到我的Recaps小项目。 我从检查日志开始,发现了以下内容:

2012-03-04T01:52:51+00:00 heroku[web.1]: Idling
2012-03-04T01:52:53+00:00 heroku[web.1]: Stopping process with SIGTERM
2012-03-04T01:53:03+00:00 heroku[web.1]: Error R12 (Exit timeout) -> Process failed to exit within 10 seconds of SIGTERM
2012-03-04T01:53:03+00:00 heroku[web.1]: Stopping process with SIGKILL

我不知道您的想法,但是每当我在日志中看到“错误”时,我都会感到担忧。 因此,我决定删除该讨厌的行。 发生的事情令人不愉快。

解决方案

这似乎是一个简单的问题。 我首先找到了这个SIGTERM的全部含义。 我知道这是Linux信号,我只是想知道Heroku实际在做什么。 因此,基本上,有时候Heroku只是将SIGTERM发送到您的进程,以便可以正常关闭它。 这很简单。

正如我在上一篇文章中提到的那样,我决定使用Jetty代替Grizzly。 最初,我决定使用jetty-runner运行我的Web应用程序,它运行良好,在Jersey Servlet启动时对资源进行了扫描。 部署到Heroku也很容易,并且通过修改Procfile可以启动应用程序。

但是,该应用程序对SIGTERM的反应不正确,因此在不深入研究jetty-runner配置的情况下,我决定只使用嵌入式Jetty服务器。 这非常简单,运行领班启动可以使应用程序真正启动。 因此,无需进一步考虑,我便将更改后的应用程序部署到了Heroku。 为了检查错误是否再次出现,在第一次启动后,我只是重启了heroku并连接到另一个终端上的日志。 但是退出超时错误消息仍然存在。 我在这里的错误–我没有测试应用程序在使用工头时是否会正确退出。 再说一次,领班开始然后是ctrl + c只是为了看看会发生什么(后来我试过kill -s TERM procand得到了类似的输出):

pbu@pbudesk ~/recaps $ foreman start
21:57:27 web.1     | started with pid 9603
21:57:27 web.1     | 0    [main] INFO  org.eclipse.jetty.server.Server  - jetty-8.1.1.v20120215
21:57:27 web.1     | 110  [main] INFO  org.eclipse.jetty.webapp.StandardDescriptorProcessor  - NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet
21:57:27 web.1     | 132  [main] INFO  org.eclipse.jetty.server.handler.ContextHandler  - started o.e.j.w.WebAppContext{/,file:/home/pbu/Devel/IdeaProjects/recaps/webmodule/src/main/webapp/},webmodule/src/main/webapp
21:57:27 web.1     | 133  [main] INFO  org.eclipse.jetty.server.handler.ContextHandler  - started o.e.j.w.WebAppContext{/,file:/home/pbu/Devel/IdeaProjects/recaps/webmodule/src/main/webapp/},webmodule/src/main/webapp
21:57:27 web.1     | 183  [main] INFO  org.eclipse.jetty.server.AbstractConnector  - Started SelectChannelConnector@0.0.0.0:5000
^CSIGINT received
21:57:57 system    | sending SIGTERM to all processes
21:57:57 system    | sending SIGTERM to pid 9603
21:57:57 web.1     | process terminated
pbu@pbudesk ~/recaps $

好的,因此当领班收到SIGINT时,它将SIGTERM发送到所有进程,很酷–也许Heroku测功机的行为相同。 不过,这并不是正常关机,但是Jetty的正常关机部分提到了两个不错的属性:gracefulShutdown和stopAtShutdown。 修改后的类如下所示:

public class Serve {
    public static void main(String[] args) throws Exception {
        int port = Integer.valueOf(System.getenv("PORT"));

        Server jetty = new Server(port);

        WebAppContext context = new WebAppContext();
        context.setContextPath("/");
        String webapp = "webmodule/src/main/webapp";
        context.setWar(webapp);
        context.setResourceBase(webapp);

        jetty.setHandler(context);

        jetty.setGracefulShutdown(1000);
        jetty.setStopAtShutdown(true);

        jetty.start();
        jetty.join();
    }
}

再次运行领班并使用ctrl + c证明此方法有效! 大!

pbu@pbudesk ~/recaps $ foreman start
22:11:47 web.1     | started with pid 9863
22:11:47 web.1     | 0    [main] INFO  org.eclipse.jetty.server.Server  - jetty-8.1.1.v20120215
22:11:47 web.1     | 110  [main] INFO  org.eclipse.jetty.webapp.StandardDescriptorProcessor  - NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet
22:11:47 web.1     | 131  [main] INFO  org.eclipse.jetty.server.handler.ContextHandler  - started o.e.j.w.WebAppContext{/,file:/home/pbu/Devel/IdeaProjects/recaps/webmodule/src/main/webapp/},webmodule/src/main/webapp
22:11:47 web.1     | 132  [main] INFO  org.eclipse.jetty.server.handler.ContextHandler  - started o.e.j.w.WebAppContext{/,file:/home/pbu/Devel/IdeaProjects/recaps/webmodule/src/main/webapp/},webmodule/src/main/webapp
22:11:48 web.1     | 183  [main] INFO  org.eclipse.jetty.server.AbstractConnector  - Started SelectChannelConnector@0.0.0.0:5000
^C22:11:49 web.1     | 1969 [Thread-1] INFO  org.eclipse.jetty.server.Server  - Graceful shutdown SelectChannelConnector@0.0.0.0:5000
22:11:49 web.1     | 1970 [Thread-1] INFO  org.eclipse.jetty.server.Server  - Graceful shutdown o.e.j.w.WebAppContext{/,file:/home/pbu/Devel/IdeaProjects/recaps/webmodule/src/main/webapp/},webmodule/src/main/webapp
SIGINT received
22:11:49 system    | sending SIGTERM to all processes
22:11:49 system    | sending SIGTERM to pid 9863
22:11:50 web.1     | 2982 [Thread-1] INFO  org.eclipse.jetty.server.handler.ContextHandler  - stopped o.e.j.w.WebAppContext{/,file:/home/pbu/Devel/IdeaProjects/recaps/webmodule/src/main/webapp/},webmodule/src/main/webapp
22:11:50 web.1     | process terminated
pbu@pbudesk ~/recaps $
So off to deploy it to the cloud! Again, deploy, heroku restart and watch logs... but it doesn't work.

不同的方式

最初失败后,我尝试了另一种方法。 我发现您可以注册关闭钩子-非常简单。 为此,只需使用Runtime.getRuntime()。addShutdownHook(Thread)方法注册一个新线程:

public class Serve {
    public static void main(String[] args) throws Exception {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                System.out.println("Shutting down by shutdown hook");
            }
        });
        int port = Integer.valueOf(System.getenv("PORT"));

        Server jetty = new Server(port);

        WebAppContext context = new WebAppContext();
        context.setContextPath("/");
        String webapp = "webmodule/src/main/webapp";
        context.setWar(webapp);
        context.setResourceBase(webapp);

        jetty.setHandler(context);

        jetty.setGracefulShutdown(1000);
        jetty.setStopAtShutdown(true);

        jetty.start();
        jetty.join();
    }
}

用工头进行的最终测试证明它也可以工作,但是再次在Heroku上不起作用。

在这一点上,我不知道如何摆脱超时。 这不是很重要,我只是想检查我是否可以对此做出反应,但无济于事。 现在,我想我只想联系Heroku,也许他们会帮助您。 另一种选择是尝试嵌入式Tomcat,但可能稍后再尝试。 现在,我还有其他事情要做,例如查看Jelastic


翻译自: https://www.javacodegeeks.com/2013/05/heroku-and-java-from-newbie-to-beginner-part-2.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值