use of io.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.
the class CaffeineCacheProviderWithConfig method testBasicInjection.
@Test
public void testBasicInjection() throws Exception {
final CaffeineCacheConfig config = new CaffeineCacheConfig();
Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.<Module>of(binder -> {
binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test/redis");
binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
binder.bind(CaffeineCacheConfig.class).toInstance(config);
binder.bind(Cache.class).toProvider(CaffeineCacheProviderWithConfig.class).in(ManageLifecycle.class);
}));
final Lifecycle lifecycle = injector.getInstance(Lifecycle.class);
lifecycle.start();
try {
Cache cache = injector.getInstance(Cache.class);
Assert.assertEquals(CaffeineCache.class, cache.getClass());
} finally {
lifecycle.stop();
}
}
use of io.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.
the class LifecycleScopeTest method testAnnotatedAndExplicit.
/**
* This is a test for documentation purposes. It's there to show what weird things Guice will do when
* it sees both the annotation and an explicit binding.
*
* @throws Exception
*/
@Test
public void testAnnotatedAndExplicit() throws Exception {
final Injector injector = Guice.createInjector(new DruidGuiceExtensions(), new LifecycleModule(), new Module() {
@Override
public void configure(Binder binder) {
binder.bind(TestInterface.class).to(AnnotatedClass.class).in(ManageLifecycle.class);
}
});
final Lifecycle lifecycle = injector.getInstance(Lifecycle.class);
final TestInterface instance = injector.getInstance(TestInterface.class);
Assert.assertEquals(0, instance.getStarted());
Assert.assertEquals(0, instance.getStopped());
Assert.assertEquals(0, instance.getRan());
instance.run();
Assert.assertEquals(0, instance.getStarted());
Assert.assertEquals(0, instance.getStopped());
Assert.assertEquals(1, instance.getRan());
lifecycle.start();
Assert.assertEquals(2, instance.getStarted());
Assert.assertEquals(0, instance.getStopped());
Assert.assertEquals(1, instance.getRan());
// It's a singleton
injector.getInstance(TestInterface.class).run();
Assert.assertEquals(2, instance.getStarted());
Assert.assertEquals(0, instance.getStopped());
Assert.assertEquals(2, instance.getRan());
lifecycle.stop();
Assert.assertEquals(2, instance.getStarted());
Assert.assertEquals(2, instance.getStopped());
Assert.assertEquals(2, instance.getRan());
}
use of io.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.
the class LifecycleScopeTest method testExplicit.
@Test
public void testExplicit() throws Exception {
final Injector injector = Guice.createInjector(new DruidGuiceExtensions(), new LifecycleModule(), new Module() {
@Override
public void configure(Binder binder) {
binder.bind(TestInterface.class).to(ExplicitClass.class).in(ManageLifecycle.class);
}
});
final Lifecycle lifecycle = injector.getInstance(Lifecycle.class);
final TestInterface instance = injector.getInstance(TestInterface.class);
testIt(injector, lifecycle, instance);
}
use of io.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.
the class LifecycleScopeTest method testAnnotation.
@Test
public void testAnnotation() throws Exception {
final Injector injector = Guice.createInjector(new DruidGuiceExtensions(), new LifecycleModule(), new Module() {
@Override
public void configure(Binder binder) {
binder.bind(TestInterface.class).to(AnnotatedClass.class);
}
});
final Lifecycle lifecycle = injector.getInstance(Lifecycle.class);
final TestInterface instance = injector.getInstance(TestInterface.class);
testIt(injector, lifecycle, instance);
}
use of io.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.
the class PrioritizedListenableFutureTask method create.
public static PrioritizedExecutorService create(Lifecycle lifecycle, DruidProcessingConfig config) {
final PrioritizedExecutorService service = new PrioritizedExecutorService(new ThreadPoolExecutor(config.getNumThreads(), config.getNumThreads(), 0L, TimeUnit.MILLISECONDS, new PriorityBlockingQueue<Runnable>(), new ThreadFactoryBuilder().setDaemon(true).setNameFormat(config.getFormatString()).build()), config);
lifecycle.addHandler(new Lifecycle.Handler() {
@Override
public void start() throws Exception {
}
@Override
public void stop() {
service.shutdownNow();
}
});
return service;
}
Aggregations