Package

com.yang_bo.dsl.domains.akka

actor

Permalink

package actor

Visibility
  1. Public
  2. All

Value Members

  1. object typed

    Permalink

    Contains the com.thoughtworks.dsl.Dsl instances in the akka.actor.typed.Behavior domain.

    Contains the com.thoughtworks.dsl.Dsl instances in the akka.actor.typed.Behavior domain.

    Installation

    == Imports == {{{ import com.yang_bo.dsl.domains.akka.actor.typed._ }}}

    Author:

    杨博 (Yang Bo)

    Example:
    1. This library can be used as an alternative to akka.actor.FSM, for creating state machines in simple Scala control flow. The following state machine contains two states and two transitions between them. It can be created as a simple while loop with the help of keywords.akka.actor.ReceiveMessagePartial:

      import akka.actor.typed._
      import com.yang_bo.dsl.keywords.akka.actor.ReceiveMessagePartial
      sealed trait State
      case object Opened extends State
      case object Closed extends State
      sealed trait Transition
      case class Open(response: ActorRef[State]) extends Transition
      case class Close(response: ActorRef[State]) extends Transition
      def doorActor: Behavior[Transition] = {
        while (true) {
          val open = !ReceiveMessagePartial[Open]
          open.response ! Opened
          val close = !ReceiveMessagePartial[Close]
          close.response ! Closed
        }
        throw new Exception("Unreachable code!")
      }

      The door should reply an Opened state after performing an Open transition,

      import akka.actor.testkit.typed.scaladsl._
      val door = BehaviorTestKit(doorActor)
      val state = TestInbox[State]()
      door.run(Open(state.ref))
      state.expectMessage(Opened)

      and the state of the door can be switched between Opend and Closed according to Open and Close transition.

      door.run(Close(state.ref))
      state.expectMessage(Closed)
      door.run(Open(state.ref))
      state.expectMessage(Opened)
      door.run(Close(state.ref))
      state.expectMessage(Closed)

Ungrouped