Search in sources :

Example 1 with ExecutorServiceProvider

use of org.glassfish.jersey.spi.ExecutorServiceProvider in project jersey by jersey.

the class ExecutorProviders method createInjectionBindings.

/**
     * Create qualified {@link java.util.concurrent.ExecutorService} and {@link java.util.concurrent.ScheduledExecutorService}
     * injection bindings based on the registered providers implementing the
     * {@link org.glassfish.jersey.spi.ExecutorServiceProvider} and/or
     * {@link org.glassfish.jersey.spi.ScheduledExecutorServiceProvider} SPI.
     * <p>
     * This method supports creation of qualified injection bindings based on custom
     * {@link javax.inject.Qualifier qualifier annotations} attached to the registered provider implementation classes
     * as well as named injection bindings based on the {@link javax.inject.Named} qualifier annotation attached to the
     * registered provider implementation classes.
     * </p>
     *
     * @param injectionManager application's injection manager.
     */
public static void createInjectionBindings(InjectionManager injectionManager) {
    /*
         * Add ExecutorService into DI framework.
         */
    Map<Class<? extends Annotation>, List<ExecutorServiceProvider>> executorProviderMap = getQualifierToProviderMap(injectionManager, ExecutorServiceProvider.class);
    for (Map.Entry<Class<? extends Annotation>, List<ExecutorServiceProvider>> qualifierToProviders : executorProviderMap.entrySet()) {
        Class<? extends Annotation> qualifierAnnotationClass = qualifierToProviders.getKey();
        Iterator<ExecutorServiceProvider> bucketProviderIterator = qualifierToProviders.getValue().iterator();
        ExecutorServiceProvider executorProvider = bucketProviderIterator.next();
        logExecutorServiceProvider(qualifierAnnotationClass, bucketProviderIterator, executorProvider);
        SupplierInstanceBinding<ExecutorService> descriptor = Bindings.supplier(new ExecutorServiceSupplier(executorProvider)).in(Singleton.class).to(ExecutorService.class);
        Annotation qualifier = executorProvider.getClass().getAnnotation(qualifierAnnotationClass);
        if (qualifier instanceof Named) {
            descriptor.named(((Named) qualifier).value());
        } else {
            descriptor.qualifiedBy(qualifier);
        }
        injectionManager.register(descriptor);
    }
    /*
         * Add ScheduledExecutorService into DI framework.
         */
    Map<Class<? extends Annotation>, List<ScheduledExecutorServiceProvider>> schedulerProviderMap = getQualifierToProviderMap(injectionManager, ScheduledExecutorServiceProvider.class);
    for (Map.Entry<Class<? extends Annotation>, List<ScheduledExecutorServiceProvider>> qualifierToProviders : schedulerProviderMap.entrySet()) {
        Class<? extends Annotation> qualifierAnnotationClass = qualifierToProviders.getKey();
        Iterator<ScheduledExecutorServiceProvider> bucketProviderIterator = qualifierToProviders.getValue().iterator();
        ScheduledExecutorServiceProvider executorProvider = bucketProviderIterator.next();
        logScheduledExecutorProvider(qualifierAnnotationClass, bucketProviderIterator, executorProvider);
        SupplierInstanceBinding<ScheduledExecutorService> descriptor = Bindings.supplier(new ScheduledExecutorServiceSupplier(executorProvider)).in(Singleton.class).to(ScheduledExecutorService.class);
        if (!executorProviderMap.containsKey(qualifierAnnotationClass)) {
            // it is safe to register binding for ExecutorService too...
            descriptor.to(ExecutorService.class);
        }
        Annotation qualifier = executorProvider.getClass().getAnnotation(qualifierAnnotationClass);
        if (qualifier instanceof Named) {
            descriptor.named(((Named) qualifier).value());
        } else {
            descriptor.qualifiedBy(qualifier);
        }
        injectionManager.register(descriptor);
    }
}
Also used : Named(javax.inject.Named) ScheduledExecutorServiceProvider(org.glassfish.jersey.spi.ScheduledExecutorServiceProvider) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Annotation(java.lang.annotation.Annotation) ExecutorServiceProvider(org.glassfish.jersey.spi.ExecutorServiceProvider) ScheduledExecutorServiceProvider(org.glassfish.jersey.spi.ScheduledExecutorServiceProvider) Singleton(javax.inject.Singleton) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ExecutorServiceProvider

use of org.glassfish.jersey.spi.ExecutorServiceProvider in project dropwizard by dropwizard.

the class DropwizardExecutorProviderTest method shutsDownDisposableExecutorService.

@Test
public void shutsDownDisposableExecutorService() {
    final ExecutorService executor = Executors.newSingleThreadExecutor();
    final ExecutorService disposableExecutor = new DropwizardExecutorProvider.DisposableExecutorService(executor);
    final ExecutorServiceProvider provider = new DropwizardExecutorProvider(disposableExecutor, SHUTDOWN_TIME);
    assertThat(executor.isShutdown()).isFalse();
    provider.dispose(disposableExecutor);
    assertThat(executor.isShutdown()).isTrue();
}
Also used : ExecutorServiceProvider(org.glassfish.jersey.spi.ExecutorServiceProvider) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 3 with ExecutorServiceProvider

use of org.glassfish.jersey.spi.ExecutorServiceProvider in project dropwizard by dropwizard.

the class DropwizardExecutorProviderTest method doesntShutDownNonDisposableExecutorService.

@Test
public void doesntShutDownNonDisposableExecutorService() {
    final ExecutorService executor = Executors.newSingleThreadExecutor();
    final ExecutorServiceProvider provider = new DropwizardExecutorProvider(executor, SHUTDOWN_TIME);
    assertThat(executor.isShutdown()).isFalse();
    provider.dispose(executor);
    assertThat(executor.isShutdown()).isFalse();
    executor.shutdown();
}
Also used : ExecutorServiceProvider(org.glassfish.jersey.spi.ExecutorServiceProvider) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Aggregations

ExecutorService (java.util.concurrent.ExecutorService)3 ExecutorServiceProvider (org.glassfish.jersey.spi.ExecutorServiceProvider)3 Test (org.junit.Test)2 Annotation (java.lang.annotation.Annotation)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Named (javax.inject.Named)1 Singleton (javax.inject.Singleton)1 ScheduledExecutorServiceProvider (org.glassfish.jersey.spi.ScheduledExecutorServiceProvider)1