How to Cancel an Akka actor?

廉价感情. 提交于 2019-12-05 21:21:05

You could also check the status of the Future in the Actor.

class MyActor extends Actor {
  def receive = {
    case msg =>
      while(!self.senderFuture.get.isCompleted) {
        performWork(msg)
      }
      self reply result
  }
  ...
}

This requires the message to be sent with '?' or 'ask' though. Hope it helps.

If you're just in-VM you can just pass along an AtomicBoolean with your Job message and check that in your actor intermittently to see if you should abort.

actor ! Job(..., someAtomicBoolean)

class MyActor extends Actor {
  def receive = {
    case Job(..., cancelPlease) =>
      while(cancelPlease.get == false) {
        performWork
      }
      self reply result
  }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!