=typ #24683 Behaviors receive, receiveMessage, receivePartial, receiveMessagePartial (#24718)

* wip

* =typ #24683 Behaviors receive, receiveMessage, receivePartial,
receiveMessagePartial

* move MutableBehavior out as separate file

* receive, receiveSignal

* missing copy

* final cleanup

* cleanup, formatting
This commit is contained in:
Konrad `ktoso` Malawski 2018-03-20 00:20:13 +09:00 committed by GitHub
parent 6656b3c869
commit b3fbf6869b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
99 changed files with 639 additions and 557 deletions

View file

@ -68,13 +68,13 @@ private[persistence] class EventsourcedReplayingEvents[C, E, S](override val set
private def stay(state: ReplayingState[S]): Behavior[InternalProtocol] =
withMdc(setup, MDC.ReplayingEvents) {
Behaviors.immutable[InternalProtocol] {
case (_, JournalResponse(r)) onJournalResponse(state, r)
case (_, SnapshotterResponse(r)) onSnapshotterResponse(r)
case (_, RecoveryTickEvent(snap)) onRecoveryTick(state, snap)
case (_, cmd: IncomingCommand[C]) onCommand(cmd)
case (_, RecoveryPermitGranted) Behaviors.unhandled // should not happen, we already have the permit
}.onSignal(returnPermitOnStop)
Behaviors.receiveMessage[InternalProtocol] {
case JournalResponse(r) onJournalResponse(state, r)
case SnapshotterResponse(r) onSnapshotterResponse(r)
case RecoveryTickEvent(snap) onRecoveryTick(state, snap)
case cmd: IncomingCommand[C] onCommand(cmd)
case RecoveryPermitGranted Behaviors.unhandled // should not happen, we already have the permit
}.receiveSignal(returnPermitOnStop)
}
private def onJournalResponse(

View file

@ -46,13 +46,13 @@ private[akka] class EventsourcedReplayingSnapshot[C, E, S](override val setup: E
loadSnapshot(setup.recovery.fromSnapshot, setup.recovery.toSequenceNr)
withMdc(setup, MDC.ReplayingSnapshot) {
Behaviors.immutable[InternalProtocol] {
case (_, SnapshotterResponse(r)) onSnapshotterResponse(r)
case (_, JournalResponse(r)) onJournalResponse(r)
case (_, RecoveryTickEvent(snapshot)) onRecoveryTick(snapshot)
case (_, cmd: IncomingCommand[C]) onCommand(cmd)
case (_, RecoveryPermitGranted) Behaviors.unhandled // should not happen, we already have the permit
}.onSignal(returnPermitOnStop)
Behaviors.receiveMessage[InternalProtocol] {
case SnapshotterResponse(r) onSnapshotterResponse(r)
case JournalResponse(r) onJournalResponse(r)
case RecoveryTickEvent(snapshot) onRecoveryTick(snapshot)
case cmd: IncomingCommand[C] onCommand(cmd)
case RecoveryPermitGranted Behaviors.unhandled // should not happen, we already have the permit
}.receiveSignal(returnPermitOnStop)
}
}

View file

@ -37,7 +37,7 @@ private[akka] class EventsourcedRequestingRecoveryPermit[C, E, S](override val s
requestRecoveryPermit()
withMdc {
Behaviors.immutable[InternalProtocol] {
Behaviors.receive[InternalProtocol] {
case (_, InternalProtocol.RecoveryPermitGranted)
becomeReplaying()

View file

@ -7,7 +7,7 @@ package akka.persistence.typed.internal
import akka.actor.typed.{ Behavior, Signal }
import akka.actor.typed.Behavior.StoppedBehavior
import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.scaladsl.Behaviors.MutableBehavior
import akka.actor.typed.scaladsl.MutableBehavior
import akka.annotation.InternalApi
import akka.persistence.JournalProtocol._
import akka.persistence._
@ -148,9 +148,9 @@ private[akka] object EventsourcedRunning {
}
withMdc(setup, MDC.RunningCmds) {
Behaviors.immutable[EventsourcedBehavior.InternalProtocol] {
case (_, IncomingCommand(c: C @unchecked)) onCommand(state, c)
case _ Behaviors.unhandled
Behaviors.receiveMessage[EventsourcedBehavior.InternalProtocol] {
case IncomingCommand(c: C @unchecked) onCommand(state, c)
case _ Behaviors.unhandled
}
}

View file

@ -92,7 +92,7 @@ abstract class PersistentBehavior[Command, Event, State >: Null](val persistence
/** INTERNAL API */
@InternalApi private[akka] override def untypedProps(props: akka.actor.typed.Props): akka.actor.Props = {
val behaviorImpl = scaladsl.PersistentBehaviors.immutable[Command, Event, State](
val behaviorImpl = scaladsl.PersistentBehaviors.receive[Command, Event, State](
persistenceId,
initialState,
(c, state, cmd) commandHandler()(c.asJava, state, cmd).asInstanceOf[EffectImpl[Event, State]],

View file

@ -18,7 +18,7 @@ object PersistentBehaviors {
/**
* Create a `Behavior` for a persistent actor.
*/
def immutable[Command, Event, State](
def receive[Command, Event, State](
persistenceId: String,
initialState: State,
commandHandler: CommandHandler[Command, Event, State],

View file

@ -8,11 +8,8 @@ import akka.actor.typed.ActorRef;
import akka.actor.typed.javadsl.Behaviors;
import akka.japi.Pair;
import akka.japi.function.Function3;
import akka.persistence.typed.scaladsl.PersistentBehaviorSpec;
import akka.persistence.typed.scaladsl.PersistentBehaviorSpec$;
import akka.testkit.AkkaJUnitActorSystemResource;
import akka.testkit.typed.javadsl.TestKitJunitResource;
import akka.testkit.typed.scaladsl.ActorTestKit;
import akka.testkit.typed.javadsl.TestProbe;
import org.junit.ClassRule;
import org.junit.Test;
@ -204,7 +201,7 @@ public class PersistentActorTest {
.matchCommand(IncrementLater.class, (ctx, state, command) -> {
ActorRef<Object> delay = ctx.spawnAnonymous(Behaviors.withTimers(timers -> {
timers.startSingleTimer(Tick.instance, Tick.instance, FiniteDuration.create(10, TimeUnit.MILLISECONDS));
return Behaviors.immutable((context, o) -> Behaviors.stopped());
return Behaviors.receive((context, o) -> Behaviors.stopped());
}));
ctx.watchWith(delay, new DelayFinished());
return Effect().none();

View file

@ -42,7 +42,7 @@ object RecoveryPermitterSpec {
commandProbe: TestProbe[Any],
eventProbe: TestProbe[Any],
throwOnRecovery: Boolean = false): Behavior[Command] =
PersistentBehaviors.immutable[Command, Event, State](
PersistentBehaviors.receive[Command, Event, State](
persistenceId = name,
initialState = EmptyState,
commandHandler = CommandHandler.command {
@ -57,7 +57,7 @@ object RecoveryPermitterSpec {
}
def forwardingBehavior(target: TestProbe[Any]): Behavior[Any] =
Behaviors.immutable[Any] {
Behaviors.receive[Any] {
(_, any) target.ref ! any; Behaviors.same
}
}
@ -188,7 +188,7 @@ class RecoveryPermitterSpec extends ActorTestKit with TypedAkkaSpecWithShutdown
val persistentActor =
ctx.spawnAnonymous(persistentBehavior("p3", p3, p3, throwOnRecovery = true))
ctx.watch(persistentActor)
Behaviors.immutable[Command] {
Behaviors.receive[Command] {
case (_, StopActor)
stopProbe.ref ! persistentActor
ctx.stop(persistentActor)

View file

@ -43,7 +43,7 @@ object PersistentActorCompileOnlyTest {
//#behavior
val simpleBehavior: PersistentBehavior[SimpleCommand, SimpleEvent, ExampleState] =
PersistentBehaviors.immutable[SimpleCommand, SimpleEvent, ExampleState](
PersistentBehaviors.receive[SimpleCommand, SimpleEvent, ExampleState](
persistenceId = "sample-id-1",
initialState = ExampleState(Nil),
commandHandler = commandHandler,
@ -63,7 +63,7 @@ object PersistentActorCompileOnlyTest {
case class ExampleState(events: List[String] = Nil)
PersistentBehaviors.immutable[MyCommand, MyEvent, ExampleState](
PersistentBehaviors.receive[MyCommand, MyEvent, ExampleState](
persistenceId = "sample-id-1",
initialState = ExampleState(Nil),
@ -107,7 +107,7 @@ object PersistentActorCompileOnlyTest {
.foreach(sender ! _)
}
PersistentBehaviors.immutable[Command, Event, EventsInFlight](
PersistentBehaviors.receive[Command, Event, EventsInFlight](
persistenceId = "recovery-complete-id",
initialState = EventsInFlight(0, Map.empty),
@ -149,7 +149,7 @@ object PersistentActorCompileOnlyTest {
sealed trait Event
case class MoodChanged(to: Mood) extends Event
val b: Behavior[Command] = PersistentBehaviors.immutable[Command, Event, Mood](
val b: Behavior[Command] = PersistentBehaviors.receive[Command, Event, Mood](
persistenceId = "myPersistenceId",
initialState = Happy,
commandHandler = CommandHandler.byState {
@ -190,7 +190,7 @@ object PersistentActorCompileOnlyTest {
case class State(tasksInFlight: List[Task])
PersistentBehaviors.immutable[Command, Event, State](
PersistentBehaviors.receive[Command, Event, State](
persistenceId = "asdf",
initialState = State(Nil),
commandHandler = CommandHandler.command {
@ -217,7 +217,7 @@ object PersistentActorCompileOnlyTest {
def worker(task: Task): Behavior[Nothing] = ???
PersistentBehaviors.immutable[Command, Event, State](
PersistentBehaviors.receive[Command, Event, State](
persistenceId = "asdf",
initialState = State(Nil),
commandHandler = (ctx, _, cmd) cmd match {
@ -280,7 +280,7 @@ object PersistentActorCompileOnlyTest {
.persist[Event, List[Id]](ItemAdded(id))
.andThen(metadataRegistry ! GetMetaData(id, adapt))
PersistentBehaviors.immutable[Command, Event, List[Id]](
PersistentBehaviors.receive[Command, Event, List[Id]](
persistenceId = "basket-1",
initialState = Nil,
commandHandler =
@ -341,7 +341,7 @@ object PersistentActorCompileOnlyTest {
if (currentState == newMood) Effect.none
else Effect.persist(MoodChanged(newMood))
PersistentBehaviors.immutable[Command, Event, Mood](
PersistentBehaviors.receive[Command, Event, Mood](
persistenceId = "myPersistenceId",
initialState = Sad,
commandHandler = (_, state, cmd)
@ -377,7 +377,7 @@ object PersistentActorCompileOnlyTest {
class State
PersistentBehaviors.immutable[Command, Event, State](
PersistentBehaviors.receive[Command, Event, State](
persistenceId = "myPersistenceId",
initialState = new State,
commandHandler = CommandHandler.command {

View file

@ -84,7 +84,7 @@ object PersistentBehaviorSpec {
persistenceId: String,
loggingActor: ActorRef[String],
probe: ActorRef[(State, Event)]): PersistentBehavior[Command, Event, State] = {
PersistentBehaviors.immutable[Command, Event, State](
PersistentBehaviors.receive[Command, Event, State](
persistenceId,
initialState = State(0, Vector.empty),
commandHandler = (ctx, state, cmd) cmd match {
@ -116,7 +116,7 @@ object PersistentBehaviorSpec {
// purpose is to test signals
val delay = ctx.spawnAnonymous(Behaviors.withTimers[Tick.type] { timers
timers.startSingleTimer(Tick, Tick, 10.millis)
Behaviors.immutable((_, msg) msg match {
Behaviors.receive((_, msg) msg match {
case Tick Behaviors.stopped
})
})
@ -412,8 +412,8 @@ class PersistentBehaviorSpec extends ActorTestKit with TypedAkkaSpecWithShutdown
val probe = TestProbe[String]()
val w = Behaviors.setup[Any] { (ctx)
ctx.watch(toWatch)
Behaviors.immutable[Any] { (_, _) Behaviors.same }
.onSignal {
Behaviors.receive[Any] { (_, _) Behaviors.same }
.receiveSignal {
case (_, s: Terminated)
probe.ref ! "Terminated"
Behaviors.stopped

View file

@ -15,7 +15,7 @@ object BasicPersistentBehaviorsSpec {
case class State()
val behavior: Behavior[Command] =
PersistentBehaviors.immutable[Command, Event, State](
PersistentBehaviors.receive[Command, Event, State](
persistenceId = "abc",
initialState = State(),
commandHandler = (ctx, state, cmd) ???,
@ -24,7 +24,7 @@ object BasicPersistentBehaviorsSpec {
//#recovery
val recoveryBehavior: Behavior[Command] =
PersistentBehaviors.immutable[Command, Event, State](
PersistentBehaviors.receive[Command, Event, State](
persistenceId = "abc",
initialState = State(),
commandHandler = (ctx, state, cmd) ???,
@ -36,7 +36,7 @@ object BasicPersistentBehaviorsSpec {
//#tagging
val taggingBehavior: Behavior[Command] =
PersistentBehaviors.immutable[Command, Event, State](
PersistentBehaviors.receive[Command, Event, State](
persistenceId = "abc",
initialState = State(),
commandHandler = (ctx, state, cmd) ???,

View file

@ -119,7 +119,7 @@ object InDepthPersistentBehaviorSpec {
//#behavior
def behavior(entityId: String): Behavior[BlogCommand] =
PersistentBehaviors.immutable[BlogCommand, BlogEvent, BlogState](
PersistentBehaviors.receive[BlogCommand, BlogEvent, BlogState](
persistenceId = "Blog-" + entityId,
initialState = BlogState.empty,
commandHandler,