Created
May 23, 2015 13:51
-
-
Save reevik/f5f3f0f02fbea6e513dd to your computer and use it in GitHub Desktop.
Counter example with actors.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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