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.
import akka.actor.typed._
sealedtrait State
caseobject Opened extends State
caseobject Closed extends State
sealedtrait Transition
caseclass Open(response: ActorRef[State]) extends Transition
caseclass Close(response: ActorRef[State]) extends Transition
def doorActor: Behavior[Transition] = {
while (true) {
val open = !ReceiveMessage.Partial[Open]
open.response ! Opened
val close = !ReceiveMessage.Partial[Close]
close.response ! Closed
}
thrownew 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.
To use try / catch / finally expressions with !-notation,
the return type of enclosing function should be Behavior[?] !! Throwable,
as shown in the following createDecoderActor method.
It will open an java.io.InputStream,
read String from the stream,
and close the stream in a finally block.
Since createDecoderActor returns Behavior[Command] !! Throwable,
it receives message of type Command,
and accepts an additional callback function
to handle exceptions that are not handled in createDecoderActor.
import akka.actor.testkit.typed.scaladsl._
val errorHandler = mockFunction[Throwable, Behavior[Command]]
val decoderActor = BehaviorTestKit(createDecoderActor(errorHandler))
Given an InputStream that throws an java.io.IOException when read from it,
Contains the com.thoughtworks.dsl.Dsl instances in a typed actor.
Installation
This typed object supports !-notation for keywords.akka.actor.ReceiveMessage in the typed actor domains, which requires BangNotation and ResetEverywhere compiler plugins along with this
typed
library. For sbt, add the following settings to yourbuild.sbt
:Imports
Then, add the following import statement to enable
ReceiveMessage
in the akka.actor.typed.Behavior domain.Author:
杨博 (Yang Bo)
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.ReceiveMessage.Partial:The door should reply an
Opened
state after performing anOpen
transition,and the state of the door can be switched between
Opend
andClosed
according toOpen
andClose
transition.To use
try
/catch
/finally
expressions with !-notation, the return type of enclosing function should beBehavior[?] !! Throwable
, as shown in the followingcreateDecoderActor
method. It will open an java.io.InputStream, read String from the stream, and close the stream in afinally
block.Since
createDecoderActor
returnsBehavior[Command] !! Throwable
, it receives message of typeCommand
, and accepts an additional callback function to handle exceptions that are not handled increateDecoderActor
.Given an
InputStream
that throws an java.io.IOException when read from it,when the
decoderActor
try to read a String from the stream, it should close the stream due tofinally
block triggered by the exception.