How do I run 2 Akka actors with the caller sending the consumer a message every n seconds?
As there are no loop-react methods as in the Scala.Actors library I am stuck
somehow, the following will not compile and produces:
overriding method receive in trait Actor of type =>
Caller.this.Receive; method receive has incompatible type
object Foo {
def init() {
actorOf[Caller].start()
actorOf[Consumer].start()
}
}
class Caller extends Actor {
def receive {
while (true) {
self ! "msg"
Thread.sleep(1000)
}
}
}
class Consumer extends Actor {
def receive = {
case msg:String => doStuff()
case e => _
}
}
You’re missing the equals sign after
receivein Caller. Without it, the method is defined as returningUnit(i.e. no useful value), and akka needs you to return aPartialFunction[Any,Unit]from receive.Now, to actually implement your logic in the idiomatic way, you probably want to use a ReceiveTimeout, like so: