Use logger in typed intro docs examples (#26406)
* Use logger in typed intro docs examples * Remove the rest of the printlns * Fix compiler warnings + add type param * Update akka-actor-typed-tests/src/test/scala/docs/akka/typed/IntroSpec.scala Co-Authored-By: chbatey <christopher.batey@gmail.com> * Review feedback * Review feedback * Formatting
This commit is contained in:
parent
08abca7956
commit
2517ff8595
4 changed files with 51 additions and 44 deletions
|
|
@ -293,7 +293,7 @@ public class IntroTest {
|
|||
.onMessage(
|
||||
ChatRoom.SessionDenied.class,
|
||||
(context, message) -> {
|
||||
System.out.println("cannot start chat room session: " + message.reason);
|
||||
context.getLog().info("cannot start chat room session: {}", message.reason);
|
||||
return Behaviors.stopped();
|
||||
})
|
||||
.onMessage(
|
||||
|
|
@ -305,8 +305,10 @@ public class IntroTest {
|
|||
.onMessage(
|
||||
ChatRoom.MessagePosted.class,
|
||||
(context, message) -> {
|
||||
System.out.println(
|
||||
"message has been posted by '" + message.screenName + "': " + message.message);
|
||||
context
|
||||
.getLog()
|
||||
.info(
|
||||
"message has been posted by '{}': {}", message.screenName, message.message);
|
||||
return Behaviors.stopped();
|
||||
})
|
||||
.build();
|
||||
|
|
|
|||
|
|
@ -190,7 +190,7 @@ public class OOIntroTest {
|
|||
.onMessage(
|
||||
ChatRoom.SessionDenied.class,
|
||||
message -> {
|
||||
System.out.println("cannot start chat room session: " + message.reason);
|
||||
context.getLog().info("cannot start chat room session: {}", message.reason);
|
||||
return Behaviors.stopped();
|
||||
})
|
||||
.onMessage(
|
||||
|
|
@ -202,8 +202,10 @@ public class OOIntroTest {
|
|||
.onMessage(
|
||||
ChatRoom.MessagePosted.class,
|
||||
message -> {
|
||||
System.out.println(
|
||||
"message has been posted by '" + message.screenName + "': " + message.message);
|
||||
context
|
||||
.getLog()
|
||||
.info(
|
||||
"message has been posted by '{}': {}", message.screenName, message.message);
|
||||
return Behaviors.stopped();
|
||||
})
|
||||
.build();
|
||||
|
|
@ -215,7 +217,7 @@ public class OOIntroTest {
|
|||
}
|
||||
// #chatroom-gabbler
|
||||
|
||||
public static void runChatRoom() throws Exception {
|
||||
public static void runChatRoom() {
|
||||
|
||||
// #chatroom-main
|
||||
Behavior<Void> main =
|
||||
|
|
|
|||
|
|
@ -149,17 +149,15 @@ object IntroSpec {
|
|||
room: ActorRef[PublishSessionMessage],
|
||||
screenName: String,
|
||||
client: ActorRef[SessionEvent]): Behavior[SessionCommand] =
|
||||
Behaviors.receive { (context, message) =>
|
||||
message match {
|
||||
case PostMessage(message) =>
|
||||
// from client, publish to others via the room
|
||||
room ! PublishSessionMessage(screenName, message)
|
||||
Behaviors.same
|
||||
case NotifyClient(message) =>
|
||||
// published from the room
|
||||
client ! message
|
||||
Behaviors.same
|
||||
}
|
||||
Behaviors.receiveMessage {
|
||||
case PostMessage(message) =>
|
||||
// from client, publish to others via the room
|
||||
room ! PublishSessionMessage(screenName, message)
|
||||
Behaviors.same
|
||||
case NotifyClient(message) =>
|
||||
// published from the room
|
||||
client ! message
|
||||
Behaviors.same
|
||||
}
|
||||
//#chatroom-behavior
|
||||
}
|
||||
|
|
@ -194,19 +192,21 @@ class IntroSpec extends ScalaTestWithActorTestKit with WordSpecLike {
|
|||
import ChatRoom._
|
||||
|
||||
val gabbler: Behavior[SessionEvent] =
|
||||
Behaviors.receiveMessage {
|
||||
//#chatroom-gabbler
|
||||
// We document that the compiler warns about the missing handler for `SessionDenied`
|
||||
case SessionDenied(reason) =>
|
||||
println(s"cannot start chat room session: $reason")
|
||||
Behaviors.stopped
|
||||
//#chatroom-gabbler
|
||||
case SessionGranted(handle) =>
|
||||
handle ! PostMessage("Hello World!")
|
||||
Behaviors.same
|
||||
case MessagePosted(screenName, message) =>
|
||||
println(s"message has been posted by '$screenName': $message")
|
||||
Behaviors.stopped
|
||||
Behaviors.setup { context ⇒
|
||||
Behaviors.receiveMessage {
|
||||
//#chatroom-gabbler
|
||||
// We document that the compiler warns about the missing handler for `SessionDenied`
|
||||
case SessionDenied(reason) =>
|
||||
context.log.info("cannot start chat room session: {}", reason)
|
||||
Behaviors.stopped
|
||||
//#chatroom-gabbler
|
||||
case SessionGranted(handle) =>
|
||||
handle ! PostMessage("Hello World!")
|
||||
Behaviors.same
|
||||
case MessagePosted(screenName, message) =>
|
||||
context.log.info("message has been posted by '{}': {}", screenName, message)
|
||||
Behaviors.stopped
|
||||
}
|
||||
}
|
||||
//#chatroom-gabbler
|
||||
|
||||
|
|
@ -219,13 +219,14 @@ class IntroSpec extends ScalaTestWithActorTestKit with WordSpecLike {
|
|||
chatRoom ! GetSession("ol’ Gabbler", gabblerRef)
|
||||
|
||||
Behaviors.receiveSignal {
|
||||
case (_, Terminated(ref)) =>
|
||||
case (_, Terminated(_)) =>
|
||||
Behaviors.stopped
|
||||
}
|
||||
}
|
||||
|
||||
val system = ActorSystem(main, "ChatRoomDemo")
|
||||
//#chatroom-main
|
||||
system.whenTerminated // remove compiler warnings
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -93,16 +93,18 @@ class OOIntroSpec extends ScalaTestWithActorTestKit with WordSpecLike {
|
|||
import ChatRoom._
|
||||
|
||||
val gabbler =
|
||||
Behaviors.receiveMessage[SessionEvent] {
|
||||
case SessionDenied(reason) =>
|
||||
println(s"cannot start chat room session: $reason")
|
||||
Behaviors.stopped
|
||||
case SessionGranted(handle) =>
|
||||
handle ! PostMessage("Hello World!")
|
||||
Behaviors.same
|
||||
case MessagePosted(screenName, message) =>
|
||||
println(s"message has been posted by '$screenName': $message")
|
||||
Behaviors.stopped
|
||||
Behaviors.setup[SessionEvent] { context ⇒
|
||||
Behaviors.receiveMessage[SessionEvent] {
|
||||
case SessionDenied(reason) =>
|
||||
context.log.info("cannot start chat room session: {}", reason)
|
||||
Behaviors.stopped
|
||||
case SessionGranted(handle) ⇒
|
||||
handle ! PostMessage("Hello World!")
|
||||
Behaviors.same
|
||||
case MessagePosted(screenName, message) =>
|
||||
context.log.info("message has been posted by '{}': {}", screenName, message)
|
||||
Behaviors.stopped
|
||||
}
|
||||
}
|
||||
//#chatroom-gabbler
|
||||
|
||||
|
|
@ -115,13 +117,13 @@ class OOIntroSpec extends ScalaTestWithActorTestKit with WordSpecLike {
|
|||
|
||||
Behaviors
|
||||
.receiveMessagePartial[String] {
|
||||
case "go" =>
|
||||
case "go" ⇒
|
||||
chatRoom ! GetSession("ol’ Gabbler", gabblerRef)
|
||||
Behaviors.same
|
||||
}
|
||||
.receiveSignal {
|
||||
case (_, Terminated(_)) =>
|
||||
println("Stopping guardian")
|
||||
context.log.info("Stopping guardian")
|
||||
Behaviors.stopped
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue