Docs sample of accessing context from persistent behavior

This commit is contained in:
Johan Andrén 2018-09-20 15:45:16 +02:00
parent 68c4e14dbd
commit 5d129a804b
3 changed files with 54 additions and 3 deletions

View file

@ -50,8 +50,7 @@ Next we'll discuss each of these in detail.
### Command handler ### Command handler
The command handler is a function with @java[2 parameters for]@scala[3 parameters for the `ActorContext`,] The command handler is a function with 2 parameters, the current `State` and the incoming `Command`.
current `State` and `Command`.
A command handler returns an `Effect` directive that defines what event or events, if any, to persist. A command handler returns an `Effect` directive that defines what event or events, if any, to persist.
Effects are created using @java[a factory that is returned via the `Effect()` method] @scala[the `Effect` factory] Effects are created using @java[a factory that is returned via the `Effect()` method] @scala[the `Effect` factory]
@ -127,7 +126,18 @@ Java
The `PersistentBehavior` can then be run as with any plain typed actor as described in [typed actors documentation](actors-typed.md). The `PersistentBehavior` can then be run as with any plain typed actor as described in [typed actors documentation](actors-typed.md).
@java[The `ActorContext` can be obtained with `Behaviors.setup` and be passed as a constructor parameter.]
## Accessing the ActorContext
If the persistent behavior needs to use the `ActorContext`, for example to spawn child actors, it can be obtained by
wrapping construction with `Behaviors.setup`:
Scala
: @@snip [PersistentActorCompileOnyTest.scala](/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentActorCompileOnlyTest.scala) { #actor-context }
Java
: @@snip [PersistentActorCompileOnyTest.java](/akka-persistence-typed/src/test/java/akka/persistence/typed/javadsl/PersistentActorCompileOnlyTest.java) { #actor-context }
## Larger example ## Larger example

View file

@ -5,8 +5,10 @@
package akka.persistence.typed.javadsl; package akka.persistence.typed.javadsl;
import akka.actor.Scheduler; import akka.actor.Scheduler;
import akka.actor.typed.Behavior;
import akka.actor.typed.javadsl.ActorContext; import akka.actor.typed.javadsl.ActorContext;
import akka.actor.typed.ActorRef; import akka.actor.typed.ActorRef;
import akka.actor.typed.javadsl.Behaviors;
import akka.persistence.typed.EventAdapter; import akka.persistence.typed.EventAdapter;
import akka.actor.testkit.typed.javadsl.TestInbox; import akka.actor.testkit.typed.javadsl.TestInbox;
import akka.persistence.typed.SideEffect; import akka.persistence.typed.SideEffect;
@ -266,13 +268,24 @@ public class PersistentActorCompileOnlyTest {
.thenAccept(sender::tell); .thenAccept(sender::tell);
} }
// #actor-context
public Behavior<Command> behavior(String persistenceId) {
return Behaviors.setup(ctx -> new MyPersistentBehavior(persistenceId, ctx));
}
// #actor-context
// #actor-context
class MyPersistentBehavior extends PersistentBehavior<Command, Event, RecoveryComplete.EventsInFlight> { class MyPersistentBehavior extends PersistentBehavior<Command, Event, RecoveryComplete.EventsInFlight> {
// this makes the context available to the command handler etc.
private final ActorContext<Command> ctx; private final ActorContext<Command> ctx;
public MyPersistentBehavior(String persistenceId, ActorContext<Command> ctx) { public MyPersistentBehavior(String persistenceId, ActorContext<Command> ctx) {
super(persistenceId); super(persistenceId);
this.ctx = ctx; this.ctx = ctx;
} }
// #actor-context
@Override @Override
public EventsInFlight emptyState() { public EventsInFlight emptyState() {
@ -307,4 +320,5 @@ public class PersistentActorCompileOnlyTest {
} }
} }
} }
} }

View file

@ -432,4 +432,31 @@ object PersistentActorCompileOnlyTest {
} }
object WithContext {
sealed trait Command
sealed trait Event
class State
// #actor-context
val behavior: Behavior[String] =
Behaviors.setup { ctx =>
PersistentBehaviors.receive[String, String, State](
persistenceId = "myPersistenceId",
emptyState = new State,
commandHandler = CommandHandler.command {
cmd
ctx.log.info("Got command {}", cmd)
Effect.persist(cmd).thenRun { state =>
ctx.log.info("event persisted, new state {}", state)
}
},
eventHandler = {
case (state, _) state
})
}
// #actor-context
}
} }