Search in sources :

Example 1 with LifecycleEnvironment

use of io.dropwizard.lifecycle.setup.LifecycleEnvironment in project dropwizard by dropwizard.

the class CsvReporterFactoryTest method directoryCreatedOnStartup.

@Test
public void directoryCreatedOnStartup() throws Exception {
    File dir = new File("metrics");
    dir.delete();
    MetricsFactory config = factory.build(new File(Resources.getResource("yaml/metrics.yml").toURI()));
    config.configure(new LifecycleEnvironment(), new MetricRegistry());
    assertThat(dir.exists()).isEqualTo(true);
}
Also used : LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) MetricRegistry(com.codahale.metrics.MetricRegistry) File(java.io.File) Test(org.junit.Test)

Example 2 with LifecycleEnvironment

use of io.dropwizard.lifecycle.setup.LifecycleEnvironment in project dropwizard by dropwizard.

the class HttpClientBuilderTest method managedByEnvironment.

@Test
public 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.Test)

Example 3 with LifecycleEnvironment

use of io.dropwizard.lifecycle.setup.LifecycleEnvironment in project dropwizard by dropwizard.

the class JerseyIntegrationTest method configure.

@Override
protected Application configure() {
    forceSet(TestProperties.CONTAINER_PORT, "0");
    final MetricRegistry metricRegistry = new MetricRegistry();
    final SessionFactoryFactory factory = new SessionFactoryFactory();
    final DataSourceFactory dbConfig = new DataSourceFactory();
    final HibernateBundle<?> bundle = mock(HibernateBundle.class);
    final Environment environment = mock(Environment.class);
    final LifecycleEnvironment lifecycleEnvironment = mock(LifecycleEnvironment.class);
    when(environment.lifecycle()).thenReturn(lifecycleEnvironment);
    when(environment.metrics()).thenReturn(metricRegistry);
    dbConfig.setUrl("jdbc:hsqldb:mem:DbTest-" + System.nanoTime() + "?hsqldb.translate_dti_types=false");
    dbConfig.setUser("sa");
    dbConfig.setDriverClass("org.hsqldb.jdbcDriver");
    dbConfig.setValidationQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS");
    this.sessionFactory = factory.build(bundle, environment, dbConfig, ImmutableList.of(Person.class));
    try (Session session = sessionFactory.openSession()) {
        Transaction transaction = session.beginTransaction();
        session.createNativeQuery("DROP TABLE people IF EXISTS").executeUpdate();
        session.createNativeQuery("CREATE TABLE people (name varchar(100) primary key, email varchar(16), birthday timestamp with time zone)").executeUpdate();
        session.createNativeQuery("INSERT INTO people VALUES ('Coda', 'coda@example.com', '1979-01-02 00:22:00+0:00')").executeUpdate();
        transaction.commit();
    }
    final DropwizardResourceConfig config = DropwizardResourceConfig.forTesting(new MetricRegistry());
    config.register(new UnitOfWorkApplicationListener("hr-db", sessionFactory));
    config.register(new PersonResource(new PersonDAO(sessionFactory)));
    config.register(new JacksonMessageBodyProvider(Jackson.newObjectMapper()));
    config.register(new PersistenceExceptionMapper());
    config.register(new DataExceptionMapper());
    config.register(new EmptyOptionalExceptionMapper());
    return config;
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) MetricRegistry(com.codahale.metrics.MetricRegistry) EmptyOptionalExceptionMapper(io.dropwizard.jersey.optional.EmptyOptionalExceptionMapper) JacksonMessageBodyProvider(io.dropwizard.jersey.jackson.JacksonMessageBodyProvider) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Transaction(org.hibernate.Transaction) DropwizardResourceConfig(io.dropwizard.jersey.DropwizardResourceConfig) Environment(io.dropwizard.setup.Environment) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Session(org.hibernate.Session)

Example 4 with LifecycleEnvironment

use of io.dropwizard.lifecycle.setup.LifecycleEnvironment in project helios by spotify.

the class EventSenderFactory method build.

public static List<EventSender> build(final Environment environment, final CommonConfiguration<?> config, final MetricRegistry metricRegistry, final String pubsubHealthcheckTopic) {
    final List<EventSender> senders = new ArrayList<>();
    final KafkaClientProvider kafkaClientProvider = new KafkaClientProvider(config.getKafkaBrokers());
    final Optional<KafkaProducer<String, byte[]>> kafkaProducer = kafkaClientProvider.getDefaultProducer();
    kafkaProducer.ifPresent(producer -> senders.add(new KafkaSender(producer)));
    final LifecycleEnvironment lifecycle = environment.lifecycle();
    if (!config.getPubsubPrefixes().isEmpty()) {
        final PubSub pubsub = PubSubOptions.getDefaultInstance().getService();
        final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("pubsub-healthchecker-%d").build());
        // choose an arbitrary prefix to use in the healthcheck. we assume if we can connect to
        // one we can connect to all
        final String topicToHealthcheck = config.getPubsubPrefixes().iterator().next() + pubsubHealthcheckTopic;
        final GooglePubSubSender.DefaultHealthChecker healthchecker = new GooglePubSubSender.DefaultHealthChecker(pubsub, topicToHealthcheck, executor, Duration.ofMinutes(5));
        metricRegistry.register("pubsub-health", (Gauge<Boolean>) healthchecker::isHealthy);
        for (final String prefix : config.getPubsubPrefixes()) {
            final GooglePubSubSender sender = GooglePubSubSender.create(pubsub, prefix, healthchecker);
            senders.add(sender);
        }
        lifecycle.manage(new ManagedPubSub(pubsub));
    }
    senders.forEach(lifecycle::manage);
    return senders;
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) PubSub(com.google.cloud.pubsub.PubSub) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ArrayList(java.util.ArrayList) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder)

Aggregations

LifecycleEnvironment (io.dropwizard.lifecycle.setup.LifecycleEnvironment)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 Environment (io.dropwizard.setup.Environment)2 Test (org.junit.Test)2 PubSub (com.google.cloud.pubsub.PubSub)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 DataSourceFactory (io.dropwizard.db.DataSourceFactory)1 DropwizardResourceConfig (io.dropwizard.jersey.DropwizardResourceConfig)1 JacksonMessageBodyProvider (io.dropwizard.jersey.jackson.JacksonMessageBodyProvider)1 EmptyOptionalExceptionMapper (io.dropwizard.jersey.optional.EmptyOptionalExceptionMapper)1 Managed (io.dropwizard.lifecycle.Managed)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1