Akka Typed Add example of how to java getSelf for persistence typed #27061 (#27196)

This commit is contained in:
Helena Edelson 2019-06-21 07:30:33 -07:00 committed by GitHub
parent 611e32de91
commit 25b5daa617
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View file

@ -21,6 +21,33 @@ forming an actor hierarchy. @apidoc[akka.actor.typed.ActorSystem] hosts the hier
actor at the top of the hierarchy of the `ActorSystem`. The lifecycle of a child actor is tied to the parent -- a child actor at the top of the hierarchy of the `ActorSystem`. The lifecycle of a child actor is tied to the parent -- a child
can stop itself or be stopped at any time but it can never outlive its parent. can stop itself or be stopped at any time but it can never outlive its parent.
### The ActorContext
The ActorContext can be accessed for many purposes such as:
* Spawning child actors and supervision
* Watching other actors (`DeathWatch`) to receive a `Terminated(otherActor)` event should the watched actor stop permanently
* Logging
* Creating message adapters
* Request-response interactions (ask) with another actor
* Access to the `self` ActorRef
If a behavior needs to use the `ActorContext`, for example to spawn child actors, or use
@scala[`context.self`]@java[`context.getSelf()`], it can be obtained by wrapping construction with `Behaviors.setup`:
Scala
: @@snip [BasicPersistentBehaviorCompileOnly.scala](/akka-persistence-typed/src/test/scala/docs/akka/persistence/typed/BasicPersistentBehaviorCompileOnly.scala) { #actor-context }
Java
: @@snip [BasicPersistentBehaviorTest.java](/akka-persistence-typed/src/test/java/jdocs/akka/persistence/typed/BasicPersistentBehaviorTest.java) { #actor-context }
#### ActorContext Thread Safety
Many of the methods in `ActorContext` are not thread-safe and
* Must not be accessed by threads from @scala[`scala.concurrent.Future`]@java[`java.util.concurrent.CompletionStage`] callbacks
* Must not be shared between several actor instances
* Should only be used in the ordinary actor message processing thread
### The Guardian Actor ### The Guardian Actor

View file

@ -4,6 +4,7 @@
package jdocs.akka.persistence.typed; package jdocs.akka.persistence.typed;
import akka.actor.typed.ActorRef;
import akka.actor.typed.Behavior; import akka.actor.typed.Behavior;
import akka.actor.typed.SupervisorStrategy; import akka.actor.typed.SupervisorStrategy;
import akka.actor.typed.javadsl.ActorContext; import akka.actor.typed.javadsl.ActorContext;
@ -341,9 +342,13 @@ public class BasicPersistentBehaviorTest {
// this makes the context available to the command handler etc. // this makes the context available to the command handler etc.
private final ActorContext<Command> ctx; private final ActorContext<Command> ctx;
// optionally if you only need `ActorContext.getSelf()`
private final ActorRef<Command> self;
public MyPersistentBehavior(PersistenceId persistenceId, ActorContext<Command> ctx) { public MyPersistentBehavior(PersistenceId persistenceId, ActorContext<Command> ctx) {
super(persistenceId); super(persistenceId);
this.ctx = ctx; this.ctx = ctx;
this.self = ctx.getSelf();
} }
// #actor-context // #actor-context