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?
Hi,
This is not accessible in 0.7 because the server factory refactoring. Now there 2 types of servers:
You can get the http port in this way:
int httpPort = 0;
DefaultServerFactory serverFactory = (DefaultServerFactory) configuration.getServerFactory();
for (ConnectorFactory connector : serverFactory.getApplicationConnectors()) {
if (connector.getClass().isAssignableFrom(HttpConnectorFactory.class)) {
httpPort = ((HttpConnectorFactory) connector).getPort();
break;
}
}
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
If anyone happens to run into this issue, we did add https://github.com/dropwizard/dropwizard/blob/master/dropwizard-lifecycle/src/main/java/io/dropwizard/lifecycle/ServerLifecycleListener.java#L19 in https://github.com/dropwizard/dropwizard/pull/1603 to make this easier.
Most helpful comment
If anyone happens to run into this issue, we did add https://github.com/dropwizard/dropwizard/blob/master/dropwizard-lifecycle/src/main/java/io/dropwizard/lifecycle/ServerLifecycleListener.java#L19 in https://github.com/dropwizard/dropwizard/pull/1603 to make this easier.