本文介绍了使用guice注入与actor抛出空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Schedular part:

  private ActorRef myActor = Akka.system()。actorOf(
new Props(Retreiver.class));

@Override
public void onStart(应用程序应用程序){
log.info(启动schedular .....!);
Akka.system()
.scheduler()
.schedule(Duration.create(0,TimeUnit.MILLISECONDS),
Duration.create(30,TimeUnit.MINUTES) myActor,tick,
Akka.system()。dispatcher());

}

Retreiver class part: / p>

  public class Retreiver extends UntypedActor {

private Logger.ALogger log = Logger.of(Retreiver .class );

@Inject
private myDataService dataService;

@Override
public void onReceive(Object arg0)throws Exception {

if(0!= dataService.getDataCount()){
.. ..
....
....
}

}



我为dataService变为空。请给我建议。



谢谢。

解决方案

因为Akka正在实例化您的 Retriever 演员而不是Guice,所以获得 NullPointerException 您需要让Guice构建您的实例,然后将其传递给Akka,可以帮助您实现这一点,例如:

  class RetrieverDependencyInjector实现了IndirectActorProducer {
final注射器;

public RetrieverDependencyInjector(注射器注射器){
this.injector = inject;
}

@Override
public Class<?延伸演员> actorClass(){
return Retriever.class;
}

@Override
public Retriever produce(){
return injector.getInstance(Retriever.class);
}
}

请注意, produce() 必须在每次调用实例时创建一个新的 Actor 实例,它不能返回相同的实例。

然后,您可以通过 RetrieverDependencyInjector 获取Akka来检索您的演员,例如:



pre> ActorRef myActor = Akka.system()。actorOf(
Props.create(RetrieverDependencyInjector.class,inject)
);

更新



我想到你进一步评论,你可能可以通过提供类别为$ code> RetrieverDependencyInjector 到一个 GenericDependencyInjector 您想要作为构造函数参数的 Actor ,这可能会允许您执行以下操作:

  Props.create(GenericDependencyInjector.class,inject,Retriever.class)

I没有尝试过,但可能会给您一个起点。


I'm getting null pointer exception on the field injection of a server which is started as an akka actor.

Schedular part:

private ActorRef myActor = Akka.system().actorOf(
        new Props(Retreiver.class));

@Override
public void onStart(Application app) {
    log.info("Starting schedular.....!");
    Akka.system()
            .scheduler()
            .schedule(Duration.create(0, TimeUnit.MILLISECONDS),
                    Duration.create(30, TimeUnit.MINUTES), myActor, "tick",
                    Akka.system().dispatcher());

}

Retreiver class part:

public class Retreiver extends UntypedActor {

private Logger.ALogger log = Logger.of(Retreiver .class);

@Inject
private myDataService dataService;

@Override
public void onReceive(Object arg0) throws Exception {

    if (0 != dataService.getDataCount()) {
    ....
    ....
    ....
    }

}

I'm getting null for dataService. Please advice me on this.

Thanks.

解决方案

You're getting the NullPointerException because Akka is instantiating your Retriever actor and not Guice. You need to get Guice to construct your instance and then pass that to Akka, IndirectActorProducer can help you achieve this, e.g.:

class RetrieverDependencyInjector implements IndirectActorProducer {
    final Injector injector;

    public RetrieverDependencyInjector(Injector injector) {
        this.injector = injector;
    }

    @Override
    public Class<? extends Actor> actorClass() {
        return Retriever.class;
    }

    @Override
    public Retriever produce() {
        return injector.getInstance(Retriever.class);
    }
}

Note that produce() must create a new Actor instance each time it is invoked, it cannot return the same instance.

You can then get Akka to retrieve your actor through the RetrieverDependencyInjector, e.g.:

ActorRef myActor = Akka.system().actorOf(
    Props.create(RetrieverDependencyInjector.class, injector)
);

UPDATE

I thought about you comment further, you might be able to turn RetrieverDependencyInjector into a GenericDependencyInjector by providing the class of the Actor you want as a constructor parameter, that perhaps will allow you to do something like:

Props.create(GenericDependencyInjector.class, injector, Retriever.class)

I haven't tried this, but it might give you a starting point.

这篇关于使用guice注入与actor抛出空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 22:38