Guice DI in Callable

时间:2018-09-12 06:13:44

标签: java guice

我对在并行环境中使用Guice有一个特定的问题。

我有一个带有构造函数参数的可运行对象。

class task implements Runnable{
String dbname="";
task(String dbname)
{
this.dbname=dbname
}
@
Inject
private ICacheService cacheService
public void run(){
//logic here

}
bind(ICacheService).to(CacheServiceImpl.class);


}

问题:如何实例化任务。

我不能使用ExecutorService.submit(新的,因为这不是正确的方法,并且违反了guice的目的。)

2 个答案:

答案 0 :(得分:1)

我将创建一个创建任务的工厂,通过任务中的构造函数注入ICacheService并使用javax.inject.Provider

public class TaskFactory {

    private Provider<ICacheService> cacheServiceProvider;

    @Inject
    public TaskFactory(Provider<ICacheService> cacheServiceProvider) {
        this.cacheServiceProvider = cacheServiceProvider;
    }

    public task create(String dbname) {
        return new task(dbname, cacheServiceProvider.get());
    }
}


class task implements Runnable {

    private final String dbname;
    private final ICacheService cacheService;

    public task(String dbname, ICacheService cacheService) {
        this.dbname = dbname;
        this.cacheService = cacheService;
    }

    @Override
    public void run() {

    }
}

完成此操作后,将注入TaskFactory,然后使用dbName调用create方法。

您还可以使用Guice中的AssistedInject做类似的事情。

顺便说一句,类名称应以大写字母开头。

答案 1 :(得分:0)

经过一番谷歌搜索后,我找到了解决方案。 绑定:

public class DependecnyBinderModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(CloudSync.class).to(AWSCloudSyncImpl.class);

        bind(IConfigurationPropertyInitializer.class).to(ConfigurationPropertyInitializerImpl.class);

        bind(ICloudClientProvider.class).to(CloudClientProvider.class);

        bind(IQueryConnection.class).to(QueryConnectionImpl.class);

        bind(IWorkSpaceDescriptorInitialization.class).to(WorkSpaceDescriptorInitialize.class);
        bind(ISubscribeQueue.class).to(SubScribeQueueImpl.class);

        bind(InitializeDataSource.class).to(InitializeDataSourceImpl.class);

        bind(InitiateQueueListening.class).to(InitiateQueueListeningImpl.class);

        bind(DataExtractor.class).to(DataExtractorImpl.class);

        bind(DataTransformer.class).in(Scopes.SINGLETON);
        bind(TaskDispatcher.class);
        **install(new FactoryModuleBuilder().implement(Runnable.class, MultiQueueConsumer.class).build(TaskCreatorFactory.class));**

    }

    @Provides
    @Singleton
    ThreadFactory providesThreadFactory() {
        return new ThreadFactoryBuilder().setDaemon(true).setNameFormat("RealTimeSync-%d")
                .setUncaughtExceptionHandler(new RealTimeSyncExceptionHandler()).build();
    }



}

您的任务分派器将任务分派给执行器。

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import com.google.inject.Inject;

/**
 * @author marwaha
 *
 */
public class TaskDispatcher {
@Inject
private ThreadFactory factory;

@Inject

private RuunableFactory factory2;
    private ExecutorService executor;

    public void dispatch(String name) {
        if(executor==null) {
            executor=Executors.newFixedThreadPool(5, factory);
        }
        executor.submit(factory2.create(name));

    }

}

RunnableFactory

public interface RuunableFactory {

    Task create(String dbname);
}