You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.2 KiB
48 lines
1.2 KiB
package nova.monadic_sfx.actors
|
|
|
|
import akka.actor.typed._
|
|
import akka.actor.typed.scaladsl._
|
|
|
|
object Counter {
|
|
sealed trait Command
|
|
case object Increment extends Command
|
|
final case class GetValue(replyTo: ActorRef[Value]) extends Command
|
|
final case class Value(n: Int)
|
|
final case object Stop extends Command
|
|
|
|
def apply(): Behavior[Command] = {
|
|
Behaviors.setup(context => new Counter(context))
|
|
}
|
|
}
|
|
|
|
class Counter(context: ActorContext[Counter.Command])
|
|
extends AbstractBehavior[Counter.Command](context) {
|
|
import Counter._
|
|
|
|
private var n = 0
|
|
|
|
override def onMessage(msg: Command): Behavior[Counter.Command] = {
|
|
msg match {
|
|
case Increment =>
|
|
n += 1
|
|
context.log.debug("Incremented counter to [{}]", n)
|
|
this
|
|
case GetValue(replyTo) =>
|
|
replyTo ! Value(n)
|
|
this
|
|
case Stop =>
|
|
context.log.info("Recieved shutdown counter actor")
|
|
Behaviors.stopped
|
|
}
|
|
}
|
|
|
|
override def onSignal: PartialFunction[Signal, Behavior[Counter.Command]] = {
|
|
case _: Terminated =>
|
|
context.log.info("Recieved shutdown counter actor terminated")
|
|
this
|
|
case PostStop =>
|
|
context.log.info("Recieved shutdown counter actor poststop")
|
|
this
|
|
}
|
|
|
|
}
|