Dropwizard: Expose a way to get http port in run()

Created on 17 Oct 2014  路  5Comments  路  Source: dropwizard/dropwizard

In dropwizard 0.6 you could get the http port from the configuration in the run() method with configuration.getHttpConfiguration().getPort(). In 0.7.x this isn't accessible.

We tried using jetty's connectors, but it hasn't started yet in run(), so we can't use that. Are there any other ways of getting the port?

All 5 comments

Hi,

This is not accessible in 0.7 because the server factory refactoring. Now there 2 types of servers:

  • default (separate ports for application and admin connectors)
  • simple (both connectors on the same port).

You can get the http port in this way:

  • For default server factory
int httpPort = 0;
DefaultServerFactory serverFactory = (DefaultServerFactory) configuration.getServerFactory();
for (ConnectorFactory connector : serverFactory.getApplicationConnectors()) {
    if (connector.getClass().isAssignableFrom(HttpConnectorFactory.class)) {
        httpPort = ((HttpConnectorFactory) connector).getPort();
        break;
    }
}
  • For simple server factory
int httpPort = 0;
SimpleServerFactory serverFactory = (SimpleServerFactory) configuration.getServerFactory();
HttpConnectorFactory connector = (HttpConnectorFactory) serverFactory.getConnector();
if (connector.getClass().isAssignableFrom(HttpConnectorFactory.class)) {
    httpPort = connector.getPort();
}

Technically, if a user specifies an httpPort of 0 for a random port, I had to something like this https://github.com/dropwizard/dropwizard-discovery/blob/master/src/main/java/io/dropwizard/discovery/core/CuratorAdvertisementListener.java to actually get the listening port from jetty upon startup.

True, that seems like a better approach.

Closing due to lack of feedback

Was this page helpful?
0 / 5 - 0 ratings

Related issues

animesh-sharama picture animesh-sharama  路  6Comments

mattnelson picture mattnelson  路  3Comments

Allsimon picture Allsimon  路  4Comments

zhenik picture zhenik  路  6Comments

jamisonhyatt picture jamisonhyatt  路  8Comments