diff --git a/akka-persistence-typed/src/main/scala/akka/persistence/typed/javadsl/EventSourcedBehavior.scala b/akka-persistence-typed/src/main/scala/akka/persistence/typed/javadsl/EventSourcedBehavior.scala index c8f4f46d72..045ef81f7c 100644 --- a/akka-persistence-typed/src/main/scala/akka/persistence/typed/javadsl/EventSourcedBehavior.scala +++ b/akka-persistence-typed/src/main/scala/akka/persistence/typed/javadsl/EventSourcedBehavior.scala @@ -14,6 +14,7 @@ import akka.actor.typed.Behavior.DeferredBehavior import akka.actor.typed.javadsl.ActorContext import akka.annotation.ApiMayChange import akka.annotation.InternalApi +import akka.persistence.SnapshotSelectionCriteria import akka.persistence.typed.EventAdapter import akka.persistence.typed._ import akka.persistence.typed.internal._ @@ -121,6 +122,26 @@ abstract class EventSourcedBehavior[Command, Event, State >: Null] private[akka] */ def shouldSnapshot(state: State, event: Event, sequenceNr: Long): Boolean = false + /** + * Override and define the journal plugin id that this actor should use instead of the default. + */ + def journalPluginId: String = "" + + /** + * Override and define the snapshot store plugin id that this actor should use instead of the default. + */ + def snapshotPluginId: String = "" + + /** + * Override and define the snapshot selection criteria used by this actor instead of the default. + * By default the most recent snapshot is used, and the remaining state updates are recovered by replaying events + * from the sequence number up until which the snapshot reached. + * + * You may configure the behavior to skip replaying snapshots completely, in which case the recovery will be + * performed by replaying all events -- which may take a long time. + */ + def snapshotSelectionCriteria: SnapshotSelectionCriteria = SnapshotSelectionCriteria.Latest + /** * The `tagger` function should give event tags, which will be used in persistence query */ @@ -154,7 +175,13 @@ abstract class EventSourcedBehavior[Command, Event, State >: Null] private[akka] emptyState, (state, cmd) => commandHandler()(state, cmd).asInstanceOf[EffectImpl[Event, State]], eventHandler()(_, _), - getClass).snapshotWhen(snapshotWhen).withTagger(tagger).eventAdapter(eventAdapter()) + getClass) + .snapshotWhen(snapshotWhen) + .withTagger(tagger) + .eventAdapter(eventAdapter()) + .withJournalPluginId(journalPluginId) + .withSnapshotPluginId(snapshotPluginId) + .withSnapshotSelectionCriteria(snapshotSelectionCriteria) val handler = signalHandler() val behaviorWithSignalHandler = diff --git a/akka-persistence-typed/src/test/java/akka/persistence/typed/javadsl/PersistentActorCompileOnlyTest.java b/akka-persistence-typed/src/test/java/akka/persistence/typed/javadsl/PersistentActorCompileOnlyTest.java index 4ac2c13f61..7ab27bdf34 100644 --- a/akka-persistence-typed/src/test/java/akka/persistence/typed/javadsl/PersistentActorCompileOnlyTest.java +++ b/akka-persistence-typed/src/test/java/akka/persistence/typed/javadsl/PersistentActorCompileOnlyTest.java @@ -9,6 +9,7 @@ import akka.actor.typed.Behavior; import akka.actor.typed.javadsl.ActorContext; import akka.actor.typed.ActorRef; import akka.actor.typed.javadsl.Behaviors; +import akka.persistence.SnapshotSelectionCriteria; import akka.persistence.typed.EventAdapter; import akka.actor.testkit.typed.javadsl.TestInbox; import akka.persistence.typed.PersistenceId; @@ -100,7 +101,7 @@ public class PersistentActorCompileOnlyTest { @Override public EventHandler eventHandler() { - return (state, event) -> state.addEvent(event); + return SimpleState::addEvent; } // #install-event-adapter @@ -110,6 +111,44 @@ public class PersistentActorCompileOnlyTest { } // #install-event-adapter }; + + static class AdditionalSettings + extends EventSourcedBehavior { + + public AdditionalSettings(PersistenceId persistenceId) { + super(new PersistenceId("p1")); + } + + @Override + public SimpleState emptyState() { + return new SimpleState(); + } + + @Override + public CommandHandler commandHandler() { + return (state, cmd) -> Effect().persist(new SimpleEvent(cmd.data)); + } + + @Override + public EventHandler eventHandler() { + return SimpleState::addEvent; + } + + @Override + public SnapshotSelectionCriteria snapshotSelectionCriteria() { + return SnapshotSelectionCriteria.none(); + } + + @Override + public String journalPluginId() { + return "other.journal"; + } + + @Override + public String snapshotPluginId() { + return "other.snapshot-store"; + } + } } abstract static class WithAck {