io.dropwizard.lifecycle.ServerLifecycleListener Java Examples

The following examples show how to use io.dropwizard.lifecycle.ServerLifecycleListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DropwizardModule.java    From dropwizard-guicier with Apache License 2.0 6 votes vote down vote up
@Override
public void configure(final Binder binder) {
  binder.bindListener(Matchers.any(), new ProvisionListener() {
    @Override
    public <T> void onProvision(ProvisionInvocation<T> provision) {
      Object obj = provision.provision();

      if (obj instanceof Managed) {
        handle((Managed) obj);
      }

      if (obj instanceof Task) {
        handle((Task) obj);
      }

      if (obj instanceof HealthCheck) {
        handle((HealthCheck) obj);
      }

      if (obj instanceof ServerLifecycleListener) {
        handle((ServerLifecycleListener) obj);
      }
    }
  });
}
 
Example #2
Source File: JAXWSBundleTest.java    From dropwizard-jaxws with Apache License 2.0 6 votes vote down vote up
@Test
public void initializeAndRun() {
    JAXWSBundle<?> jaxwsBundle = new JAXWSBundle<>("/soap", jaxwsEnvironment);

    try {
        jaxwsBundle.run(null, null);
    }
    catch (Exception e) {
        assertThat(e, is(instanceOf(IllegalArgumentException.class)));
    }

    jaxwsBundle.initialize(bootstrap);
    verify(jaxwsEnvironment).setInstrumentedInvokerBuilder(any(InstrumentedInvokerFactory.class));

    jaxwsBundle.run(null, environment);
    verify(servletEnvironment).addServlet(startsWith("CXF Servlet"), any(Servlet.class));
    verify(lifecycleEnvironment).addServerLifecycleListener(any(ServerLifecycleListener.class));
    verify(servlet).addMapping("/soap/*");
    verify(jaxwsEnvironment, never()).setPublishedEndpointUrlPrefix(anyString());
}
 
Example #3
Source File: JAXWSBundleTest.java    From dropwizard-jaxws with Apache License 2.0 6 votes vote down vote up
@Test
public void initializeAndRunWithPublishedEndpointUrlPrefix() {
    JAXWSBundle<?> jaxwsBundle = new JAXWSBundle<Configuration>("/soap", jaxwsEnvironment) {
        @Override
        protected String getPublishedEndpointUrlPrefix(Configuration configuration) {
            return "http://some/prefix";
        }
    };

    try {
        jaxwsBundle.run(null, null);
    }
    catch (Exception e) {
        assertThat(e, is(instanceOf(IllegalArgumentException.class)));
    }

    jaxwsBundle.initialize(bootstrap);
    verify(jaxwsEnvironment).setInstrumentedInvokerBuilder(any(InstrumentedInvokerFactory.class));

    jaxwsBundle.run(null, environment);
    verify(servletEnvironment).addServlet(startsWith("CXF Servlet"), any(Servlet.class));
    verify(lifecycleEnvironment).addServerLifecycleListener(any(ServerLifecycleListener.class));
    verify(servlet).addMapping("/soap/*");
    verify(jaxwsEnvironment).setPublishedEndpointUrlPrefix("http://some/prefix");
}
 
Example #4
Source File: LocalSchemaRegistryServer.java    From registry with Apache License 2.0 5 votes vote down vote up
@Override
public void run(RegistryConfiguration registryConfiguration, Environment environment) throws Exception {
    super.run(registryConfiguration, environment);
    environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() {
        @Override
        public void serverStarted(Server server) {
            localServer = server;
            LOG.info("Received callback as server is started :[{}]", server);
        }
    });
}
 
Example #5
Source File: EmoService.java    From emodb with Apache License 2.0 5 votes vote down vote up
private void evaluateServiceStartedListeners() throws Exception {
    final List<ServerStartedListener> listeners = _injector.getInstance(
            Key.get(new TypeLiteral<List<ServerStartedListener>>() {}));

    if (!listeners.isEmpty()) {
        _environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() {
            @Override
            public void serverStarted(Server server) {
                for (ServerStartedListener listener : listeners) {
                    listener.serverStarted();
                }
            }
        });
    }
}
 
Example #6
Source File: SundialBundleITBase.java    From dropwizard-sundial with Apache License 2.0 5 votes vote down vote up
@Override
public void run(TestConfiguration testConfiguration, Environment environment) throws Exception {
    environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() {
        @Override
        public void serverStarted(Server server) {
            SundialJobScheduler.addJob(JOB_NAME, TestJob.class);
        }
    });
}
 
Example #7
Source File: AbstractAppTest.java    From monasca-common with Apache License 2.0 5 votes vote down vote up
private void startIfRequired() {
  if (jettyServer != null) {
    return;
  }

  try {
    application = newApplication();

    final Bootstrap<C> bootstrap = new Bootstrap<C>(application) {
      @Override
      public void run(C configuration, Environment environment) throws Exception {
        environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() {
          @Override
          public void serverStarted(Server server) {
            jettyServer = server;
          }
        });
        AbstractAppTest.this.configuration = configuration;
        AbstractAppTest.this.environment = environment;
        super.run(configuration, environment);
      }
    };

    application.initialize(bootstrap);
    final ServerCommand<C> command = new ServerCommand<>(application);
    final Namespace namespace = new Namespace(ImmutableMap.<String, Object>of("file", configPath));
    command.run(bootstrap, namespace);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example #8
Source File: RegistryApplication.java    From registry with Apache License 2.0 4 votes vote down vote up
private void registerAndNotifyOtherServers(RegistryConfiguration configuration, Environment environment) {
    environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() {
        @Override
        public void serverStarted(Server server) {

            DefaultSchemaRegistry.Options options = null;

            for (ModuleConfiguration moduleConfiguration : configuration.getModules()) {
                if (moduleConfiguration.getClassName().equals("com.hortonworks.registries.schemaregistry.webservice.SchemaRegistryModule")) {
                   options = new DefaultSchemaRegistry.Options(moduleConfiguration.getConfig());
                   haServerNotificationManager.setIsCacheEnabled(options.isCacheEnabled());
                }
            }

            if (options.isCacheEnabled()) {
                LOG.debug("Enabled server side caching");

                String serverURL = server.getURI().toString();

                haServerNotificationManager.setHomeNodeURL(serverURL);

                try {
                    transactionManager.beginTransaction(TransactionIsolation.SERIALIZABLE);
                    HostConfigStorable hostConfigStorable = storageManager.get(new HostConfigStorable(serverURL).getStorableKey());
                    if (hostConfigStorable == null) {
                        storageManager.add(new HostConfigStorable(storageManager.nextId(HostConfigStorable.NAME_SPACE), serverURL,
                                System.currentTimeMillis()));
                    }
                    haServerNotificationManager.refreshServerInfo(storageManager.<HostConfigStorable>list(HostConfigStorable.NAME_SPACE));
                    transactionManager.commitTransaction();
                } catch (Exception e) {
                    transactionManager.rollbackTransaction();
                    throw e;
                }

                haServerNotificationManager.notifyDebut();

                refreshHAServerManagedTask = new RefreshHAServerManagedTask(storageManager, transactionManager, haServerNotificationManager);
                environment.lifecycle().manage(refreshHAServerManagedTask);
                refreshHAServerManagedTask.start();
            } else {
                LOG.debug("Disabled server side caching");
            }
        }
    });

}
 
Example #9
Source File: DropwizardModule.java    From dropwizard-guicier with Apache License 2.0 4 votes vote down vote up
private void handle(ServerLifecycleListener serverLifecycleListener) {
  environment.lifecycle().addServerLifecycleListener(serverLifecycleListener);
  LOG.info("Added guice injected server lifecycle listener: {}", serverLifecycleListener.getClass().getName());
}
 
Example #10
Source File: GuiceyEnvironment.java    From dropwizard-guicey with MIT License 2 votes vote down vote up
/**
 * Shortcut for {@link ServerLifecycleListener} registration.
 * <p>
 * Note that server listener is called only when jetty starts up and so will no be called with lightweight
 * guicey test helpers {@link ru.vyarus.dropwizard.guice.test.GuiceyAppRule} or
 * {@link ru.vyarus.dropwizard.guice.test.spock.UseGuiceyApp}. Prefer using
 * {@link #onApplicationStartup(ApplicationStartupListener)} to be correctly called in tests (of course, if not
 * server only execution is desired).
 * <p>
 * Obviously not called for custom command execution.
 *
 * @param listener server startup listener.
 * @return environment instance for chained calls
 */
public GuiceyEnvironment listenServer(final ServerLifecycleListener listener) {
    environment().lifecycle().addServerLifecycleListener(listener);
    return this;
}