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(
|
.onMessage(
|
||||||
ChatRoom.SessionDenied.class,
|
ChatRoom.SessionDenied.class,
|
||||||
(context, message) -> {
|
(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();
|
return Behaviors.stopped();
|
||||||
})
|
})
|
||||||
.onMessage(
|
.onMessage(
|
||||||
|
|
@ -305,8 +305,10 @@ public class IntroTest {
|
||||||
.onMessage(
|
.onMessage(
|
||||||
ChatRoom.MessagePosted.class,
|
ChatRoom.MessagePosted.class,
|
||||||
(context, message) -> {
|
(context, message) -> {
|
||||||
System.out.println(
|
context
|
||||||
"message has been posted by '" + message.screenName + "': " + message.message);
|
.getLog()
|
||||||
|
.info(
|
||||||
|
"message has been posted by '{}': {}", message.screenName, message.message);
|
||||||
return Behaviors.stopped();
|
return Behaviors.stopped();
|
||||||
})
|
})
|
||||||
.build();
|
.build();
|
||||||
|
|
|
||||||
|
|
@ -190,7 +190,7 @@ public class OOIntroTest {
|
||||||
.onMessage(
|
.onMessage(
|
||||||
ChatRoom.SessionDenied.class,
|
ChatRoom.SessionDenied.class,
|
||||||
message -> {
|
message -> {
|
||||||
System.out.println("cannot start chat room session: " + message.reason);
|
context.getLog().info("cannot start chat room session: {}", message.reason);
|
||||||
return Behaviors.stopped();
|
return Behaviors.stopped();
|
||||||
})
|
})
|
||||||
.onMessage(
|
.onMessage(
|
||||||
|
|
@ -202,8 +202,10 @@ public class OOIntroTest {
|
||||||
.onMessage(
|
.onMessage(
|
||||||
ChatRoom.MessagePosted.class,
|
ChatRoom.MessagePosted.class,
|
||||||
message -> {
|
message -> {
|
||||||
System.out.println(
|
context
|
||||||
"message has been posted by '" + message.screenName + "': " + message.message);
|
.getLog()
|
||||||
|
.info(
|
||||||
|
"message has been posted by '{}': {}", message.screenName, message.message);
|
||||||
return Behaviors.stopped();
|
return Behaviors.stopped();
|
||||||
})
|
})
|
||||||
.build();
|
.build();
|
||||||
|
|
@ -215,7 +217,7 @@ public class OOIntroTest {
|
||||||
}
|
}
|
||||||
// #chatroom-gabbler
|
// #chatroom-gabbler
|
||||||
|
|
||||||
public static void runChatRoom() throws Exception {
|
public static void runChatRoom() {
|
||||||
|
|
||||||
// #chatroom-main
|
// #chatroom-main
|
||||||
Behavior<Void> main =
|
Behavior<Void> main =
|
||||||
|
|
|
||||||
|
|
@ -149,17 +149,15 @@ object IntroSpec {
|
||||||
room: ActorRef[PublishSessionMessage],
|
room: ActorRef[PublishSessionMessage],
|
||||||
screenName: String,
|
screenName: String,
|
||||||
client: ActorRef[SessionEvent]): Behavior[SessionCommand] =
|
client: ActorRef[SessionEvent]): Behavior[SessionCommand] =
|
||||||
Behaviors.receive { (context, message) =>
|
Behaviors.receiveMessage {
|
||||||
message match {
|
case PostMessage(message) =>
|
||||||
case PostMessage(message) =>
|
// from client, publish to others via the room
|
||||||
// from client, publish to others via the room
|
room ! PublishSessionMessage(screenName, message)
|
||||||
room ! PublishSessionMessage(screenName, message)
|
Behaviors.same
|
||||||
Behaviors.same
|
case NotifyClient(message) =>
|
||||||
case NotifyClient(message) =>
|
// published from the room
|
||||||
// published from the room
|
client ! message
|
||||||
client ! message
|
Behaviors.same
|
||||||
Behaviors.same
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//#chatroom-behavior
|
//#chatroom-behavior
|
||||||
}
|
}
|
||||||
|
|
@ -194,19 +192,21 @@ class IntroSpec extends ScalaTestWithActorTestKit with WordSpecLike {
|
||||||
import ChatRoom._
|
import ChatRoom._
|
||||||
|
|
||||||
val gabbler: Behavior[SessionEvent] =
|
val gabbler: Behavior[SessionEvent] =
|
||||||
Behaviors.receiveMessage {
|
Behaviors.setup { context ⇒
|
||||||
//#chatroom-gabbler
|
Behaviors.receiveMessage {
|
||||||
// We document that the compiler warns about the missing handler for `SessionDenied`
|
//#chatroom-gabbler
|
||||||
case SessionDenied(reason) =>
|
// We document that the compiler warns about the missing handler for `SessionDenied`
|
||||||
println(s"cannot start chat room session: $reason")
|
case SessionDenied(reason) =>
|
||||||
Behaviors.stopped
|
context.log.info("cannot start chat room session: {}", reason)
|
||||||
//#chatroom-gabbler
|
Behaviors.stopped
|
||||||
case SessionGranted(handle) =>
|
//#chatroom-gabbler
|
||||||
handle ! PostMessage("Hello World!")
|
case SessionGranted(handle) =>
|
||||||
Behaviors.same
|
handle ! PostMessage("Hello World!")
|
||||||
case MessagePosted(screenName, message) =>
|
Behaviors.same
|
||||||
println(s"message has been posted by '$screenName': $message")
|
case MessagePosted(screenName, message) =>
|
||||||
Behaviors.stopped
|
context.log.info("message has been posted by '{}': {}", screenName, message)
|
||||||
|
Behaviors.stopped
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//#chatroom-gabbler
|
//#chatroom-gabbler
|
||||||
|
|
||||||
|
|
@ -219,13 +219,14 @@ class IntroSpec extends ScalaTestWithActorTestKit with WordSpecLike {
|
||||||
chatRoom ! GetSession("ol’ Gabbler", gabblerRef)
|
chatRoom ! GetSession("ol’ Gabbler", gabblerRef)
|
||||||
|
|
||||||
Behaviors.receiveSignal {
|
Behaviors.receiveSignal {
|
||||||
case (_, Terminated(ref)) =>
|
case (_, Terminated(_)) =>
|
||||||
Behaviors.stopped
|
Behaviors.stopped
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val system = ActorSystem(main, "ChatRoomDemo")
|
val system = ActorSystem(main, "ChatRoomDemo")
|
||||||
//#chatroom-main
|
//#chatroom-main
|
||||||
|
system.whenTerminated // remove compiler warnings
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -93,16 +93,18 @@ class OOIntroSpec extends ScalaTestWithActorTestKit with WordSpecLike {
|
||||||
import ChatRoom._
|
import ChatRoom._
|
||||||
|
|
||||||
val gabbler =
|
val gabbler =
|
||||||
Behaviors.receiveMessage[SessionEvent] {
|
Behaviors.setup[SessionEvent] { context ⇒
|
||||||
case SessionDenied(reason) =>
|
Behaviors.receiveMessage[SessionEvent] {
|
||||||
println(s"cannot start chat room session: $reason")
|
case SessionDenied(reason) =>
|
||||||
Behaviors.stopped
|
context.log.info("cannot start chat room session: {}", reason)
|
||||||
case SessionGranted(handle) =>
|
Behaviors.stopped
|
||||||
handle ! PostMessage("Hello World!")
|
case SessionGranted(handle) ⇒
|
||||||
Behaviors.same
|
handle ! PostMessage("Hello World!")
|
||||||
case MessagePosted(screenName, message) =>
|
Behaviors.same
|
||||||
println(s"message has been posted by '$screenName': $message")
|
case MessagePosted(screenName, message) =>
|
||||||
Behaviors.stopped
|
context.log.info("message has been posted by '{}': {}", screenName, message)
|
||||||
|
Behaviors.stopped
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//#chatroom-gabbler
|
//#chatroom-gabbler
|
||||||
|
|
||||||
|
|
@ -115,13 +117,13 @@ class OOIntroSpec extends ScalaTestWithActorTestKit with WordSpecLike {
|
||||||
|
|
||||||
Behaviors
|
Behaviors
|
||||||
.receiveMessagePartial[String] {
|
.receiveMessagePartial[String] {
|
||||||
case "go" =>
|
case "go" ⇒
|
||||||
chatRoom ! GetSession("ol’ Gabbler", gabblerRef)
|
chatRoom ! GetSession("ol’ Gabbler", gabblerRef)
|
||||||
Behaviors.same
|
Behaviors.same
|
||||||
}
|
}
|
||||||
.receiveSignal {
|
.receiveSignal {
|
||||||
case (_, Terminated(_)) =>
|
case (_, Terminated(_)) =>
|
||||||
println("Stopping guardian")
|
context.log.info("Stopping guardian")
|
||||||
Behaviors.stopped
|
Behaviors.stopped
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue