1
votes

I am trying to create a small web server using embedded Jetty. I build it into an "uberjar" using shade plugin for maven. This is my Startup class's main function:

BasicConfigurator.configure();

logger.info("Starting server");

URI baseUri = UriBuilder.fromUri("http://localhost").port(8080).build();
ResourceConfig config = new ResourceConfig(WindowAction.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config);

server.start();
server.join();
logger.info("Server started");

So far it works locally. I am able to build and run the server standalone locally:

java -jar target/server-0.jar

And the server responds requests to localhost:8080. Awesome. When I run:

heroku local web

Same, the server responds just fine. However, when I push it to Heroku remote, I get a port bind timeout error:

2017-08-08 22:12:52.396:INFO:oejs.Server:main: jetty-9.2.z-SNAPSHOT
2017-08-08T22:12:52.464157+00:00 app[web.1]: 2017-08-08 22:12:52.460:INFO:oejs.ServerConnector:main: Started ServerConnector@5890e879{HTTP/1.1}{0.0.0.0:8080}
2017-08-08T22:12:52.464414+00:00 app[web.1]: 2017-08-08 22:12:52.464:INFO:oejs.Server:main: Started @2891ms
2017-08-08T22:14:17.656011+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 90 seconds of launch
2017-08-08T22:14:17.656011+00:00 heroku[web.1]: Stopping process with SIGKILL
2017-08-08T22:14:17.792987+00:00 heroku[web.1]: Process exited with status 137
2017-08-08T22:14:17.810279+00:00 heroku[web.1]: State changed from starting to crashed

I have tried putting the port in a command line argument to java in the Procfile like so:

$JAVA_OPTS -Dserver.port=$PORT -jar target/myserver.jar

I also tried getting the server port at runtime:

System.getenv("port")

That evaluates to empty string. Maybe Heroku doesn't like to be told that the embedded server will use "localhost" but JettyHttpContainerFactory.createServer needs a URI. Can anyone point me to the proper way to make them play nice?

1

1 Answers

2
votes

The env var PORT is all caps:

System.getenv("PORT")

Here's a full example: https://github.com/kissaten/java-minimal/blob/master/src/main/java/Main.java#L63