Search code examples
javacloud-foundryhttplistenerundertow

Undertow Simple HelloWorld not working on Cloud Foundry


Im creating a simple undertow HTTP server following the documentation: http://undertow.io/undertow-docs/undertow-docs-1.3.0/index.html

   public static void main(final String[] args) {
      Undertow server = Undertow.builder()
         .addHttpListener(8080, "0.0.0.0")
         .setHandler(new HttpHandler() {
            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
               exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
               exchange.getResponseSender().send("Hello World");
            }
         }).build();
      server.start();
   }

It works normally on localhost. But when I deploy on Cloud Foundry as a standalone java app:

cf push im-gateway -p target\gateway.jar

The app fails to start and show this error in the log:

Instance (index 0) failed to start accepting connections

After investigation, I modify the push command:

cf push im-gateway -p target\gateway.jar --no-route

The deployment this time is successful, i create the route manually and try to access it but got the error:

502 Bad Gateway: Registered endpoint failed to handle the request.

Which port should I listen on ? How Cloud Foundry redirect the request to my app ?

I appreciate any reply.


Solution

  • According to the docs Cloud Foundry dynamically assigns a port to each app instance.

    https://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#PORT

    Try replacing .addHttpListener(8080, "0.0.0.0") with .addHttpListener(System.getenv("PORT"), "0.0.0.0")