Returns an com.thoughtworks.dsl.Dsl instance for keywords.akka.actor.ReceiveMessage in the akka.actor.typed.Behavior domain.
Returns an com.thoughtworks.dsl.Dsl instance for keywords.akka.actor.ReceiveMessage in the akka.actor.typed.Behavior domain.
Given an echoActor
that receives Ping
messages and replies corresponding Pong
messages
import akka.actor.typed._ case class Ping(message: String, response: ActorRef[Pong]) case class Pong(message: String) def echoActor: Behavior[Ping] = { while (true) { val Ping(m, replyTo) = !ReceiveMessage[Ping] replyTo ! Pong(m) } throw new Exception("Unreachable code!") }
When pinging it with "hello",
then it should reply a Pong
with the same message "hello".
import akka.actor.testkit.typed.scaladsl._ val pinger = BehaviorTestKit(echoActor) val probe = TestInbox[Pong]() pinger.run(Ping("hello", probe.ref)) probe.expectMessage(Pong("hello"))
Returns an com.thoughtworks.dsl.Dsl instance for keywords.akka.actor.ReceiveMessage.Partial in the akka.actor.typed.Behavior domain.
Returns an com.thoughtworks.dsl.Dsl instance for keywords.akka.actor.ReceiveMessage.Partial in the akka.actor.typed.Behavior domain.
Given an echoActor
that receives Ping
messages and replies corresponding Pong
messages
import akka.actor.typed._ case class Ping(message: String, response: ActorRef[Pong]) case class Pong(message: String) def echoActor: Behavior[AnyRef] = { while (true) { val Ping(m, replyTo) = !(ReceiveMessage.Partial[Ping]) replyTo ! Pong(m) } throw new Exception("Unreachable code!") }
When pinging it with "hello",
then it should reply a Pong
with the same message "message",
and other types of messages should not be handled.
import akka.actor.testkit.typed.scaladsl._ val pinger = BehaviorTestKit(echoActor) object UnhandledMessage pinger.run(UnhandledMessage) val probe = TestInbox[Pong]() pinger.run(Ping("hello", probe.ref)) probe.expectMessage(Pong("hello"))
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.