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

  1. package nova.monadic_sfx.actors
  2. import akka.actor.typed._
  3. import akka.actor.typed.scaladsl._
  4. object Counter {
  5. sealed trait Command
  6. case object Increment extends Command
  7. final case class GetValue(replyTo: ActorRef[Value]) extends Command
  8. final case class Value(n: Int)
  9. final case object Stop extends Command
  10. def apply(): Behavior[Command] = {
  11. Behaviors.setup(context => new Counter(context))
  12. }
  13. }
  14. class Counter(context: ActorContext[Counter.Command])
  15. extends AbstractBehavior[Counter.Command](context) {
  16. import Counter._
  17. private var n = 0
  18. override def onMessage(msg: Command): Behavior[Counter.Command] = {
  19. msg match {
  20. case Increment =>
  21. n += 1
  22. context.log.debug("Incremented counter to [{}]", n)
  23. this
  24. case GetValue(replyTo) =>
  25. replyTo ! Value(n)
  26. this
  27. case Stop =>
  28. context.log.info("Recieved shutdown counter actor")
  29. Behaviors.stopped
  30. }
  31. }
  32. override def onSignal: PartialFunction[Signal, Behavior[Counter.Command]] = {
  33. case _: Terminated =>
  34. context.log.info("Recieved shutdown counter actor terminated")
  35. this
  36. case PostStop =>
  37. context.log.info("Recieved shutdown counter actor poststop")
  38. this
  39. }
  40. }