Scheduling an operation at a specified interval
There are some situations where we want an operation to occur at a specified time or occur repeatedly after an interval. This is like a cron job. Let's go through the recipe.
Getting ready
To step through this recipe, we need to import our Hello-Akka
project into the IDE and create a com.packt.chapter5
package in which we will put all the code.
How to do it…
- Let's create a file, say,
ScheduleOperation.scala
in thecom.packt.chapter5
package. - Add the following imports to the top of the file:
import akka.actor.ActorSystem import scala.concurrent.duration._
- Create a test application, as follows, in which we create an actor system and schedule the operation:
object ScheduleOperation extends App { val system = ActorSystem("Hello-Akka") import system.dispatcher system.scheduler.scheduleOnce(10 seconds) { println(s"Sum of (1 + 2) is ${1 + 2}") } system.scheduler.schedule(11...