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

307 Views Asked by At

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

There are 1 best solutions below

3
On

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.