Search in sources :

Example 6 with Managed

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();
    }
}
Also used : Timestamp(java.sql.Timestamp) Managed(io.dropwizard.lifecycle.Managed) Handle(org.skife.jdbi.v2.Handle) Before(org.junit.Before)

Example 7 with Managed

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);
}
Also used : SampleRepository(org.opennms.newts.api.SampleRepository) MetricRegistry(com.codahale.metrics.MetricRegistry) JmxReporter(com.codahale.metrics.JmxReporter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Indexer(org.opennms.newts.api.search.Indexer) Injector(com.google.inject.Injector) GraphiteListener(org.opennms.newts.graphite.GraphiteListener) Managed(io.dropwizard.lifecycle.Managed)

Example 8 with Managed

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));
}
Also used : ContainerLifeCycle(org.eclipse.jetty.util.component.ContainerLifeCycle) JettyManaged(io.dropwizard.lifecycle.JettyManaged) Managed(io.dropwizard.lifecycle.Managed) Test(org.junit.jupiter.api.Test)

Example 9 with 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();
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) MetricRegistry(com.codahale.metrics.MetricRegistry) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Environment(io.dropwizard.setup.Environment) Managed(io.dropwizard.lifecycle.Managed) Test(org.junit.jupiter.api.Test)

Aggregations

Managed (io.dropwizard.lifecycle.Managed)9 MetricRegistry (com.codahale.metrics.MetricRegistry)2 Timestamp (java.sql.Timestamp)2 Before (org.junit.Before)2 Test (org.junit.jupiter.api.Test)2 Handle (org.skife.jdbi.v2.Handle)2 JmxReporter (com.codahale.metrics.JmxReporter)1 Injector (com.google.inject.Injector)1 AlertJobResource (com.linkedin.thirdeye.anomaly.alert.AlertJobResource)1 AlertJobScheduler (com.linkedin.thirdeye.anomaly.alert.AlertJobScheduler)1 AlertJobSchedulerV2 (com.linkedin.thirdeye.anomaly.alert.v2.AlertJobSchedulerV2)1 DetectionJobResource (com.linkedin.thirdeye.anomaly.detection.DetectionJobResource)1 DetectionJobScheduler (com.linkedin.thirdeye.anomaly.detection.DetectionJobScheduler)1 AnomalyMergeExecutor (com.linkedin.thirdeye.anomaly.merge.AnomalyMergeExecutor)1 MonitorJobScheduler (com.linkedin.thirdeye.anomaly.monitor.MonitorJobScheduler)1 TaskDriver (com.linkedin.thirdeye.anomaly.task.TaskDriver)1 AlertFilterAutotuneFactory (com.linkedin.thirdeye.anomalydetection.alertFilterAutotune.AlertFilterAutotuneFactory)1 AutoLoadPinotMetricsService (com.linkedin.thirdeye.autoload.pinot.metrics.AutoLoadPinotMetricsService)1 DataCompletenessScheduler (com.linkedin.thirdeye.completeness.checker.DataCompletenessScheduler)1 AnomalyFunctionResource (com.linkedin.thirdeye.dashboard.resources.AnomalyFunctionResource)1