F. Lun
F. Lun

Reputation: 15

Akka scheduler not completing

I need to send a message to an actor at specific intervals. I am using the following code:

object SendToActor extends App {

  import Sender._

  val system: ActorSystem = ActorSystem("sender")

  try {
    val senderActor: ActorRef = system.actorOf(Sender.props, "sendActor")
    val sendSchedule =
      system.scheduler.schedule(0 milliseconds, 5 minutes, senderActor, doSomething())
  } finally {
    system.terminate()
  }
}

Unfortunately, the scheduler doesn't seem to run unless I do one of the following:

  1. Put a readLine() right after it:

    val sendSchedule = system.scheduler.schedule(0 milliseconds, 5 minutes, senderActor, doSomething())
    readLine()
    
  2. Put a Thread.sleep() right after it:

    val sendSchedule = system.scheduler.schedule(0 milliseconds, 5 minutes, senderActor, doSomething())
    Thread.sleep(10000)
    

Is there a reason why the scheduler won't run as coded above? Why does it require the sleep in order to work?

Upvotes: 0

Views: 381

Answers (1)

Jeffrey Chung
Jeffrey Chung

Reputation: 19517

Probably because you're terminating the actor system immediately after defining the scheduler.

Upvotes: 4

Related Questions