0
votes

I'm trying to forward a message (say, "msg") from one actor to another actor selected randomly.

It works perfectly fine unless the randomly picked actor is again one of the earlier actor. Scala waits indefinitely when such a scenario occurs. I want the program to pass the message irrespective of it receiving already (i.e., infinite looping of the message between the actors)

Actor A ---- msg ---> Actor B
Actor B ---- msg ---> Actor C
Actor C ---- msg ---> Actor A

After this, the program does not exit nor does it continue passing the message. Can you please point where i'm wrong?

class MyActor extends Actor {
def act() {
react{
case str : String =>
// Picking a random actor
val randNo : Int = Random.nextInt(5)
actorArray(randNo) ! str
}
}
}

Thanks, MS

1
If you're interested in Actors (particularly in the context of Scala) I'd highly recommend checking out Akka Actors (akka.io) which are replacing Scala Actors in Scala 2.10 when it comes out.adelbertc

1 Answers

4
votes

You need to put a loop around your react call to let an actor react more than once.

class MyActor extends Actor {
  def act() {
    loop {
      react {
        // ...
      }
    }
  }
}

See this short tutorial on Scala actors.