简体   繁体   中英

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. 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. It's not acting as a web application or a web server. 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.

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.

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. Also, in that situation, a user has to manually start the "bot" process using the Heroku website or a command line tool.

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?

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. I can even use an undocumented app.json property to achieve this:

"formation": [
    { "process": "web",    "quantity": 0},
    { "process": "bot", "quantity": 1}
 ]

However, they still have to manually start the bot process. 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. For example:

web: sh start.sh

And then add a start.sh script to your project like this:

ruby -rwebrick -e'WEBrick::HTTPServer.new(:Port => ENV["PORT"], :DocumentRoot => Dir.pwd).start' &

java -jar myapp.jar

Note the "&" after the ruby command

In your Procfile, just name the process type something other than "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM