How to periodically check an actor mailbox and alter variables in scala

こ雲淡風輕ζ 提交于 2019-12-25 09:17:39

问题


I am using akka actors in Scala. I would like to know if there is a way to have an actor, which while processing a message received can periodically check its mailbox for other messages and following this messages can alter its variables.

A scheme like:

class Actor1 (constructors){

    def receive={
       case "go" => run()       //the actor starts
       case "alter variables"  // a new message is stashed in mailbox
    }

    def run={
       //do stuff
       check(mailbox) //while the porocessing of the "go" message 
                     // is not finished
       if ( "alter variables" in mailbox) {
          change a variable value
       }
    }

}


回答1:


Nothing says you can't move a long running task into a different thread, using a Future, inside an actor. Your actor would continue to respond to messages in the mailbox, while another thread chugs away on your processing. You can set up your callbacks on the future, and let the task finish normally.



来源:https://stackoverflow.com/questions/41247523/how-to-periodically-check-an-actor-mailbox-and-alter-variables-in-scala

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!