java heroku_Heroku和Java –从新手到初学者,第2部分
java heroku
問題
所以過了幾天,我可以回到我的Recaps小項目。 我從檢查日志開始,發(fā)現(xiàn)了以下內容:
我不知道您的想法,但是每當我在日志中看到“錯誤”時,我都會感到擔憂。 因此,我決定刪除該討厭的行。 發(fā)生的事情令人不愉快。
解決方案
這似乎是一個簡單的問題。 我首先找到了SIGTERM的全部含義。 我知道這是Linux信號,我只是想知道Heroku實際在做什么。 因此,基本上,有時候Heroku只是將SIGTERM發(fā)送到您的進程,以便允許它正常關閉。 這很簡單。
正如我在上一篇文章中提到的那樣,我決定使用Jetty代替Grizzly。 最初,我決定使用jetty-runner運行我的Web應用程序,它運行良好,在Jersey Servlet啟動時對資源進行了掃描。 部署到Heroku也很容易,并且通過修改Procfile可以啟動應用程序。
但是,該應用程序對SIGTERM的React不正確,因此在不深入研究jetty-runner配置的情況下,我決定只使用嵌入式Jetty服務器。 這非常簡單,運行領班啟動可以使應用程序真正啟動。 因此,無需進一步考慮,我便將更改后的應用程序部署到了Heroku。 為了檢查錯誤是否再次出現(xiàn),在第一次啟動后,我只是重啟了heroku并連接到另一個終端上的日志。 但是退出超時錯誤消息仍然存在。 我在這里的錯誤–我沒有測試應用程序在使用工頭時是否會正確退出。 再次,領班開始,然后再按ctrl + c看看會發(fā)生什么(后來我嘗試使用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發(fā)送到所有進程,很酷–也許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.不同的方式
最初失敗后,我嘗試了另一種方法。 我發(fā)現(xiàn)您可以注冊關閉鉤子-非常簡單。 為此,只需使用Runtime.getRuntime()。addShutdownHook(Thread)方法注冊一個新線程:
public class Serve {public static void main(String[] args) throws Exception {Runtime.getRuntime().addShutdownHook(new Thread() {@Overridepublic 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上不起作用。
在這一點上,我不知道如何擺脫超時。 這不是很重要,我只想檢查我是否可以對此做出React,但無濟于事。 現(xiàn)在,我想我只想聯(lián)系Heroku,也許他們會幫助您。 另一種選擇是嘗試嵌入式Tomcat,但可能稍后再嘗試。 現(xiàn)在,我還有其他事情要做,例如查看Jelastic 。
翻譯自: https://www.javacodegeeks.com/2013/05/heroku-and-java-from-newbie-to-beginner-part-2.html
java heroku
總結
以上是生活随笔為你收集整理的java heroku_Heroku和Java –从新手到初学者,第2部分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全国备案查询系统(全国备案系统)
- 下一篇: 轩辕剑天之痕安卓版(轩辕剑天之痕安卓)