score:2

Accepted answer

i think you want something like this:

var result = 0
val cancellable = scheduler.schedule(0.seconds, 10.seconds) {
  result = result + 1
}

you can use the cancellable value to stop the schedule when it is no longer needed.


here is the full code:

def initializewebserver(interface: string, port: int) = {

  var result = 0
  val canceallable = context.system.scheduler.schedule(0.seconds, 10.seconds) {
    result = result + 1
  }

  val route: route =
    concat(
      get {
        path("getresult") {
          complete(result)
        }
      }
    )

  val bindingfuture = http().bindandhandle(route, interface, port.toint)
  println(s"server online at http://$interface:$port/")

  coordinatedshutdown(system).addjvmshutdownhook({
    bindingfuture
      .flatmap(_.unbind())
  })
}

score:0

you could model this by using an actor:

class stateholder extends actor {

  val state = ???

  context.system.scheduler.schedule(0.second, 10.second, self, dotask)

  def receive = {
    case dotask   => /* update state */
    case getstate => /* fetch state */
  }
}

the scheduler will cause the actor to send itself an dotask message and then you can fetch the current state by sending a message.


Related Query

More Query from same tag