Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get application port programmatically in Dropwizard

Tags:

dropwizard

I am using dropwizard version 0.7.1. It is configured to use "random" (ephemeral?) port (server.applicationConnectors.port=0). I want to get what port is really in use after startup, but I can't find any information how to do that.

like image 348
tomekK Avatar asked Aug 28 '14 06:08

tomekK


People also ask

How do I change the Dropwizard port?

Add your configuration to the command line after server . See dropwizard.codahale.com/getting-started/#running-your-service for more information. It should have the desired effect. Perfect!

Does Dropwizard use Log4j?

Dropwizard uses Logback for its logging backend. It provides an slf4j implementation, and even routes all java. util. logging , Log4j, and Apache Commons Logging usage through Logback.

Does Dropwizard use Jetty?

Overview. Dropwizard is an open-source Java framework used for the fast development of high-performance RESTful web services. It gathers some popular libraries to create the light-weight package. The main libraries that it uses are Jetty, Jersey, Jackson, JUnit, and Guava.

What is bootstrap in Dropwizard?

Bootstrap is the pre-start (temp) application environment, containing everything required to bootstrap a Dropwizard command.


1 Answers

You can get a serverStarted callback from a lifecycle listener to figure this out.

@Override
public void run(ExampleConfiguration configuration, Environment environment) throws Exception {
  environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() {
    @Override
    public void serverStarted(Server server) {
      for (Connector connector : server.getConnectors()) {
        if (connector instanceof ServerConnector) {
          ServerConnector serverConnector = (ServerConnector) connector;
          System.out.println(serverConnector.getName() + " " + serverConnector.getLocalPort());
          // Do something useful with serverConnector.getLocalPort()
        }
      }
    }
  });
}
like image 118
condit Avatar answered Sep 20 '22 13:09

condit