[英]With Heroku, how can I automatically start a Java app that doesn't bind to a port when the app is deployed?
I have a Java application (packaged as a JAR) that interacts with a chat program (Slack) via websockets. 我有一个Java应用程序(打包为JAR),可通过websocket与聊天程序(Slack)进行交互。 As far as I understand it, my app doesn't need to bind to a specific port in order to work - it's just connecting to Slack's Real Time Messaging API.
据我了解,我的应用程序无需绑定到特定端口即可工作-只需连接到Slack的Real Time Messaging API。 It's not acting as a web application or a web server.
它不充当Web应用程序或Web服务器。 It's not listening on any ports for incoming requests because it doesn't need to.
它不需要监听任何端口上的传入请求,因为它不需要监听。 I want someone to be able to click on the "Deploy to Heroku" button for my application and I want the Java application to run as soon as the app is deployed, without a user having to manually turn on the process that starts the Java app.
我希望某人能够为我的应用程序单击“部署到Heroku”按钮,并且希望Java应用程序在部署该应用程序后立即运行,而无需用户手动打开启动Java应用程序的过程。
I've tried using "web" as the process type that starts my Java app (java -jar ...), but, as you can probably guess, since my app uses websockets, the app stops running after 60 seconds because Heroku detects that it failed to bind to a port. 我尝试使用“ web”作为启动我的Java应用程序的进程类型(java -jar ...),但是,您可能猜到了,因为我的应用程序使用了websockets,所以该应用程序在60秒后停止运行,因为Heroku检测到它无法绑定到端口。
I've tried using a different process name like "bot" as the process type, but, after a period of time, I get a "No web process running" error because no web process is running. 我尝试使用其他类型的进程名称(例如“ bot”)作为进程类型,但是一段时间后,由于没有Web进程在运行,因此出现“ No web process running”错误。 Also, in that situation, a user has to manually start the "bot" process using the Heroku website or a command line tool.
同样,在这种情况下,用户必须使用Heroku网站或命令行工具手动启动“机器人”过程。
Is there a way I can set it up so it doesn't complain that I haven't bound to aa port and that there isn't a web process, but that it also starts automatically when someone deploys it? 有没有一种我可以设置的方法,所以它不会抱怨我没有绑定到端口并且没有Web进程,但是它在有人部署时也会自动启动?
EDIT: I've discovered that I can get rid of the "No web process running" error by telling it to not expect any web dynos. 编辑:我发现我可以通过告诉它不要期望任何Web动态来摆脱“没有运行Web进程”错误。 I can even use an undocumented app.json property to achieve this:
我什至可以使用未记录的app.json属性来实现此目的:
"formation": [
{ "process": "web", "quantity": 0},
{ "process": "bot", "quantity": 1}
]
However, they still have to manually start the bot process. 但是,他们仍然必须手动启动Bot进程。 Also I'd prefer not using an undocumented property.
我也不想使用未记录的属性。 If I don't use it, they have to use the command line to modify the app after it is deployed.
如果我不使用它,则在部署应用程序后,他们必须使用命令行来修改该应用程序。 Still haven't found a way to get it to start automatically.
仍未找到使它自动启动的方法。
This is a bit of a hack, but you could use the "web" proc type and start a simple web server alongside your Java process. 这有点麻烦,但是您可以使用“ web” proc类型并在Java进程旁边启动一个简单的Web服务器。 For example:
例如:
web: sh start.sh
And then add a start.sh script to your project like this: 然后将start.sh脚本添加到您的项目中,如下所示:
ruby -rwebrick -e'WEBrick::HTTPServer.new(:Port => ENV["PORT"], :DocumentRoot => Dir.pwd).start' &
java -jar myapp.jar
Note the "&" after the ruby command 在ruby命令后注意“&”
In your Procfile, just name the process type something other than "web". 在您的Procfile中,只需将进程类型命名为“ web”以外的其他名称。 For example:
例如:
worker: java -jar myapp.jar
Then scale it up accordingly: 然后相应地扩大规模:
heroku ps:scale worker=1
Only "web" dynos are required to bind to a port. 仅需要“网络”测功机才能绑定到端口。
You can also scale it up from the dashboard. 您也可以从仪表板放大它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.