Skip to content

Instantly share code, notes, and snippets.

@reevik
Created May 23, 2015 13:51
Show Gist options
  • Save reevik/f5f3f0f02fbea6e513dd to your computer and use it in GitHub Desktop.
Save reevik/f5f3f0f02fbea6e513dd to your computer and use it in GitHub Desktop.
Counter example with actors.
package io.moo.training.akka
import akka.actor.{Actor, Props}
class CounterActor extends Actor {
var counter: Int = 0
override def receive: Receive = {
case "incr" => counter += 1
case "get" => sender ! counter
}
}
// main actor application (run by user and starts a supervisor actor)
class Main extends Actor {
// starts a counter actor.
val counterActor = context.actorOf(Props[CounterActor], "counter")
// set incr message to the counter actor.
counterActor ! "incr"
// get the status of counter
counterActor ! "get"
override def receive: Actor.Receive = {
case count: Int => {
// give the counter status out and terminate the supervisor actor.
println(s"count is $count")
context.stop(self)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment