pekko/docs/src/test/java/jdocs/persistence/LambdaPersistencePluginDocTest.java

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

216 lines
6.5 KiB
Java
Raw Normal View History

/*
2022-02-04 12:36:44 +01:00
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
*/
package jdocs.persistence;
// #plugin-imports
import org.apache.pekko.dispatch.Futures;
import org.apache.pekko.persistence.*;
import org.apache.pekko.persistence.journal.japi.*;
import org.apache.pekko.persistence.snapshot.japi.*;
// #plugin-imports
import org.apache.pekko.actor.*;
import org.apache.pekko.persistence.journal.leveldb.SharedLeveldbJournal;
import org.apache.pekko.persistence.journal.leveldb.SharedLeveldbStore;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import java.io.File;
2015-06-25 19:58:47 +02:00
import java.util.ArrayList;
import java.util.List;
import org.junit.runner.RunWith;
2020-01-11 15:14:21 +03:00
import org.scalatestplus.junit.JUnitRunner;
import scala.concurrent.Future;
import java.util.function.Consumer;
import org.iq80.leveldb.util.FileUtils;
import java.util.Optional;
import org.apache.pekko.persistence.japi.journal.JavaJournalSpec;
import org.apache.pekko.persistence.japi.snapshot.JavaSnapshotStoreSpec;
public class LambdaPersistencePluginDocTest {
static Object o1 =
new Object() {
final ActorSystem system = null;
// #shared-store-creation
final ActorRef store = system.actorOf(Props.create(SharedLeveldbStore.class), "store");
// #shared-store-creation
// #shared-store-usage
class SharedStorageUsage extends AbstractActor {
@Override
public void preStart() throws Exception {
String path = "akka://example@127.0.0.1:2552/user/store";
ActorSelection selection = getContext().actorSelection(path);
selection.tell(new Identify(1), getSelf());
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(
ActorIdentity.class,
ai -> {
if (ai.correlationId().equals(1)) {
Optional<ActorRef> store = ai.getActorRef();
if (store.isPresent()) {
SharedLeveldbJournal.setStore(store.get(), getContext().getSystem());
} else {
throw new RuntimeException("Couldn't identify store");
}
}
})
.build();
}
}
// #shared-store-usage
};
class MySnapshotStore extends SnapshotStore {
@Override
public Future<Optional<SelectedSnapshot>> doLoadAsync(
String persistenceId, SnapshotSelectionCriteria criteria) {
return null;
}
@Override
public Future<Void> doSaveAsync(SnapshotMetadata metadata, Object snapshot) {
return null;
}
@Override
public Future<Void> doDeleteAsync(SnapshotMetadata metadata) {
return Futures.successful(null);
}
@Override
public Future<Void> doDeleteAsync(String persistenceId, SnapshotSelectionCriteria criteria) {
return Futures.successful(null);
}
}
class MyAsyncJournal extends AsyncWriteJournal {
// #sync-journal-plugin-api
@Override
2015-06-25 19:58:47 +02:00
public Future<Iterable<Optional<Exception>>> doAsyncWriteMessages(
Iterable<AtomicWrite> messages) {
try {
Iterable<Optional<Exception>> result = new ArrayList<Optional<Exception>>();
// blocking call here...
// result.add(..)
return Futures.successful(result);
} catch (Exception e) {
return Futures.failed(e);
}
}
// #sync-journal-plugin-api
@Override
public Future<Void> doAsyncDeleteMessagesTo(String persistenceId, long toSequenceNr) {
return null;
}
@Override
public Future<Void> doAsyncReplayMessages(
String persistenceId,
long fromSequenceNr,
long toSequenceNr,
long max,
Consumer<PersistentRepr> replayCallback) {
return null;
}
@Override
!per #15230 rename processorId => persistentId * This is NOT binary compatible, we're in an *experimental* module. * disabled binary compat checks for package akka.persistence * Source compatibility is retained, but users should migrate do the new method name ASAP. * Plugin APIs were migrated in a way that allows the old plugins to compile agains 2.3.4 without having to change anything. Hopefuly this will help authors migrate to 2.3.4 sooner. This is only source level compatible, not binary compatible. * added deprecation warnings on all processorId methods and provided bridges where possible * for users, the migration should be painless, they can still override the old method, and it'll work. But we encourage them to move to persistenceId; All delegation code will have to be removed afterwards ofc. Conflicts: akka-persistence/src/main/scala/akka/persistence/Channel.scala akka-persistence/src/main/scala/akka/persistence/JournalProtocol.scala akka-persistence/src/main/scala/akka/persistence/Persistent.scala akka-persistence/src/main/scala/akka/persistence/PersistentChannel.scala akka-persistence/src/main/scala/akka/persistence/Processor.scala akka-persistence/src/main/scala/akka/persistence/Snapshot.scala akka-persistence/src/main/scala/akka/persistence/journal/AsyncWriteProxy.scala akka-persistence/src/main/scala/akka/persistence/journal/inmem/InmemJournal.scala akka-persistence/src/main/scala/akka/persistence/journal/leveldb/LeveldbKey.scala akka-persistence/src/main/scala/akka/persistence/snapshot/SnapshotStore.scala akka-persistence/src/test/scala/akka/persistence/serialization/SerializerSpec.scala project/AkkaBuild.scala
2014-06-23 14:33:35 +02:00
public Future<Long> doAsyncReadHighestSequenceNr(String persistenceId, long fromSequenceNr) {
return null;
}
}
static Object o2 =
new Object() {
// #journal-tck-java
@RunWith(JUnitRunner.class)
class MyJournalSpecTest extends JavaJournalSpec {
public MyJournalSpecTest() {
super(
ConfigFactory.parseString(
"akka.persistence.journal.plugin = "
+ "\"akka.persistence.journal.leveldb-shared\""));
}
@Override
public CapabilityFlag supportsRejectingNonSerializableObjects() {
return CapabilityFlag.off();
}
}
// #journal-tck-java
};
static Object o3 =
new Object() {
// #snapshot-store-tck-java
@RunWith(JUnitRunner.class)
class MySnapshotStoreTest extends JavaSnapshotStoreSpec {
public MySnapshotStoreTest() {
super(
ConfigFactory.parseString(
"akka.persistence.snapshot-store.plugin = "
+ "\"akka.persistence.snapshot-store.local\""));
}
}
// #snapshot-store-tck-java
};
static Object o4 =
new Object() {
2019-05-24 08:11:50 +02:00
// https://github.com/akka/akka/issues/26826
// #journal-tck-before-after-java
@RunWith(JUnitRunner.class)
class MyJournalSpecTest extends JavaJournalSpec {
List<File> storageLocations = new ArrayList<File>();
public MyJournalSpecTest() {
super(
ConfigFactory.parseString(
"persistence.journal.plugin = "
+ "\"akka.persistence.journal.leveldb-shared\""));
Config config = system().settings().config();
storageLocations.add(
new File(config.getString("akka.persistence.journal.leveldb.dir")));
storageLocations.add(
new File(config.getString("akka.persistence.snapshot-store.local.dir")));
}
@Override
public CapabilityFlag supportsRejectingNonSerializableObjects() {
return CapabilityFlag.on();
}
@Override
public void beforeAll() {
for (File storageLocation : storageLocations) {
FileUtils.deleteRecursively(storageLocation);
}
super.beforeAll();
}
@Override
public void afterAll() {
super.afterAll();
for (File storageLocation : storageLocations) {
FileUtils.deleteRecursively(storageLocation);
}
}
}
// #journal-tck-before-after-java
};
}