=doc #16614 Include best-practice tip on messages in companion object in docs
This commit is contained in:
parent
cedfaa35cc
commit
a8632befc6
6 changed files with 89 additions and 0 deletions
|
|
@ -535,6 +535,31 @@ public class UntypedActorDocTest {
|
||||||
//#props-factory
|
//#props-factory
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
//#messages-in-companion
|
||||||
|
public class DemoMessagesActor extends UntypedActor {
|
||||||
|
|
||||||
|
static public class Greeting {
|
||||||
|
private final String from;
|
||||||
|
|
||||||
|
public Greeting(String from) {
|
||||||
|
this.from = from;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGreeter() {
|
||||||
|
return from;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onReceive(Object message) throws Exception {
|
||||||
|
if (message instanceof Greeting) {
|
||||||
|
getSender().tell("Hello " + ((Greeting) message).getGreeter(), getSelf());
|
||||||
|
} else
|
||||||
|
unhandled(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//#messages-in-companion
|
||||||
|
|
||||||
public static class MyActor extends UntypedActor {
|
public static class MyActor extends UntypedActor {
|
||||||
|
|
||||||
final String s;
|
final String s;
|
||||||
|
|
|
||||||
|
|
@ -130,6 +130,13 @@ a reference to its enclosing scope:
|
||||||
|
|
||||||
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/ActorDocTest.java#props-factory
|
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/ActorDocTest.java#props-factory
|
||||||
|
|
||||||
|
Another good practice is to declare what messages an Actor can receive
|
||||||
|
as close to the actor definition as possible (e.g. as static classes
|
||||||
|
inside the Actor or using other suitable class), which makes it easier to know
|
||||||
|
what it can receive.
|
||||||
|
|
||||||
|
.. includecode:: ../../../akka-samples/akka-docs-java-lambda/src/test/java/docs/actor/ActorDocTest.java#messages-in-companion
|
||||||
|
|
||||||
Creating Actors with Props
|
Creating Actors with Props
|
||||||
--------------------------
|
--------------------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,13 @@ used constructor actually exists instead relying only on a runtime check.
|
||||||
|
|
||||||
.. includecode:: code/docs/actor/UntypedActorDocTest.java#props-factory
|
.. includecode:: code/docs/actor/UntypedActorDocTest.java#props-factory
|
||||||
|
|
||||||
|
Another good practice is to declare what messages an Actor can receive
|
||||||
|
as close to the actor definition as possible (e.g. as static classes
|
||||||
|
inside the Actor or using other suitable class), which makes it easier to know
|
||||||
|
what it can receive.
|
||||||
|
|
||||||
|
.. includecode:: code/docs/actor/UntypedActorDocTest.java#messages-in-companion
|
||||||
|
|
||||||
Creating Actors with Props
|
Creating Actors with Props
|
||||||
--------------------------
|
--------------------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -120,6 +120,12 @@ a reference to its enclosing scope:
|
||||||
|
|
||||||
.. includecode:: code/docs/actor/ActorDocSpec.scala#props-factory
|
.. includecode:: code/docs/actor/ActorDocSpec.scala#props-factory
|
||||||
|
|
||||||
|
Another good practice is to declare what messages an Actor can receive
|
||||||
|
in the companion object of the Actor, which makes easier
|
||||||
|
to know what it can receive:
|
||||||
|
|
||||||
|
.. includecode:: code/docs/actor/ActorDocSpec.scala#messages-in-companion
|
||||||
|
|
||||||
Creating Actors with Props
|
Creating Actors with Props
|
||||||
--------------------------
|
--------------------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -82,6 +82,24 @@ class DemoActorWrapper extends Actor {
|
||||||
def receive = Actor.emptyBehavior
|
def receive = Actor.emptyBehavior
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ActorWithMessagesWrapper {
|
||||||
|
//#messages-in-companion
|
||||||
|
object MyActor {
|
||||||
|
case class Greeting(from: String)
|
||||||
|
case object Goodbye
|
||||||
|
}
|
||||||
|
class MyActor extends Actor with ActorLogging {
|
||||||
|
import MyActor._
|
||||||
|
def receive = {
|
||||||
|
case Greeting(greeter) => log.info(s"I was greeted by $greeter.")
|
||||||
|
case Goodbye => log.info("Someone said goodbye to me.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//#messages-in-companion
|
||||||
|
|
||||||
|
def receive = Actor.emptyBehavior
|
||||||
|
}
|
||||||
|
|
||||||
class Hook extends Actor {
|
class Hook extends Actor {
|
||||||
var child: ActorRef = _
|
var child: ActorRef = _
|
||||||
//#preStart
|
//#preStart
|
||||||
|
|
|
||||||
|
|
@ -152,6 +152,32 @@ public class ActorDocTest {
|
||||||
}
|
}
|
||||||
//#props-factory
|
//#props-factory
|
||||||
|
|
||||||
|
static
|
||||||
|
//#messages-in-companion
|
||||||
|
public class DemoMessagesActor extends AbstractActor {
|
||||||
|
|
||||||
|
static public class Greeting {
|
||||||
|
private final String from;
|
||||||
|
|
||||||
|
public Greeting(String from) {
|
||||||
|
this.from = from;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGreeter() {
|
||||||
|
return from;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DemoMessagesActor() {
|
||||||
|
receive(ReceiveBuilder.
|
||||||
|
match(Greeting.class, g -> {
|
||||||
|
log.info("I was greeted by {}", g.getGreeter());
|
||||||
|
}).build()
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
//#messages-in-companion
|
||||||
|
|
||||||
public static class Hook extends AbstractActor {
|
public static class Hook extends AbstractActor {
|
||||||
ActorRef target = null;
|
ActorRef target = null;
|
||||||
public Hook() {
|
public Hook() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue