Search in sources :

Example 11 with Lifecycle

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();
    }
}
Also used : Module(com.google.inject.Module) Properties(java.util.Properties) Inject(com.google.inject.Inject) Test(org.junit.Test) Random(java.util.Random) Names(com.google.inject.name.Names) UUID(java.util.UUID) Field(java.lang.reflect.Field) Ints(com.google.common.primitives.Ints) GuiceInjectors(io.druid.guice.GuiceInjectors) ManageLifecycle(io.druid.guice.ManageLifecycle) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) Injector(com.google.inject.Injector) Initialization(io.druid.initialization.Initialization) JsonConfigurator(io.druid.guice.JsonConfigurator) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) ForkJoinPool(java.util.concurrent.ForkJoinPool) JsonConfigProvider(io.druid.guice.JsonConfigProvider) Map(java.util.Map) Assert(org.junit.Assert) Method(java.lang.reflect.Method) Before(org.junit.Before) Injector(com.google.inject.Injector) ManageLifecycle(io.druid.guice.ManageLifecycle) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) ManageLifecycle(io.druid.guice.ManageLifecycle) Test(org.junit.Test)

Example 12 with Lifecycle

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());
}
Also used : Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) Module(com.google.inject.Module) Test(org.junit.Test)

Example 13 with Lifecycle

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);
}
Also used : Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) Module(com.google.inject.Module) Test(org.junit.Test)

Example 14 with Lifecycle

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);
}
Also used : Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) Module(com.google.inject.Module) Test(org.junit.Test)

Example 15 with Lifecycle

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;
}
Also used : Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) PriorityBlockingQueue(java.util.concurrent.PriorityBlockingQueue) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Lifecycle (io.druid.java.util.common.lifecycle.Lifecycle)25 Injector (com.google.inject.Injector)10 Before (org.junit.Before)9 Test (org.junit.Test)8 Module (com.google.inject.Module)6 Binder (com.google.inject.Binder)5 NoopServiceEmitter (io.druid.server.metrics.NoopServiceEmitter)5 ExtractionNamespaceCacheFactory (io.druid.query.lookup.namespace.ExtractionNamespaceCacheFactory)4 IOException (java.io.IOException)4 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 ManageLifecycle (io.druid.guice.ManageLifecycle)3 ExtractionNamespace (io.druid.query.lookup.namespace.ExtractionNamespace)3 OnHeapNamespaceExtractionCacheManager (io.druid.server.lookup.namespace.cache.OnHeapNamespaceExtractionCacheManager)3 ExecutionException (java.util.concurrent.ExecutionException)3 Provides (com.google.inject.Provides)2 Self (io.druid.guice.annotations.Self)2 Sequence (io.druid.java.util.common.guava.Sequence)2 CountAggregatorFactory (io.druid.query.aggregation.CountAggregatorFactory)2 JDBCExtractionNamespace (io.druid.query.lookup.namespace.JDBCExtractionNamespace)2 URIExtractionNamespace (io.druid.query.lookup.namespace.URIExtractionNamespace)2