我正在使用Restlet with Guice。
CachedThreadPool在我的Guice中定义:
@Provides
@Singleton
@Named("name0")
public ExecutorService provideAutoDisconnectThreadPool () {
return Executors.newCachedThreadPool();
}
当服务器停止时想要关闭threadPool,所以在我的restlet.Application中,我使用inject来获取实例:
@Override
public void stop() throws Exception {
LOGGER.info("stopping...");
// shutdown threadPool
injector.getInstance(ExecutorService.class).shutdown();
super.stop();
LOGGER.info("stopped");
}
但是,该程序出现错误:
com.google.inject.ConfigurationException: Guice configuration errors:
1) No implementation for java.util.concurrent.ExecutorService was bound.
while locating java.util.concurrent.ExecutorService
1 error
at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1004)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1009)
那么,如何在应用程序停止时获取threadPool实例。
答案 0 :(得分:3)
Named
是binding annotation,因此在这种情况下注入密钥为ExecutorService.class
和 @Named("name0")
:
injector.getInstance(Key.get(ExecutorService.class, Names.named("name0")))