use of io.dropwizard.lifecycle.Managed in project dropwizard by dropwizard.
the class JDBITest 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 pinot by linkedin.
the class ThirdEyeAnomalyApplication method run.
@Override
public void run(final ThirdEyeAnomalyConfiguration config, final Environment environment) throws Exception {
LOG.info("Starting ThirdeyeAnomalyApplication : Scheduler {} Worker {}", config.isScheduler(), config.isWorker());
super.initDAOs();
ThirdEyeCacheRegistry.initializeCaches(config);
environment.lifecycle().manage(new Managed() {
@Override
public void start() throws Exception {
if (config.isWorker()) {
anomalyFunctionFactory = new AnomalyFunctionFactory(config.getFunctionConfigPath());
alertFilterFactory = new AlertFilterFactory(config.getAlertFilterConfigPath());
taskDriver = new TaskDriver(config, anomalyFunctionFactory, alertFilterFactory);
taskDriver.start();
}
if (config.isScheduler()) {
detectionJobScheduler = new DetectionJobScheduler();
alertFilterFactory = new AlertFilterFactory(config.getAlertFilterConfigPath());
alertFilterAutotuneFactory = new AlertFilterAutotuneFactory(config.getFilterAutotuneConfigPath());
detectionJobScheduler.start();
environment.jersey().register(new DetectionJobResource(detectionJobScheduler, alertFilterFactory, alertFilterAutotuneFactory));
environment.jersey().register(new AnomalyFunctionResource(config.getFunctionConfigPath()));
}
if (config.isMonitor()) {
monitorJobScheduler = new MonitorJobScheduler(config.getMonitorConfiguration());
monitorJobScheduler.start();
}
if (config.isAlert()) {
alertJobScheduler = new AlertJobScheduler();
alertJobScheduler.start();
// start alert scheduler v2
alertJobSchedulerV2 = new AlertJobSchedulerV2();
alertJobSchedulerV2.start();
environment.jersey().register(new AlertJobResource(alertJobScheduler, emailConfigurationDAO));
}
if (config.isMerger()) {
// anomalyFunctionFactory might have initiated if current machine is also a worker
if (anomalyFunctionFactory == null) {
anomalyFunctionFactory = new AnomalyFunctionFactory(config.getFunctionConfigPath());
}
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
anomalyMergeExecutor = new AnomalyMergeExecutor(executorService, anomalyFunctionFactory);
anomalyMergeExecutor.start();
}
if (config.isAutoload()) {
autoLoadPinotMetricsService = new AutoLoadPinotMetricsService(config);
autoLoadPinotMetricsService.start();
}
if (config.isDataCompleteness()) {
dataCompletenessScheduler = new DataCompletenessScheduler();
dataCompletenessScheduler.start();
}
}
@Override
public void stop() throws Exception {
if (config.isWorker()) {
taskDriver.stop();
}
if (config.isScheduler()) {
detectionJobScheduler.shutdown();
}
if (config.isMonitor()) {
monitorJobScheduler.stop();
}
if (config.isAlert()) {
alertJobScheduler.shutdown();
alertJobSchedulerV2.shutdown();
}
if (config.isMerger()) {
anomalyMergeExecutor.stop();
}
if (config.isAutoload()) {
autoLoadPinotMetricsService.shutdown();
}
if (config.isDataCompleteness()) {
dataCompletenessScheduler.shutdown();
}
}
});
}
use of io.dropwizard.lifecycle.Managed in project ratelimitj by mokies.
the class RateLimitApplication method run.
@Override
public void run(Configuration configuration, Environment environment) {
environment.jersey().register(new LoginResource());
environment.jersey().register(new UserResource());
environment.jersey().register(new TrekResource());
environment.jersey().register(new AuthDynamicFeature(new OAuthCredentialAuthFilter.Builder<PrincipalImpl>().setAuthenticator(new TestOAuthAuthenticator()).setPrefix("Bearer").buildAuthFilter()));
environment.jersey().register(RolesAllowedDynamicFeature.class);
environment.jersey().register(new AuthValueFactoryProvider.Binder<>(PrincipalImpl.class));
// TODO move this cleanup into the tests
environment.lifecycle().manage(new Managed() {
@Override
public void start() {
}
@Override
public void stop() {
flushRedis();
}
private void flushRedis() {
try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
connection.sync().flushdb();
}
redisClient.shutdownAsync();
}
});
}
use of io.dropwizard.lifecycle.Managed in project ratelimitj by mokies.
the class RateLimitBundle method run.
@Override
public void run(final Configuration configuration, final Environment environment) {
environment.jersey().register(new RateLimitingFactoryProvider.Binder(requestRateLimiterFactory));
environment.jersey().register(new RateLimited429EnforcerFeature());
environment.lifecycle().manage(new Managed() {
@Override
public void start() {
}
@Override
public void stop() throws Exception {
requestRateLimiterFactory.close();
}
});
}
use of io.dropwizard.lifecycle.Managed in project dropwizard by dropwizard.
the class JerseyClientBuilder method build.
private Client build(String name, ExecutorService threadPool, ObjectMapper objectMapper, Validator validator) {
if (!configuration.isGzipEnabled()) {
apacheHttpClientBuilder.disableContentCompression(true);
}
final Client client = ClientBuilder.newClient(buildConfig(name, threadPool, objectMapper, validator));
client.register(new JerseyIgnoreRequestUserAgentHeaderFilter());
// Tie the client to server lifecycle
if (environment != null) {
environment.lifecycle().manage(new Managed() {
@Override
public void start() throws Exception {
}
@Override
public void stop() throws Exception {
client.close();
}
});
}
if (configuration.isGzipEnabled()) {
client.register(new GZipDecoder());
client.register(new ConfiguredGZipEncoder(configuration.isGzipEnabledForRequests()));
}
return client;
}
Aggregations