enragedginger/akka-quartz-scheduler

Injecting class in Actor invoked by Akka-quartz-scheduler

ps3331333 opened this issue · 1 comments

I have the following actor in Play for Scala that injects a class with Guice:

class MainEtl extends Actor {

  @Inject val me2 : MainEtl2 = null

  def receive = {
    case option: String => {         
        val x = me2.run(option)
        // ... more code
        }
     } 
  }

When I try to schedule it I get an error as me2 is null:

[error] a.a.OneForOneStrategy - null
java.lang.NullPointerException: null
at tasks.etl.MainEtl$$anonfun$receive$1.applyOrElse(MainEtl.scala:17)
at akka.actor.Actor$class.aroundReceive(Actor.scala:484)
at tasks.etl.MainEtl.aroundReceive(MainEtl.scala:8)
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526)
at akka.actor.ActorCell.invoke(ActorCell.scala:495)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)
at akka.dispatch.Mailbox.run(Mailbox.scala:224)
at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)

This is how I invoke it:

         val scheduler = QuartzSchedulerExtension(system)
         val receiver = system.actorOf(Props[MainEtl])
         val d = scheduler.schedule("mycron", receiver, "abc", None)

How to get a reference to an injected object when the Actor is invoked from Akka-Quartz-Scheduler? I also tried injecting with @Inject as class parameters but that doesn't work either.

This is class MainEtl2 code:

class MainEtl2 @Inject() (ic: injectedClass) {
    def run (option: String) = {
      ic.method1() 
      // ... some code
    }
}

Hello,

I'm willing to bet that you're running into this error because Props[MainEtl] is creating a reference to the actor without going through Guice. Therefore, your @Inject annotations are irrelevant at runtime. You'll need to ensure that the actor reference passed to scheduler.schedule is created by Guice if you plan on using dependency injection with your actors.