use of io.dropwizard.lifecycle.Managed in project dropwizard by dropwizard.
the class GuavaJDBITest method setUp.
@Before
public void setUp() throws Exception {
when(environment.healthChecks()).thenReturn(healthChecks);
when(environment.lifecycle()).thenReturn(lifecycleEnvironment);
when(environment.metrics()).thenReturn(metricRegistry);
when(environment.getHealthCheckExecutorService()).thenReturn(Executors.newSingleThreadExecutor());
this.dbi = factory.build(environment, hsqlConfig, "hsql");
final ArgumentCaptor<Managed> managedCaptor = ArgumentCaptor.forClass(Managed.class);
verify(lifecycleEnvironment).manage(managedCaptor.capture());
managed.addAll(managedCaptor.getAllValues());
for (Managed obj : managed) {
obj.start();
}
try (Handle handle = dbi.open()) {
handle.createCall("DROP TABLE people IF EXISTS").invoke();
handle.createCall("CREATE TABLE people (name varchar(100) primary key, email varchar(100), age int, created_at timestamp)").invoke();
handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Coda Hale").bind(1, "chale@yammer-inc.com").bind(2, 30).bind(3, new Timestamp(1365465078000L)).execute();
handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Kris Gale").bind(1, "kgale@yammer-inc.com").bind(2, 32).bind(3, new Timestamp(1365465078000L)).execute();
handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Old Guy").bindNull(1, Types.VARCHAR).bind(2, 99).bind(3, new Timestamp(1365465078000L)).execute();
handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Alice Example").bind(1, "alice@example.org").bind(2, 99).bindNull(3, Types.TIMESTAMP).execute();
}
}
use of io.dropwizard.lifecycle.Managed in project newts by OpenNMS.
the class NewtsService method run.
@Override
public void run(final NewtsConfig config, Environment environment) throws Exception {
// Filters
configureCors(environment);
configureUIRedirect(environment);
configureAuthentication(environment, config);
final Injector injector = Guice.createInjector(new NewtsGuiceModule(), new CassandraGuiceModule(config), new GraphiteGuiceModule(config));
MetricRegistry metricRegistry = injector.getInstance(MetricRegistry.class);
// Create/start a JMX reporter for our MetricRegistry
final JmxReporter reporter = JmxReporter.forRegistry(metricRegistry).inDomain("newts").build();
// Create (and start if so configured), a Graphite line-protocol listener
final GraphiteListenerThread listener = new GraphiteListenerThread(injector.getInstance(GraphiteListener.class));
environment.lifecycle().manage(new Managed() {
@Override
public void stop() throws Exception {
reporter.stop();
}
@Override
public void start() throws Exception {
reporter.start();
if (config.getGraphiteConfig().isEnabled()) {
listener.start();
}
}
});
SampleRepository repository = injector.getInstance(SampleRepository.class);
Indexer indexer = injector.getInstance(Indexer.class);
// Rest resources
environment.jersey().register(new MeasurementsResource(repository, config.getReports()));
environment.jersey().register(new SamplesResource(repository, indexer));
// Add search resource only if search is enabled
if (config.getSearchConfig().isEnabled()) {
environment.jersey().register(new SearchResource(injector.getInstance(Searcher.class)));
}
// Health checks
environment.healthChecks().register("repository", new RepositoryHealthCheck(repository));
// Mapped exceptions
environment.jersey().register(IllegalArgumentExceptionMapper.class);
}
use of io.dropwizard.lifecycle.Managed in project dropwizard by dropwizard.
the class LifecycleEnvironmentTest method managesManagedObjects.
@Test
void managesManagedObjects() {
final Managed managed = mock(Managed.class);
environment.manage(managed);
final ContainerLifeCycle container = new ContainerLifeCycle();
environment.attach(container);
assertThat(container.getBeans()).singleElement().isInstanceOfSatisfying(JettyManaged.class, jettyManaged -> assertThat(jettyManaged.getManaged()).isSameAs(managed));
}
use of io.dropwizard.lifecycle.Managed in project dropwizard by dropwizard.
the class HttpClientBuilderTest method managedByEnvironment.
@Test
void managedByEnvironment() throws Exception {
final Environment environment = mock(Environment.class);
when(environment.getName()).thenReturn("test-env");
when(environment.metrics()).thenReturn(new MetricRegistry());
final LifecycleEnvironment lifecycle = mock(LifecycleEnvironment.class);
when(environment.lifecycle()).thenReturn(lifecycle);
final CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
HttpClientBuilder httpClientBuilder = spy(new HttpClientBuilder(environment));
when(httpClientBuilder.buildWithDefaultRequestConfiguration("test-apache-client")).thenReturn(new ConfiguredCloseableHttpClient(httpClient, RequestConfig.DEFAULT));
assertThat(httpClientBuilder.build("test-apache-client")).isSameAs(httpClient);
// Verify that we registered the managed object
final ArgumentCaptor<Managed> argumentCaptor = ArgumentCaptor.forClass(Managed.class);
verify(lifecycle).manage(argumentCaptor.capture());
// Verify that the managed object actually stops the HTTP client
final Managed managed = argumentCaptor.getValue();
managed.stop();
verify(httpClient).close();
}
Aggregations