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);
}
}
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();
}
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();
}
Aggregations