0
votes

I'm trying to create a scheduler in my akka typed just to test it out, like to run every x seconds.

def start(): Behavior[ClientMsg] = Behaviors.setup { ctx =>
    ctx.log.info("start() called")

  ctx.system.scheduler.scheduleAtFixedRate(30.seconds, 5000.millis) { () =>
    ctx.self ! TestMessage(""""this is pretty cool"""")
  }  
}

I am getting an error saying an implicit execution context is not in scope.

Where should I get the execution context from when inside of an typed actor? Also, is this how I should be setting up a scheduler/timer?

2

2 Answers

2
votes

Note that using Behaviors.withTimers { timers => ... } should be preferred over directly using the system scheduler as it handles removing scheduled sends if the actors stops etc.

0
votes

In non-typed Akka the default ExecutionContext is the dispatcher object in the system object:

implicit val executionContext: ExecutionContext = ctx.system.dispatcher

I put this in a base class that I use for all Actors, along with default implicits for Timeout and ActorMaterializer.