rename akka-docs dir to docs (#62)
This commit is contained in:
parent
13dce0ec69
commit
708da8caec
1029 changed files with 2033 additions and 2039 deletions
|
|
@ -0,0 +1,740 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.persistence;
|
||||
|
||||
import org.apache.pekko.actor.*;
|
||||
import org.apache.pekko.japi.Procedure;
|
||||
import org.apache.pekko.pattern.BackoffOpts;
|
||||
import org.apache.pekko.pattern.BackoffSupervisor;
|
||||
import org.apache.pekko.persistence.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.Duration;
|
||||
import java.util.Optional;
|
||||
|
||||
public class LambdaPersistenceDocTest {
|
||||
|
||||
public interface SomeOtherMessage {}
|
||||
|
||||
public interface PersistentActorMethods {
|
||||
// #persistence-id
|
||||
public String persistenceId();
|
||||
|
||||
// #persistence-id
|
||||
}
|
||||
|
||||
public interface PersistenceActorRecoveryMethods {
|
||||
// #recovery-status
|
||||
public boolean recoveryRunning();
|
||||
|
||||
public boolean recoveryFinished();
|
||||
// #recovery-status
|
||||
}
|
||||
|
||||
static Object o2 =
|
||||
new Object() {
|
||||
abstract class MyPersistentActor1 extends AbstractPersistentActor {
|
||||
// #recovery-disabled
|
||||
@Override
|
||||
public Recovery recovery() {
|
||||
return Recovery.none();
|
||||
}
|
||||
// #recovery-disabled
|
||||
|
||||
// #recover-on-restart-disabled
|
||||
@Override
|
||||
public void preRestart(Throwable reason, Optional<Object> message) {}
|
||||
// #recover-on-restart-disabled
|
||||
}
|
||||
|
||||
abstract class MyPersistentActor2 extends AbstractPersistentActor {
|
||||
// #recovery-custom
|
||||
@Override
|
||||
public Recovery recovery() {
|
||||
return Recovery.create(457L);
|
||||
}
|
||||
// #recovery-custom
|
||||
}
|
||||
|
||||
class MyPersistentActor4 extends AbstractPersistentActor implements PersistentActorMethods {
|
||||
// #persistence-id-override
|
||||
@Override
|
||||
public String persistenceId() {
|
||||
return "my-stable-persistence-id";
|
||||
}
|
||||
|
||||
// #persistence-id-override
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
String.class,
|
||||
cmd -> {
|
||||
/* ... */
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceiveRecover() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
String.class,
|
||||
evt -> {
|
||||
/* ... */
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
// #recovery-completed
|
||||
class MyPersistentActor5 extends AbstractPersistentActor {
|
||||
|
||||
@Override
|
||||
public String persistenceId() {
|
||||
return "my-stable-persistence-id";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceiveRecover() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
RecoveryCompleted.class,
|
||||
r -> {
|
||||
// perform init after recovery, before any other messages
|
||||
// ...
|
||||
})
|
||||
.match(String.class, this::handleEvent)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(String.class, s -> s.equals("cmd"), s -> persist("evt", this::handleEvent))
|
||||
.build();
|
||||
}
|
||||
|
||||
private void handleEvent(String event) {
|
||||
// update state
|
||||
// ...
|
||||
}
|
||||
}
|
||||
// #recovery-completed
|
||||
|
||||
abstract class MyPersistentActor6 extends AbstractPersistentActor {
|
||||
// #recovery-no-snap
|
||||
@Override
|
||||
public Recovery recovery() {
|
||||
return Recovery.create(SnapshotSelectionCriteria.none());
|
||||
}
|
||||
// #recovery-no-snap
|
||||
}
|
||||
|
||||
abstract class MyActor extends AbstractPersistentActor {
|
||||
// #backoff
|
||||
@Override
|
||||
public void preStart() throws Exception {
|
||||
final Props childProps = Props.create(MyPersistentActor1.class);
|
||||
final Props props =
|
||||
BackoffSupervisor.props(
|
||||
BackoffOpts.onStop(
|
||||
childProps, "myActor", Duration.ofSeconds(3), Duration.ofSeconds(30), 0.2));
|
||||
getContext().actorOf(props, "mySupervisor");
|
||||
super.preStart();
|
||||
}
|
||||
// #backoff
|
||||
}
|
||||
};
|
||||
|
||||
static Object atLeastOnceExample =
|
||||
new Object() {
|
||||
// #at-least-once-example
|
||||
|
||||
class Msg implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
public final long deliveryId;
|
||||
public final String s;
|
||||
|
||||
public Msg(long deliveryId, String s) {
|
||||
this.deliveryId = deliveryId;
|
||||
this.s = s;
|
||||
}
|
||||
}
|
||||
|
||||
class Confirm implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
public final long deliveryId;
|
||||
|
||||
public Confirm(long deliveryId) {
|
||||
this.deliveryId = deliveryId;
|
||||
}
|
||||
}
|
||||
|
||||
class MsgSent implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
public final String s;
|
||||
|
||||
public MsgSent(String s) {
|
||||
this.s = s;
|
||||
}
|
||||
}
|
||||
|
||||
class MsgConfirmed implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
public final long deliveryId;
|
||||
|
||||
public MsgConfirmed(long deliveryId) {
|
||||
this.deliveryId = deliveryId;
|
||||
}
|
||||
}
|
||||
|
||||
class MyPersistentActor extends AbstractPersistentActorWithAtLeastOnceDelivery {
|
||||
private final ActorSelection destination;
|
||||
|
||||
public MyPersistentActor(ActorSelection destination) {
|
||||
this.destination = destination;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String persistenceId() {
|
||||
return "persistence-id";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
String.class,
|
||||
s -> {
|
||||
persist(new MsgSent(s), evt -> updateState(evt));
|
||||
})
|
||||
.match(
|
||||
Confirm.class,
|
||||
confirm -> {
|
||||
persist(new MsgConfirmed(confirm.deliveryId), evt -> updateState(evt));
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceiveRecover() {
|
||||
return receiveBuilder().match(Object.class, evt -> updateState(evt)).build();
|
||||
}
|
||||
|
||||
void updateState(Object event) {
|
||||
if (event instanceof MsgSent) {
|
||||
final MsgSent evt = (MsgSent) event;
|
||||
deliver(destination, deliveryId -> new Msg(deliveryId, evt.s));
|
||||
} else if (event instanceof MsgConfirmed) {
|
||||
final MsgConfirmed evt = (MsgConfirmed) event;
|
||||
confirmDelivery(evt.deliveryId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MyDestination extends AbstractActor {
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
Msg.class,
|
||||
msg -> {
|
||||
// ...
|
||||
getSender().tell(new Confirm(msg.deliveryId), getSelf());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
// #at-least-once-example
|
||||
|
||||
};
|
||||
|
||||
static Object o4 =
|
||||
new Object() {
|
||||
class MyPersistentActor extends AbstractPersistentActor {
|
||||
|
||||
private void updateState(String evt) {}
|
||||
|
||||
// #save-snapshot
|
||||
private Object state;
|
||||
private int snapShotInterval = 1000;
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
SaveSnapshotSuccess.class,
|
||||
ss -> {
|
||||
SnapshotMetadata metadata = ss.metadata();
|
||||
// ...
|
||||
})
|
||||
.match(
|
||||
SaveSnapshotFailure.class,
|
||||
sf -> {
|
||||
SnapshotMetadata metadata = sf.metadata();
|
||||
// ...
|
||||
})
|
||||
.match(
|
||||
String.class,
|
||||
cmd -> {
|
||||
persist(
|
||||
"evt-" + cmd,
|
||||
e -> {
|
||||
updateState(e);
|
||||
if (lastSequenceNr() % snapShotInterval == 0 && lastSequenceNr() != 0)
|
||||
saveSnapshot(state);
|
||||
});
|
||||
})
|
||||
.build();
|
||||
}
|
||||
// #save-snapshot
|
||||
|
||||
@Override
|
||||
public String persistenceId() {
|
||||
return "persistence-id";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceiveRecover() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
RecoveryCompleted.class,
|
||||
r -> {
|
||||
/* ...*/
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static Object o5 =
|
||||
new Object() {
|
||||
|
||||
class MyPersistentActor extends AbstractPersistentActor {
|
||||
|
||||
// #snapshot-criteria
|
||||
@Override
|
||||
public Recovery recovery() {
|
||||
return Recovery.create(
|
||||
SnapshotSelectionCriteria.create(457L, System.currentTimeMillis()));
|
||||
}
|
||||
// #snapshot-criteria
|
||||
|
||||
// #snapshot-offer
|
||||
private Object state;
|
||||
|
||||
@Override
|
||||
public Receive createReceiveRecover() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
SnapshotOffer.class,
|
||||
s -> {
|
||||
state = s.snapshot();
|
||||
// ...
|
||||
})
|
||||
.match(
|
||||
String.class,
|
||||
s -> {
|
||||
/* ...*/
|
||||
})
|
||||
.build();
|
||||
}
|
||||
// #snapshot-offer
|
||||
|
||||
@Override
|
||||
public String persistenceId() {
|
||||
return "persistence-id";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
String.class,
|
||||
s -> {
|
||||
/* ...*/
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
class MyActor extends AbstractActor {
|
||||
private final ActorRef persistentActor =
|
||||
getContext().actorOf(Props.create(MyPersistentActor.class));
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
Object.class,
|
||||
o -> {
|
||||
/* ... */
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static Object o9 =
|
||||
new Object() {
|
||||
// #persist-async
|
||||
class MyPersistentActor extends AbstractPersistentActor {
|
||||
|
||||
@Override
|
||||
public String persistenceId() {
|
||||
return "my-stable-persistence-id";
|
||||
}
|
||||
|
||||
private void handleCommand(String c) {
|
||||
getSender().tell(c, getSelf());
|
||||
|
||||
persistAsync(
|
||||
String.format("evt-%s-1", c),
|
||||
e -> {
|
||||
getSender().tell(e, getSelf());
|
||||
});
|
||||
persistAsync(
|
||||
String.format("evt-%s-2", c),
|
||||
e -> {
|
||||
getSender().tell(e, getSelf());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceiveRecover() {
|
||||
return receiveBuilder().match(String.class, this::handleCommand).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder().match(String.class, this::handleCommand).build();
|
||||
}
|
||||
}
|
||||
// #persist-async
|
||||
|
||||
public void usage() {
|
||||
final ActorSystem system = ActorSystem.create("example");
|
||||
// #persist-async-usage
|
||||
final ActorRef persistentActor = system.actorOf(Props.create(MyPersistentActor.class));
|
||||
persistentActor.tell("a", null);
|
||||
persistentActor.tell("b", null);
|
||||
|
||||
// possible order of received messages:
|
||||
// a
|
||||
// b
|
||||
// evt-a-1
|
||||
// evt-a-2
|
||||
// evt-b-1
|
||||
// evt-b-2
|
||||
// #persist-async-usage
|
||||
}
|
||||
};
|
||||
|
||||
static Object o10 =
|
||||
new Object() {
|
||||
// #defer
|
||||
class MyPersistentActor extends AbstractPersistentActor {
|
||||
|
||||
@Override
|
||||
public String persistenceId() {
|
||||
return "my-stable-persistence-id";
|
||||
}
|
||||
|
||||
private void handleCommand(String c) {
|
||||
persistAsync(
|
||||
String.format("evt-%s-1", c),
|
||||
e -> {
|
||||
getSender().tell(e, getSelf());
|
||||
});
|
||||
persistAsync(
|
||||
String.format("evt-%s-2", c),
|
||||
e -> {
|
||||
getSender().tell(e, getSelf());
|
||||
});
|
||||
|
||||
deferAsync(
|
||||
String.format("evt-%s-3", c),
|
||||
e -> {
|
||||
getSender().tell(e, getSelf());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceiveRecover() {
|
||||
return receiveBuilder().match(String.class, this::handleCommand).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder().match(String.class, this::handleCommand).build();
|
||||
}
|
||||
}
|
||||
// #defer
|
||||
|
||||
public void usage() {
|
||||
final ActorSystem system = ActorSystem.create("example");
|
||||
final ActorRef sender = null; // your imaginary sender here
|
||||
// #defer-caller
|
||||
final ActorRef persistentActor = system.actorOf(Props.create(MyPersistentActor.class));
|
||||
persistentActor.tell("a", sender);
|
||||
persistentActor.tell("b", sender);
|
||||
|
||||
// order of received messages:
|
||||
// a
|
||||
// b
|
||||
// evt-a-1
|
||||
// evt-a-2
|
||||
// evt-a-3
|
||||
// evt-b-1
|
||||
// evt-b-2
|
||||
// evt-b-3
|
||||
// #defer-caller
|
||||
}
|
||||
};
|
||||
|
||||
static Object o100 =
|
||||
new Object() {
|
||||
// #defer-with-persist
|
||||
class MyPersistentActor extends AbstractPersistentActor {
|
||||
|
||||
@Override
|
||||
public String persistenceId() {
|
||||
return "my-stable-persistence-id";
|
||||
}
|
||||
|
||||
private void handleCommand(String c) {
|
||||
persist(
|
||||
String.format("evt-%s-1", c),
|
||||
e -> {
|
||||
sender().tell(e, self());
|
||||
});
|
||||
persist(
|
||||
String.format("evt-%s-2", c),
|
||||
e -> {
|
||||
sender().tell(e, self());
|
||||
});
|
||||
|
||||
defer(
|
||||
String.format("evt-%s-3", c),
|
||||
e -> {
|
||||
sender().tell(e, self());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceiveRecover() {
|
||||
return receiveBuilder().match(String.class, this::handleCommand).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder().match(String.class, this::handleCommand).build();
|
||||
}
|
||||
}
|
||||
// #defer-with-persist
|
||||
|
||||
};
|
||||
|
||||
static Object o11 =
|
||||
new Object() {
|
||||
|
||||
class MyPersistentActor extends AbstractPersistentActor {
|
||||
@Override
|
||||
public String persistenceId() {
|
||||
return "my-stable-persistence-id";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder().matchAny(event -> {}).build();
|
||||
}
|
||||
|
||||
// #nested-persist-persist
|
||||
@Override
|
||||
public Receive createReceiveRecover() {
|
||||
final Procedure<String> replyToSender = event -> getSender().tell(event, getSelf());
|
||||
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
String.class,
|
||||
msg -> {
|
||||
persist(
|
||||
String.format("%s-outer-1", msg),
|
||||
event -> {
|
||||
getSender().tell(event, getSelf());
|
||||
persist(String.format("%s-inner-1", event), replyToSender);
|
||||
});
|
||||
|
||||
persist(
|
||||
String.format("%s-outer-2", msg),
|
||||
event -> {
|
||||
getSender().tell(event, getSelf());
|
||||
persist(String.format("%s-inner-2", event), replyToSender);
|
||||
});
|
||||
})
|
||||
.build();
|
||||
}
|
||||
// #nested-persist-persist
|
||||
|
||||
void usage(ActorRef persistentActor) {
|
||||
// #nested-persist-persist-caller
|
||||
persistentActor.tell("a", ActorRef.noSender());
|
||||
persistentActor.tell("b", ActorRef.noSender());
|
||||
|
||||
// order of received messages:
|
||||
// a
|
||||
// a-outer-1
|
||||
// a-outer-2
|
||||
// a-inner-1
|
||||
// a-inner-2
|
||||
// and only then process "b"
|
||||
// b
|
||||
// b-outer-1
|
||||
// b-outer-2
|
||||
// b-inner-1
|
||||
// b-inner-2
|
||||
|
||||
// #nested-persist-persist-caller
|
||||
}
|
||||
}
|
||||
|
||||
class MyPersistAsyncActor extends AbstractPersistentActor {
|
||||
@Override
|
||||
public String persistenceId() {
|
||||
return "my-stable-persistence-id";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceiveRecover() {
|
||||
return receiveBuilder().matchAny(event -> {}).build();
|
||||
}
|
||||
|
||||
// #nested-persistAsync-persistAsync
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
final Procedure<String> replyToSender = event -> getSender().tell(event, getSelf());
|
||||
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
String.class,
|
||||
msg -> {
|
||||
persistAsync(
|
||||
String.format("%s-outer-1", msg),
|
||||
event -> {
|
||||
getSender().tell(event, getSelf());
|
||||
persistAsync(String.format("%s-inner-1", event), replyToSender);
|
||||
});
|
||||
|
||||
persistAsync(
|
||||
String.format("%s-outer-2", msg),
|
||||
event -> {
|
||||
getSender().tell(event, getSelf());
|
||||
persistAsync(String.format("%s-inner-1", event), replyToSender);
|
||||
});
|
||||
})
|
||||
.build();
|
||||
}
|
||||
// #nested-persistAsync-persistAsync
|
||||
|
||||
void usage(ActorRef persistentActor) {
|
||||
// #nested-persistAsync-persistAsync-caller
|
||||
persistentActor.tell("a", getSelf());
|
||||
persistentActor.tell("b", getSelf());
|
||||
|
||||
// order of received messages:
|
||||
// a
|
||||
// b
|
||||
// a-outer-1
|
||||
// a-outer-2
|
||||
// b-outer-1
|
||||
// b-outer-2
|
||||
// a-inner-1
|
||||
// a-inner-2
|
||||
// b-inner-1
|
||||
// b-inner-2
|
||||
|
||||
// which can be seen as the following causal relationship:
|
||||
// a -> a-outer-1 -> a-outer-2 -> a-inner-1 -> a-inner-2
|
||||
// b -> b-outer-1 -> b-outer-2 -> b-inner-1 -> b-inner-2
|
||||
|
||||
// #nested-persistAsync-persistAsync-caller
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static Object o14 =
|
||||
new Object() {
|
||||
// #safe-shutdown
|
||||
final class Shutdown {}
|
||||
|
||||
class MyPersistentActor extends AbstractPersistentActor {
|
||||
@Override
|
||||
public String persistenceId() {
|
||||
return "some-persistence-id";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
Shutdown.class,
|
||||
shutdown -> {
|
||||
getContext().stop(getSelf());
|
||||
})
|
||||
.match(
|
||||
String.class,
|
||||
msg -> {
|
||||
System.out.println(msg);
|
||||
persist("handle-" + msg, e -> System.out.println(e));
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceiveRecover() {
|
||||
return receiveBuilder().matchAny(any -> {}).build();
|
||||
}
|
||||
}
|
||||
// #safe-shutdown
|
||||
|
||||
public void usage() {
|
||||
final ActorSystem system = ActorSystem.create("example");
|
||||
final ActorRef persistentActor = system.actorOf(Props.create(MyPersistentActor.class));
|
||||
// #safe-shutdown-example-bad
|
||||
// UN-SAFE, due to PersistentActor's command stashing:
|
||||
persistentActor.tell("a", ActorRef.noSender());
|
||||
persistentActor.tell("b", ActorRef.noSender());
|
||||
persistentActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
|
||||
// order of received messages:
|
||||
// a
|
||||
// # b arrives at mailbox, stashing; internal-stash = [b]
|
||||
// # PoisonPill arrives at mailbox, stashing; internal-stash = [b, Shutdown]
|
||||
// PoisonPill is an AutoReceivedMessage, is handled automatically
|
||||
// !! stop !!
|
||||
// Actor is stopped without handling `b` nor the `a` handler!
|
||||
// #safe-shutdown-example-bad
|
||||
|
||||
// #safe-shutdown-example-good
|
||||
// SAFE:
|
||||
persistentActor.tell("a", ActorRef.noSender());
|
||||
persistentActor.tell("b", ActorRef.noSender());
|
||||
persistentActor.tell(new Shutdown(), ActorRef.noSender());
|
||||
// order of received messages:
|
||||
// a
|
||||
// # b arrives at mailbox, stashing; internal-stash = [b]
|
||||
// # Shutdown arrives at mailbox, stashing; internal-stash = [b, Shutdown]
|
||||
// handle-a
|
||||
// # unstashing; internal-stash = [Shutdown]
|
||||
// b
|
||||
// handle-b
|
||||
// # unstashing; internal-stash = []
|
||||
// Shutdown
|
||||
// -- stop --
|
||||
// #safe-shutdown-example-good
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,215 @@
|
|||
/*
|
||||
* 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;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
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
|
||||
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
|
||||
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() {
|
||||
// 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
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.persistence;
|
||||
|
||||
import org.apache.pekko.persistence.journal.EventAdapter;
|
||||
import org.apache.pekko.persistence.journal.EventSeq;
|
||||
|
||||
public class PersistenceEventAdapterDocTest {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static
|
||||
// #identity-event-adapter
|
||||
class MyEventAdapter implements EventAdapter {
|
||||
@Override
|
||||
public String manifest(Object event) {
|
||||
return ""; // if no manifest needed, return ""
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toJournal(Object event) {
|
||||
return event; // identity
|
||||
}
|
||||
|
||||
@Override
|
||||
public EventSeq fromJournal(Object event, String manifest) {
|
||||
return EventSeq.single(event); // identity
|
||||
}
|
||||
}
|
||||
// #identity-event-adapter
|
||||
}
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.persistence;
|
||||
|
||||
import org.apache.pekko.persistence.AbstractPersistentActor;
|
||||
import org.apache.pekko.persistence.RuntimePluginConfig;
|
||||
import com.typesafe.config.Config;
|
||||
import com.typesafe.config.ConfigFactory;
|
||||
|
||||
public class PersistenceMultiDocTest {
|
||||
|
||||
// #default-plugins
|
||||
abstract class AbstractPersistentActorWithDefaultPlugins extends AbstractPersistentActor {
|
||||
@Override
|
||||
public String persistenceId() {
|
||||
return "123";
|
||||
}
|
||||
}
|
||||
// #default-plugins
|
||||
|
||||
// #override-plugins
|
||||
abstract class AbstractPersistentActorWithOverridePlugins extends AbstractPersistentActor {
|
||||
@Override
|
||||
public String persistenceId() {
|
||||
return "123";
|
||||
}
|
||||
|
||||
// Absolute path to the journal plugin configuration entry in the `reference.conf`
|
||||
@Override
|
||||
public String journalPluginId() {
|
||||
return "akka.persistence.chronicle.journal";
|
||||
}
|
||||
|
||||
// Absolute path to the snapshot store plugin configuration entry in the `reference.conf`
|
||||
@Override
|
||||
public String snapshotPluginId() {
|
||||
return "akka.persistence.chronicle.snapshot-store";
|
||||
}
|
||||
}
|
||||
// #override-plugins
|
||||
|
||||
// #runtime-config
|
||||
abstract class AbstractPersistentActorWithRuntimePluginConfig extends AbstractPersistentActor
|
||||
implements RuntimePluginConfig {
|
||||
// Variable that is retrieved at runtime, from an external service for instance.
|
||||
String runtimeDistinction = "foo";
|
||||
|
||||
@Override
|
||||
public String persistenceId() {
|
||||
return "123";
|
||||
}
|
||||
|
||||
// Absolute path to the journal plugin configuration entry in the `reference.conf`
|
||||
@Override
|
||||
public String journalPluginId() {
|
||||
return "journal-plugin-" + runtimeDistinction;
|
||||
}
|
||||
|
||||
// Absolute path to the snapshot store plugin configuration entry in the `reference.conf`
|
||||
@Override
|
||||
public String snapshotPluginId() {
|
||||
return "snapshot-store-plugin-" + runtimeDistinction;
|
||||
}
|
||||
|
||||
// Configuration which contains the journal plugin id defined above
|
||||
@Override
|
||||
public Config journalPluginConfig() {
|
||||
return ConfigFactory.empty()
|
||||
.withValue(
|
||||
"journal-plugin-" + runtimeDistinction,
|
||||
getContext()
|
||||
.getSystem()
|
||||
.settings()
|
||||
.config()
|
||||
.getValue(
|
||||
"journal-plugin") // or a very different configuration coming from an external
|
||||
// service.
|
||||
);
|
||||
}
|
||||
|
||||
// Configuration which contains the snapshot store plugin id defined above
|
||||
@Override
|
||||
public Config snapshotPluginConfig() {
|
||||
return ConfigFactory.empty()
|
||||
.withValue(
|
||||
"snapshot-plugin-" + runtimeDistinction,
|
||||
getContext()
|
||||
.getSystem()
|
||||
.settings()
|
||||
.config()
|
||||
.getValue(
|
||||
"snapshot-store-plugin") // or a very different configuration coming from an
|
||||
// external service.
|
||||
);
|
||||
}
|
||||
}
|
||||
// #runtime-config
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,422 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.persistence;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.time.Duration;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.pekko.NotUsed;
|
||||
import org.apache.pekko.persistence.query.Sequence;
|
||||
import org.apache.pekko.persistence.query.Offset;
|
||||
import com.typesafe.config.Config;
|
||||
|
||||
import org.apache.pekko.actor.*;
|
||||
import org.apache.pekko.persistence.query.*;
|
||||
import org.apache.pekko.stream.javadsl.Sink;
|
||||
import org.apache.pekko.stream.javadsl.Source;
|
||||
|
||||
import jdocs.persistence.query.MyEventsByTagSource;
|
||||
import org.reactivestreams.Subscriber;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
public class PersistenceQueryDocTest {
|
||||
|
||||
final ActorSystem system = ActorSystem.create();
|
||||
|
||||
public
|
||||
// #advanced-journal-query-types
|
||||
static class RichEvent {
|
||||
public final Set<String> tags;
|
||||
public final Object payload;
|
||||
|
||||
public RichEvent(Set<String> tags, Object payload) {
|
||||
this.tags = tags;
|
||||
this.payload = payload;
|
||||
}
|
||||
}
|
||||
// #advanced-journal-query-types
|
||||
|
||||
public
|
||||
// #advanced-journal-query-types
|
||||
// a plugin can provide:
|
||||
static final class QueryMetadata {
|
||||
public final boolean deterministicOrder;
|
||||
public final boolean infinite;
|
||||
|
||||
public QueryMetadata(boolean deterministicOrder, boolean infinite) {
|
||||
this.deterministicOrder = deterministicOrder;
|
||||
this.infinite = infinite;
|
||||
}
|
||||
}
|
||||
// #advanced-journal-query-types
|
||||
|
||||
interface OrderCompleted {}
|
||||
|
||||
public
|
||||
// #my-read-journal
|
||||
static class MyReadJournalProvider implements ReadJournalProvider {
|
||||
private final MyJavadslReadJournal javadslReadJournal;
|
||||
|
||||
public MyReadJournalProvider(ExtendedActorSystem system, Config config) {
|
||||
this.javadslReadJournal = new MyJavadslReadJournal(system, config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyScaladslReadJournal scaladslReadJournal() {
|
||||
return new MyScaladslReadJournal(javadslReadJournal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyJavadslReadJournal javadslReadJournal() {
|
||||
return this.javadslReadJournal;
|
||||
}
|
||||
}
|
||||
// #my-read-journal
|
||||
|
||||
public
|
||||
// #my-read-journal
|
||||
static class MyJavadslReadJournal
|
||||
implements org.apache.pekko.persistence.query.javadsl.ReadJournal,
|
||||
org.apache.pekko.persistence.query.javadsl.EventsByTagQuery,
|
||||
org.apache.pekko.persistence.query.javadsl.EventsByPersistenceIdQuery,
|
||||
org.apache.pekko.persistence.query.javadsl.PersistenceIdsQuery,
|
||||
org.apache.pekko.persistence.query.javadsl.CurrentPersistenceIdsQuery {
|
||||
|
||||
private final Duration refreshInterval;
|
||||
private Connection conn;
|
||||
|
||||
public MyJavadslReadJournal(ExtendedActorSystem system, Config config) {
|
||||
refreshInterval = config.getDuration("refresh-interval");
|
||||
}
|
||||
|
||||
/**
|
||||
* You can use `NoOffset` to retrieve all events with a given tag or retrieve a subset of all
|
||||
* events by specifying a `Sequence` `offset`. The `offset` corresponds to an ordered sequence
|
||||
* number for the specific tag. Note that the corresponding offset of each event is provided in
|
||||
* the [[akka.persistence.query.EventEnvelope]], which makes it possible to resume the stream at
|
||||
* a later point from a given offset.
|
||||
*
|
||||
* <p>The `offset` is exclusive, i.e. the event with the exact same sequence number will not be
|
||||
* included in the returned stream. This means that you can use the offset that is returned in
|
||||
* `EventEnvelope` as the `offset` parameter in a subsequent query.
|
||||
*/
|
||||
@Override
|
||||
public Source<EventEnvelope, NotUsed> eventsByTag(String tag, Offset offset) {
|
||||
if (offset instanceof Sequence) {
|
||||
Sequence sequenceOffset = (Sequence) offset;
|
||||
return Source.fromGraph(
|
||||
new MyEventsByTagSource(conn, tag, sequenceOffset.value(), refreshInterval));
|
||||
} else if (offset == NoOffset.getInstance())
|
||||
return eventsByTag(tag, Offset.sequence(0L)); // recursive
|
||||
else
|
||||
throw new IllegalArgumentException(
|
||||
"MyJavadslReadJournal does not support " + offset.getClass().getName() + " offsets");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Source<EventEnvelope, NotUsed> eventsByPersistenceId(
|
||||
String persistenceId, long fromSequenceNr, long toSequenceNr) {
|
||||
// implement in a similar way as eventsByTag
|
||||
throw new UnsupportedOperationException("Not implemented yet");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Source<String, NotUsed> persistenceIds() {
|
||||
// implement in a similar way as eventsByTag
|
||||
throw new UnsupportedOperationException("Not implemented yet");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Source<String, NotUsed> currentPersistenceIds() {
|
||||
// implement in a similar way as eventsByTag
|
||||
throw new UnsupportedOperationException("Not implemented yet");
|
||||
}
|
||||
|
||||
// possibility to add more plugin specific queries
|
||||
|
||||
// #advanced-journal-query-definition
|
||||
public Source<RichEvent, QueryMetadata> byTagsWithMeta(Set<String> tags) {
|
||||
// #advanced-journal-query-definition
|
||||
// implement in a similar way as eventsByTag
|
||||
throw new UnsupportedOperationException("Not implemented yet");
|
||||
}
|
||||
}
|
||||
// #my-read-journal
|
||||
|
||||
public
|
||||
// #my-read-journal
|
||||
static class MyScaladslReadJournal
|
||||
implements org.apache.pekko.persistence.query.scaladsl.ReadJournal,
|
||||
org.apache.pekko.persistence.query.scaladsl.EventsByTagQuery,
|
||||
org.apache.pekko.persistence.query.scaladsl.EventsByPersistenceIdQuery,
|
||||
org.apache.pekko.persistence.query.scaladsl.PersistenceIdsQuery,
|
||||
org.apache.pekko.persistence.query.scaladsl.CurrentPersistenceIdsQuery {
|
||||
|
||||
private final MyJavadslReadJournal javadslReadJournal;
|
||||
|
||||
public MyScaladslReadJournal(MyJavadslReadJournal javadslReadJournal) {
|
||||
this.javadslReadJournal = javadslReadJournal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public org.apache.pekko.stream.scaladsl.Source<EventEnvelope, NotUsed> eventsByTag(
|
||||
String tag, org.apache.pekko.persistence.query.Offset offset) {
|
||||
return javadslReadJournal.eventsByTag(tag, offset).asScala();
|
||||
}
|
||||
|
||||
@Override
|
||||
public org.apache.pekko.stream.scaladsl.Source<EventEnvelope, NotUsed> eventsByPersistenceId(
|
||||
String persistenceId, long fromSequenceNr, long toSequenceNr) {
|
||||
return javadslReadJournal
|
||||
.eventsByPersistenceId(persistenceId, fromSequenceNr, toSequenceNr)
|
||||
.asScala();
|
||||
}
|
||||
|
||||
@Override
|
||||
public org.apache.pekko.stream.scaladsl.Source<String, NotUsed> persistenceIds() {
|
||||
return javadslReadJournal.persistenceIds().asScala();
|
||||
}
|
||||
|
||||
@Override
|
||||
public org.apache.pekko.stream.scaladsl.Source<String, NotUsed> currentPersistenceIds() {
|
||||
return javadslReadJournal.currentPersistenceIds().asScala();
|
||||
}
|
||||
|
||||
// possibility to add more plugin specific queries
|
||||
|
||||
public org.apache.pekko.stream.scaladsl.Source<RichEvent, QueryMetadata> byTagsWithMeta(
|
||||
scala.collection.Set<String> tags) {
|
||||
Set<String> jTags = scala.collection.JavaConverters.setAsJavaSetConverter(tags).asJava();
|
||||
return javadslReadJournal.byTagsWithMeta(jTags).asScala();
|
||||
}
|
||||
}
|
||||
// #my-read-journal
|
||||
|
||||
void demonstrateBasicUsage() {
|
||||
final ActorSystem system = ActorSystem.create();
|
||||
|
||||
// #basic-usage
|
||||
// obtain read journal by plugin id
|
||||
final MyJavadslReadJournal readJournal =
|
||||
PersistenceQuery.get(system)
|
||||
.getReadJournalFor(
|
||||
MyJavadslReadJournal.class, "akka.persistence.query.my-read-journal");
|
||||
|
||||
// issue query to journal
|
||||
Source<EventEnvelope, NotUsed> source =
|
||||
readJournal.eventsByPersistenceId("user-1337", 0, Long.MAX_VALUE);
|
||||
|
||||
// materialize stream, consuming events
|
||||
source.runForeach(event -> System.out.println("Event: " + event), system);
|
||||
// #basic-usage
|
||||
}
|
||||
|
||||
void demonstrateAllPersistenceIdsLive() {
|
||||
final MyJavadslReadJournal readJournal =
|
||||
PersistenceQuery.get(system)
|
||||
.getReadJournalFor(
|
||||
MyJavadslReadJournal.class, "akka.persistence.query.my-read-journal");
|
||||
|
||||
// #all-persistence-ids-live
|
||||
readJournal.persistenceIds();
|
||||
// #all-persistence-ids-live
|
||||
}
|
||||
|
||||
void demonstrateNoRefresh() {
|
||||
final ActorSystem system = ActorSystem.create();
|
||||
|
||||
final MyJavadslReadJournal readJournal =
|
||||
PersistenceQuery.get(system)
|
||||
.getReadJournalFor(
|
||||
MyJavadslReadJournal.class, "akka.persistence.query.my-read-journal");
|
||||
|
||||
// #all-persistence-ids-snap
|
||||
readJournal.currentPersistenceIds();
|
||||
// #all-persistence-ids-snap
|
||||
}
|
||||
|
||||
void demonstrateRefresh() {
|
||||
final ActorSystem system = ActorSystem.create();
|
||||
|
||||
final MyJavadslReadJournal readJournal =
|
||||
PersistenceQuery.get(system)
|
||||
.getReadJournalFor(
|
||||
MyJavadslReadJournal.class, "akka.persistence.query.my-read-journal");
|
||||
|
||||
// #events-by-persistent-id
|
||||
readJournal.eventsByPersistenceId("user-us-1337", 0L, Long.MAX_VALUE);
|
||||
// #events-by-persistent-id
|
||||
}
|
||||
|
||||
void demonstrateEventsByTag() {
|
||||
final ActorSystem system = ActorSystem.create();
|
||||
|
||||
final MyJavadslReadJournal readJournal =
|
||||
PersistenceQuery.get(system)
|
||||
.getReadJournalFor(
|
||||
MyJavadslReadJournal.class, "akka.persistence.query.my-read-journal");
|
||||
|
||||
// #events-by-tag
|
||||
// assuming journal is able to work with numeric offsets we can:
|
||||
final Source<EventEnvelope, NotUsed> completedOrders =
|
||||
readJournal.eventsByTag("order-completed", new Sequence(0L));
|
||||
|
||||
// find first 10 completed orders:
|
||||
final CompletionStage<List<OrderCompleted>> firstCompleted =
|
||||
completedOrders
|
||||
.map(EventEnvelope::event)
|
||||
.collectType(OrderCompleted.class)
|
||||
.take(10) // cancels the query stream after pulling 10 elements
|
||||
.runFold(
|
||||
new ArrayList<>(10),
|
||||
(acc, e) -> {
|
||||
acc.add(e);
|
||||
return acc;
|
||||
},
|
||||
system);
|
||||
|
||||
// start another query, from the known offset
|
||||
Source<EventEnvelope, NotUsed> furtherOrders =
|
||||
readJournal.eventsByTag("order-completed", new Sequence(10));
|
||||
// #events-by-tag
|
||||
}
|
||||
|
||||
void demonstrateMaterializedQueryValues() {
|
||||
final ActorSystem system = ActorSystem.create();
|
||||
|
||||
final MyJavadslReadJournal readJournal =
|
||||
PersistenceQuery.get(system)
|
||||
.getReadJournalFor(
|
||||
MyJavadslReadJournal.class, "akka.persistence.query.my-read-journal");
|
||||
|
||||
// #advanced-journal-query-usage
|
||||
|
||||
Set<String> tags = new HashSet<String>();
|
||||
tags.add("red");
|
||||
tags.add("blue");
|
||||
final Source<RichEvent, QueryMetadata> events =
|
||||
readJournal
|
||||
.byTagsWithMeta(tags)
|
||||
.mapMaterializedValue(
|
||||
meta -> {
|
||||
System.out.println(
|
||||
"The query is: "
|
||||
+ "ordered deterministically: "
|
||||
+ meta.deterministicOrder
|
||||
+ " "
|
||||
+ "infinite: "
|
||||
+ meta.infinite);
|
||||
return meta;
|
||||
});
|
||||
|
||||
events
|
||||
.map(
|
||||
event -> {
|
||||
System.out.println("Event payload: " + event.payload);
|
||||
return event.payload;
|
||||
})
|
||||
.runWith(Sink.ignore(), system);
|
||||
|
||||
// #advanced-journal-query-usage
|
||||
}
|
||||
|
||||
class ReactiveStreamsCompatibleDBDriver {
|
||||
Subscriber<List<Object>> batchWriter() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
void demonstrateWritingIntoDifferentStore() {
|
||||
final ActorSystem system = ActorSystem.create();
|
||||
|
||||
final MyJavadslReadJournal readJournal =
|
||||
PersistenceQuery.get(system)
|
||||
.getReadJournalFor(
|
||||
MyJavadslReadJournal.class, "akka.persistence.query.my-read-journal");
|
||||
|
||||
// #projection-into-different-store-rs
|
||||
final ReactiveStreamsCompatibleDBDriver driver = new ReactiveStreamsCompatibleDBDriver();
|
||||
final Subscriber<List<Object>> dbBatchWriter = driver.batchWriter();
|
||||
|
||||
// Using an example (Reactive Streams) Database driver
|
||||
readJournal
|
||||
.eventsByPersistenceId("user-1337", 0L, Long.MAX_VALUE)
|
||||
.map(envelope -> envelope.event())
|
||||
.grouped(20) // batch inserts into groups of 20
|
||||
.runWith(Sink.fromSubscriber(dbBatchWriter), system); // write batches to read-side database
|
||||
// #projection-into-different-store-rs
|
||||
}
|
||||
|
||||
// #projection-into-different-store-simple-classes
|
||||
static class ExampleStore {
|
||||
CompletionStage<Void> save(Object any) {
|
||||
// ...
|
||||
// #projection-into-different-store-simple-classes
|
||||
return null;
|
||||
// #projection-into-different-store-simple-classes
|
||||
}
|
||||
}
|
||||
// #projection-into-different-store-simple-classes
|
||||
|
||||
void demonstrateWritingIntoDifferentStoreWithMapAsync() {
|
||||
final ActorSystem system = ActorSystem.create();
|
||||
|
||||
final MyJavadslReadJournal readJournal =
|
||||
PersistenceQuery.get(system)
|
||||
.getReadJournalFor(
|
||||
MyJavadslReadJournal.class, "akka.persistence.query.my-read-journal");
|
||||
|
||||
// #projection-into-different-store-simple
|
||||
final ExampleStore store = new ExampleStore();
|
||||
|
||||
readJournal
|
||||
.eventsByTag("bid", new Sequence(0L))
|
||||
.mapAsync(1, store::save)
|
||||
.runWith(Sink.ignore(), system);
|
||||
// #projection-into-different-store-simple
|
||||
}
|
||||
|
||||
// #projection-into-different-store
|
||||
static class MyResumableProjection {
|
||||
private final String name;
|
||||
|
||||
public MyResumableProjection(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public CompletionStage<Long> saveProgress(Offset offset) {
|
||||
// ...
|
||||
// #projection-into-different-store
|
||||
return null;
|
||||
// #projection-into-different-store
|
||||
}
|
||||
|
||||
public CompletionStage<Long> latestOffset() {
|
||||
// ...
|
||||
// #projection-into-different-store
|
||||
return null;
|
||||
// #projection-into-different-store
|
||||
}
|
||||
}
|
||||
// #projection-into-different-store
|
||||
|
||||
static class ComplexState {
|
||||
|
||||
boolean readyToSave() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static class Record {
|
||||
static Record of(Object any) {
|
||||
return new Record();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,547 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.persistence;
|
||||
|
||||
import docs.persistence.ExampleJsonMarshaller;
|
||||
import docs.persistence.proto.FlightAppModels;
|
||||
|
||||
import java.io.NotSerializableException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import spray.json.JsObject;
|
||||
|
||||
import org.apache.pekko.persistence.journal.EventAdapter;
|
||||
import org.apache.pekko.persistence.journal.EventSeq;
|
||||
import org.apache.pekko.protobufv3.internal.InvalidProtocolBufferException;
|
||||
import org.apache.pekko.serialization.SerializerWithStringManifest;
|
||||
|
||||
public class PersistenceSchemaEvolutionDocTest {
|
||||
|
||||
public
|
||||
// #protobuf-read-optional-model
|
||||
static enum SeatType {
|
||||
Window("W"),
|
||||
Aisle("A"),
|
||||
Other("O"),
|
||||
Unknown("");
|
||||
|
||||
private final String code;
|
||||
|
||||
private SeatType(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public static SeatType fromCode(String c) {
|
||||
if (Window.code.equals(c)) return Window;
|
||||
else if (Aisle.code.equals(c)) return Aisle;
|
||||
else if (Other.code.equals(c)) return Other;
|
||||
else return Unknown;
|
||||
}
|
||||
}
|
||||
// #protobuf-read-optional-model
|
||||
|
||||
public
|
||||
// #protobuf-read-optional-model
|
||||
static class SeatReserved {
|
||||
public final String letter;
|
||||
public final int row;
|
||||
public final SeatType seatType;
|
||||
|
||||
public SeatReserved(String letter, int row, SeatType seatType) {
|
||||
this.letter = letter;
|
||||
this.row = row;
|
||||
this.seatType = seatType;
|
||||
}
|
||||
}
|
||||
// #protobuf-read-optional-model
|
||||
|
||||
public
|
||||
// #protobuf-read-optional
|
||||
/**
|
||||
* Example serializer impl which uses protocol buffers generated classes (proto.*) to perform the
|
||||
* to/from binary marshalling.
|
||||
*/
|
||||
static class AddedFieldsSerializerWithProtobuf extends SerializerWithStringManifest {
|
||||
@Override
|
||||
public int identifier() {
|
||||
return 67876;
|
||||
}
|
||||
|
||||
private final String seatReservedManifest = SeatReserved.class.getName();
|
||||
|
||||
@Override
|
||||
public String manifest(Object o) {
|
||||
return o.getClass().getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object fromBinary(byte[] bytes, String manifest) throws NotSerializableException {
|
||||
if (seatReservedManifest.equals(manifest)) {
|
||||
// use generated protobuf serializer
|
||||
try {
|
||||
return seatReserved(FlightAppModels.SeatReserved.parseFrom(bytes));
|
||||
} catch (InvalidProtocolBufferException e) {
|
||||
throw new IllegalArgumentException(e.getMessage());
|
||||
}
|
||||
} else {
|
||||
throw new NotSerializableException("Unable to handle manifest: " + manifest);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBinary(Object o) {
|
||||
if (o instanceof SeatReserved) {
|
||||
SeatReserved s = (SeatReserved) o;
|
||||
return FlightAppModels.SeatReserved.newBuilder()
|
||||
.setRow(s.row)
|
||||
.setLetter(s.letter)
|
||||
.setSeatType(s.seatType.code)
|
||||
.build()
|
||||
.toByteArray();
|
||||
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unable to handle: " + o);
|
||||
}
|
||||
}
|
||||
|
||||
// -- fromBinary helpers --
|
||||
|
||||
private SeatReserved seatReserved(FlightAppModels.SeatReserved p) {
|
||||
return new SeatReserved(p.getLetter(), p.getRow(), seatType(p));
|
||||
}
|
||||
|
||||
// handle missing field by assigning "Unknown" value
|
||||
private SeatType seatType(FlightAppModels.SeatReserved p) {
|
||||
if (p.hasSeatType()) return SeatType.fromCode(p.getSeatType());
|
||||
else return SeatType.Unknown;
|
||||
}
|
||||
}
|
||||
// #protobuf-read-optional
|
||||
|
||||
public static class RenamePlainJson {
|
||||
public
|
||||
// #rename-plain-json
|
||||
static class JsonRenamedFieldAdapter implements EventAdapter {
|
||||
// use your favorite json library
|
||||
private final ExampleJsonMarshaller marshaller = new ExampleJsonMarshaller();
|
||||
|
||||
private final String V1 = "v1";
|
||||
private final String V2 = "v2";
|
||||
|
||||
// this could be done independently for each event type
|
||||
@Override
|
||||
public String manifest(Object event) {
|
||||
return V2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsObject toJournal(Object event) {
|
||||
return marshaller.toJson(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EventSeq fromJournal(Object event, String manifest) {
|
||||
if (event instanceof JsObject) {
|
||||
JsObject json = (JsObject) event;
|
||||
if (V1.equals(manifest)) json = rename(json, "code", "seatNr");
|
||||
return EventSeq.single(json);
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
"Can only work with JSON, was: " + event.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
private JsObject rename(JsObject json, String from, String to) {
|
||||
// use your favorite json library to rename the field
|
||||
JsObject renamed = json;
|
||||
return renamed;
|
||||
}
|
||||
}
|
||||
// #rename-plain-json
|
||||
}
|
||||
|
||||
public static class SimplestCustomSerializer {
|
||||
|
||||
public
|
||||
// #simplest-custom-serializer-model
|
||||
static class Person {
|
||||
public final String name;
|
||||
public final String surname;
|
||||
|
||||
public Person(String name, String surname) {
|
||||
this.name = name;
|
||||
this.surname = surname;
|
||||
}
|
||||
}
|
||||
// #simplest-custom-serializer-model
|
||||
|
||||
public
|
||||
// #simplest-custom-serializer
|
||||
/**
|
||||
* Simplest possible serializer, uses a string representation of the Person class.
|
||||
*
|
||||
* <p>Usually a serializer like this would use a library like: protobuf, kryo, avro, cap'n
|
||||
* proto, flatbuffers, SBE or some other dedicated serializer backend to perform the actual
|
||||
* to/from bytes marshalling.
|
||||
*/
|
||||
static class SimplestPossiblePersonSerializer extends SerializerWithStringManifest {
|
||||
private final Charset utf8 = StandardCharsets.UTF_8;
|
||||
|
||||
private final String personManifest = Person.class.getName();
|
||||
|
||||
// unique identifier of the serializer
|
||||
@Override
|
||||
public int identifier() {
|
||||
return 1234567;
|
||||
}
|
||||
|
||||
// extract manifest to be stored together with serialized object
|
||||
@Override
|
||||
public String manifest(Object o) {
|
||||
return o.getClass().getName();
|
||||
}
|
||||
|
||||
// serialize the object
|
||||
@Override
|
||||
public byte[] toBinary(Object obj) {
|
||||
if (obj instanceof Person) {
|
||||
Person p = (Person) obj;
|
||||
return (p.name + "|" + p.surname).getBytes(utf8);
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
"Unable to serialize to bytes, clazz was: " + obj.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
// deserialize the object, using the manifest to indicate which logic to apply
|
||||
@Override
|
||||
public Object fromBinary(byte[] bytes, String manifest) throws NotSerializableException {
|
||||
if (personManifest.equals(manifest)) {
|
||||
String nameAndSurname = new String(bytes, utf8);
|
||||
String[] parts = nameAndSurname.split("[|]");
|
||||
return new Person(parts[0], parts[1]);
|
||||
} else {
|
||||
throw new NotSerializableException(
|
||||
"Unable to deserialize from bytes, manifest was: "
|
||||
+ manifest
|
||||
+ "! Bytes length: "
|
||||
+ bytes.length);
|
||||
}
|
||||
}
|
||||
}
|
||||
// #simplest-custom-serializer
|
||||
}
|
||||
|
||||
public static class SamplePayload {
|
||||
private final Object payload;
|
||||
|
||||
public SamplePayload(Object payload) {
|
||||
this.payload = payload;
|
||||
}
|
||||
|
||||
public Object getPayload() {
|
||||
return payload;
|
||||
}
|
||||
}
|
||||
|
||||
// #split-events-during-recovery
|
||||
interface Version1 {};
|
||||
|
||||
interface Version2 {}
|
||||
|
||||
// #split-events-during-recovery
|
||||
public
|
||||
// #split-events-during-recovery
|
||||
// V1 event:
|
||||
static class UserDetailsChanged implements Version1 {
|
||||
public final String name;
|
||||
public final String address;
|
||||
|
||||
public UserDetailsChanged(String name, String address) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
|
||||
// #split-events-during-recovery
|
||||
public
|
||||
// #split-events-during-recovery
|
||||
// corresponding V2 events:
|
||||
static class UserNameChanged implements Version2 {
|
||||
public final String name;
|
||||
|
||||
public UserNameChanged(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
// #split-events-during-recovery
|
||||
public
|
||||
// #split-events-during-recovery
|
||||
static class UserAddressChanged implements Version2 {
|
||||
public final String address;
|
||||
|
||||
public UserAddressChanged(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
|
||||
// #split-events-during-recovery
|
||||
public
|
||||
// #split-events-during-recovery
|
||||
// event splitting adapter:
|
||||
static class UserEventsAdapter implements EventAdapter {
|
||||
@Override
|
||||
public String manifest(Object event) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public EventSeq fromJournal(Object event, String manifest) {
|
||||
if (event instanceof UserDetailsChanged) {
|
||||
UserDetailsChanged c = (UserDetailsChanged) event;
|
||||
if (c.name == null) return EventSeq.single(new UserAddressChanged(c.address));
|
||||
else if (c.address == null) return EventSeq.single(new UserNameChanged(c.name));
|
||||
else return EventSeq.create(new UserNameChanged(c.name), new UserAddressChanged(c.address));
|
||||
} else {
|
||||
return EventSeq.single(event);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toJournal(Object event) {
|
||||
return event;
|
||||
}
|
||||
}
|
||||
// #split-events-during-recovery
|
||||
|
||||
public static class CustomerBlinked {
|
||||
public final long customerId;
|
||||
|
||||
public CustomerBlinked(long customerId) {
|
||||
this.customerId = customerId;
|
||||
}
|
||||
}
|
||||
|
||||
public
|
||||
// #string-serializer-skip-deleved-event-by-manifest
|
||||
static class EventDeserializationSkipped {
|
||||
public static EventDeserializationSkipped instance = new EventDeserializationSkipped();
|
||||
|
||||
private EventDeserializationSkipped() {}
|
||||
}
|
||||
|
||||
// #string-serializer-skip-deleved-event-by-manifest
|
||||
public
|
||||
// #string-serializer-skip-deleved-event-by-manifest
|
||||
static class RemovedEventsAwareSerializer extends SerializerWithStringManifest {
|
||||
private final Charset utf8 = StandardCharsets.UTF_8;
|
||||
private final String customerBlinkedManifest = "blinked";
|
||||
|
||||
// unique identifier of the serializer
|
||||
@Override
|
||||
public int identifier() {
|
||||
return 8337;
|
||||
}
|
||||
|
||||
// extract manifest to be stored together with serialized object
|
||||
@Override
|
||||
public String manifest(Object o) {
|
||||
if (o instanceof CustomerBlinked) return customerBlinkedManifest;
|
||||
else return o.getClass().getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBinary(Object o) {
|
||||
return o.toString().getBytes(utf8); // example serialization
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object fromBinary(byte[] bytes, String manifest) {
|
||||
if (customerBlinkedManifest.equals(manifest)) return EventDeserializationSkipped.instance;
|
||||
else return new String(bytes, utf8);
|
||||
}
|
||||
}
|
||||
// #string-serializer-skip-deleved-event-by-manifest
|
||||
|
||||
public
|
||||
// #string-serializer-skip-deleved-event-by-manifest-adapter
|
||||
static class SkippedEventsAwareAdapter implements EventAdapter {
|
||||
@Override
|
||||
public String manifest(Object event) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toJournal(Object event) {
|
||||
return event;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EventSeq fromJournal(Object event, String manifest) {
|
||||
if (event == EventDeserializationSkipped.instance) return EventSeq.empty();
|
||||
else return EventSeq.single(event);
|
||||
}
|
||||
}
|
||||
// #string-serializer-skip-deleved-event-by-manifest-adapter
|
||||
|
||||
// #string-serializer-handle-rename
|
||||
public
|
||||
// #string-serializer-handle-rename
|
||||
static class RenamedEventAwareSerializer extends SerializerWithStringManifest {
|
||||
private final Charset utf8 = StandardCharsets.UTF_8;
|
||||
|
||||
// unique identifier of the serializer
|
||||
@Override
|
||||
public int identifier() {
|
||||
return 8337;
|
||||
}
|
||||
|
||||
private final String oldPayloadClassName =
|
||||
"docs.persistence.OldPayload"; // class NOT available anymore
|
||||
private final String myPayloadClassName = SamplePayload.class.getName();
|
||||
|
||||
// extract manifest to be stored together with serialized object
|
||||
@Override
|
||||
public String manifest(Object o) {
|
||||
return o.getClass().getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBinary(Object o) {
|
||||
if (o instanceof SamplePayload) {
|
||||
SamplePayload s = (SamplePayload) o;
|
||||
return s.payload.toString().getBytes(utf8);
|
||||
} else {
|
||||
// previously also handled "old" events here.
|
||||
throw new IllegalArgumentException(
|
||||
"Unable to serialize to bytes, clazz was: " + o.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object fromBinary(byte[] bytes, String manifest) throws NotSerializableException {
|
||||
if (oldPayloadClassName.equals(manifest)) return new SamplePayload(new String(bytes, utf8));
|
||||
else if (myPayloadClassName.equals(manifest))
|
||||
return new SamplePayload(new String(bytes, utf8));
|
||||
else throw new NotSerializableException("unexpected manifest [" + manifest + "]");
|
||||
}
|
||||
}
|
||||
// #string-serializer-handle-rename
|
||||
|
||||
public
|
||||
// #detach-models
|
||||
// Domain model - highly optimised for domain language and maybe "fluent" usage
|
||||
static class Customer {
|
||||
public final String name;
|
||||
|
||||
public Customer(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
// #detach-models
|
||||
public
|
||||
// #detach-models
|
||||
static class Seat {
|
||||
public final String code;
|
||||
|
||||
public Seat(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public SeatBooked bookFor(Customer customer) {
|
||||
return new SeatBooked(code, customer);
|
||||
}
|
||||
}
|
||||
|
||||
// #detach-models
|
||||
public
|
||||
// #detach-models
|
||||
static class SeatBooked {
|
||||
public final String code;
|
||||
public final Customer customer;
|
||||
|
||||
public SeatBooked(String code, Customer customer) {
|
||||
this.code = code;
|
||||
this.customer = customer;
|
||||
}
|
||||
}
|
||||
|
||||
// #detach-models
|
||||
public
|
||||
// #detach-models
|
||||
// Data model - highly optimised for schema evolution and persistence
|
||||
static class SeatBookedData {
|
||||
public final String code;
|
||||
public final String customerName;
|
||||
|
||||
public SeatBookedData(String code, String customerName) {
|
||||
this.code = code;
|
||||
this.customerName = customerName;
|
||||
}
|
||||
}
|
||||
// #detach-models
|
||||
|
||||
// #detach-models-adapter
|
||||
class DetachedModelsAdapter implements EventAdapter {
|
||||
@Override
|
||||
public String manifest(Object event) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toJournal(Object event) {
|
||||
if (event instanceof SeatBooked) {
|
||||
SeatBooked s = (SeatBooked) event;
|
||||
return new SeatBookedData(s.code, s.customer.name);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unsupported: " + event.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public EventSeq fromJournal(Object event, String manifest) {
|
||||
if (event instanceof SeatBookedData) {
|
||||
SeatBookedData d = (SeatBookedData) event;
|
||||
return EventSeq.single(new SeatBooked(d.code, new Customer(d.customerName)));
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unsupported: " + event.getClass());
|
||||
}
|
||||
}
|
||||
}
|
||||
// #detach-models-adapter
|
||||
|
||||
public
|
||||
// #detach-models-adapter-json
|
||||
static class JsonDataModelAdapter implements EventAdapter {
|
||||
|
||||
// use your favorite json library
|
||||
private final ExampleJsonMarshaller marshaller = new ExampleJsonMarshaller();
|
||||
|
||||
@Override
|
||||
public String manifest(Object event) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsObject toJournal(Object event) {
|
||||
return marshaller.toJson(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EventSeq fromJournal(Object event, String manifest) {
|
||||
if (event instanceof JsObject) {
|
||||
JsObject json = (JsObject) event;
|
||||
return EventSeq.single(marshaller.fromJson(json));
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
"Unable to fromJournal a non-JSON object! Was: " + event.getClass());
|
||||
}
|
||||
}
|
||||
}
|
||||
// #detach-models-adapter-json
|
||||
|
||||
}
|
||||
134
docs/src/test/java/jdocs/persistence/PersistentActorExample.java
Normal file
134
docs/src/test/java/jdocs/persistence/PersistentActorExample.java
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.persistence;
|
||||
|
||||
// #persistent-actor-example
|
||||
|
||||
import org.apache.pekko.actor.ActorRef;
|
||||
import org.apache.pekko.actor.ActorSystem;
|
||||
import org.apache.pekko.actor.Props;
|
||||
import org.apache.pekko.persistence.AbstractPersistentActor;
|
||||
import org.apache.pekko.persistence.SnapshotOffer;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
class Cmd implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private final String data;
|
||||
|
||||
public Cmd(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class Evt implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private final String data;
|
||||
|
||||
public Evt(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class ExampleState implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private final ArrayList<String> events;
|
||||
|
||||
public ExampleState() {
|
||||
this(new ArrayList<>());
|
||||
}
|
||||
|
||||
public ExampleState(ArrayList<String> events) {
|
||||
this.events = events;
|
||||
}
|
||||
|
||||
public ExampleState copy() {
|
||||
return new ExampleState(new ArrayList<>(events));
|
||||
}
|
||||
|
||||
public void update(Evt evt) {
|
||||
events.add(evt.getData());
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return events.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return events.toString();
|
||||
}
|
||||
}
|
||||
|
||||
class ExamplePersistentActor extends AbstractPersistentActor {
|
||||
|
||||
private ExampleState state = new ExampleState();
|
||||
private int snapShotInterval = 1000;
|
||||
|
||||
public int getNumEvents() {
|
||||
return state.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String persistenceId() {
|
||||
return "sample-id-1";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceiveRecover() {
|
||||
return receiveBuilder()
|
||||
.match(Evt.class, state::update)
|
||||
.match(SnapshotOffer.class, ss -> state = (ExampleState) ss.snapshot())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
Cmd.class,
|
||||
c -> {
|
||||
final String data = c.getData();
|
||||
final Evt evt = new Evt(data + "-" + getNumEvents());
|
||||
persist(
|
||||
evt,
|
||||
(Evt e) -> {
|
||||
state.update(e);
|
||||
getContext().getSystem().getEventStream().publish(e);
|
||||
if (lastSequenceNr() % snapShotInterval == 0 && lastSequenceNr() != 0)
|
||||
// IMPORTANT: create a copy of snapshot because ExampleState is mutable
|
||||
saveSnapshot(state.copy());
|
||||
});
|
||||
})
|
||||
.matchEquals("print", s -> System.out.println(state))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
// #persistent-actor-example
|
||||
|
||||
public class PersistentActorExample {
|
||||
public static void main(String... args) throws Exception {
|
||||
final ActorSystem system = ActorSystem.create("example");
|
||||
final ActorRef persistentActor =
|
||||
system.actorOf(Props.create(ExamplePersistentActor.class), "persistentActor-4-java8");
|
||||
persistentActor.tell(new Cmd("foo"), null);
|
||||
persistentActor.tell(new Cmd("baz"), null);
|
||||
persistentActor.tell(new Cmd("bar"), null);
|
||||
persistentActor.tell(new Cmd("buzz"), null);
|
||||
persistentActor.tell("print", null);
|
||||
|
||||
Thread.sleep(10000);
|
||||
system.terminate();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.persistence.query;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.pekko.NotUsed;
|
||||
import org.apache.pekko.actor.ActorSystem;
|
||||
import org.apache.pekko.persistence.journal.WriteEventAdapter;
|
||||
import org.apache.pekko.persistence.journal.Tagged;
|
||||
import org.apache.pekko.persistence.query.EventEnvelope;
|
||||
import org.apache.pekko.persistence.query.Sequence;
|
||||
import org.apache.pekko.persistence.query.PersistenceQuery;
|
||||
import org.apache.pekko.persistence.query.journal.leveldb.javadsl.LeveldbReadJournal;
|
||||
import org.apache.pekko.stream.javadsl.Source;
|
||||
|
||||
public class LeveldbPersistenceQueryDocTest {
|
||||
|
||||
final ActorSystem system = ActorSystem.create();
|
||||
|
||||
public void demonstrateReadJournal() {
|
||||
// #get-read-journal
|
||||
LeveldbReadJournal queries =
|
||||
PersistenceQuery.get(system)
|
||||
.getReadJournalFor(LeveldbReadJournal.class, LeveldbReadJournal.Identifier());
|
||||
// #get-read-journal
|
||||
}
|
||||
|
||||
public void demonstrateEventsByPersistenceId() {
|
||||
// #EventsByPersistenceId
|
||||
LeveldbReadJournal queries =
|
||||
PersistenceQuery.get(system)
|
||||
.getReadJournalFor(LeveldbReadJournal.class, LeveldbReadJournal.Identifier());
|
||||
|
||||
Source<EventEnvelope, NotUsed> source =
|
||||
queries.eventsByPersistenceId("some-persistence-id", 0, Long.MAX_VALUE);
|
||||
// #EventsByPersistenceId
|
||||
}
|
||||
|
||||
public void demonstrateAllPersistenceIds() {
|
||||
// #AllPersistenceIds
|
||||
LeveldbReadJournal queries =
|
||||
PersistenceQuery.get(system)
|
||||
.getReadJournalFor(LeveldbReadJournal.class, LeveldbReadJournal.Identifier());
|
||||
|
||||
Source<String, NotUsed> source = queries.persistenceIds();
|
||||
// #AllPersistenceIds
|
||||
}
|
||||
|
||||
public void demonstrateEventsByTag() {
|
||||
// #EventsByTag
|
||||
LeveldbReadJournal queries =
|
||||
PersistenceQuery.get(system)
|
||||
.getReadJournalFor(LeveldbReadJournal.class, LeveldbReadJournal.Identifier());
|
||||
|
||||
Source<EventEnvelope, NotUsed> source = queries.eventsByTag("green", new Sequence(0L));
|
||||
// #EventsByTag
|
||||
}
|
||||
|
||||
public
|
||||
// #tagger
|
||||
static class MyTaggingEventAdapter implements WriteEventAdapter {
|
||||
|
||||
@Override
|
||||
public Object toJournal(Object event) {
|
||||
if (event instanceof String) {
|
||||
String s = (String) event;
|
||||
Set<String> tags = new HashSet<String>();
|
||||
if (s.contains("green")) tags.add("green");
|
||||
if (s.contains("black")) tags.add("black");
|
||||
if (s.contains("blue")) tags.add("blue");
|
||||
if (tags.isEmpty()) return event;
|
||||
else return new Tagged(event, tags);
|
||||
} else {
|
||||
return event;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String manifest(Object event) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
// #tagger
|
||||
}
|
||||
|
|
@ -0,0 +1,133 @@
|
|||
/*
|
||||
* Copyright (C) 2019-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.persistence.query;
|
||||
|
||||
import org.apache.pekko.actor.ActorSystem;
|
||||
import org.apache.pekko.persistence.query.EventEnvelope;
|
||||
import org.apache.pekko.persistence.query.Offset;
|
||||
import org.apache.pekko.serialization.Serialization;
|
||||
import org.apache.pekko.serialization.SerializationExtension;
|
||||
import org.apache.pekko.stream.*;
|
||||
import org.apache.pekko.stream.stage.*;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
// #events-by-tag-publisher
|
||||
public class MyEventsByTagSource extends GraphStage<SourceShape<EventEnvelope>> {
|
||||
public Outlet<EventEnvelope> out = Outlet.create("MyEventByTagSource.out");
|
||||
private static final String QUERY =
|
||||
"SELECT id, persistence_id, seq_nr, serializer_id, serializer_manifest, payload "
|
||||
+ "FROM journal WHERE tag = ? AND id > ? "
|
||||
+ "ORDER BY id LIMIT ?";
|
||||
|
||||
enum Continue {
|
||||
INSTANCE;
|
||||
}
|
||||
|
||||
private static final int LIMIT = 1000;
|
||||
private final Connection connection;
|
||||
private final String tag;
|
||||
private final long initialOffset;
|
||||
private final Duration refreshInterval;
|
||||
|
||||
// assumes a shared connection, could also be a factory for creating connections/pool
|
||||
public MyEventsByTagSource(
|
||||
Connection connection, String tag, long initialOffset, Duration refreshInterval) {
|
||||
this.connection = connection;
|
||||
this.tag = tag;
|
||||
this.initialOffset = initialOffset;
|
||||
this.refreshInterval = refreshInterval;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Attributes initialAttributes() {
|
||||
return Attributes.apply(ActorAttributes.IODispatcher());
|
||||
}
|
||||
|
||||
@Override
|
||||
public SourceShape<EventEnvelope> shape() {
|
||||
return SourceShape.of(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GraphStageLogic createLogic(Attributes inheritedAttributes) {
|
||||
return new TimerGraphStageLogic(shape()) {
|
||||
private ActorSystem system = materializer().system();
|
||||
private long currentOffset = initialOffset;
|
||||
private List<EventEnvelope> buf = new LinkedList<>();
|
||||
private final Serialization serialization = SerializationExtension.get(system);
|
||||
|
||||
@Override
|
||||
public void preStart() {
|
||||
scheduleWithFixedDelay(Continue.INSTANCE, refreshInterval, refreshInterval);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTimer(Object timerKey) {
|
||||
query();
|
||||
deliver();
|
||||
}
|
||||
|
||||
private void deliver() {
|
||||
if (isAvailable(out) && !buf.isEmpty()) {
|
||||
push(out, buf.remove(0));
|
||||
}
|
||||
}
|
||||
|
||||
private void query() {
|
||||
if (buf.isEmpty()) {
|
||||
|
||||
try (PreparedStatement s = connection.prepareStatement(QUERY)) {
|
||||
s.setString(1, tag);
|
||||
s.setLong(2, currentOffset);
|
||||
s.setLong(3, LIMIT);
|
||||
try (ResultSet rs = s.executeQuery()) {
|
||||
final List<EventEnvelope> res = new ArrayList<>(LIMIT);
|
||||
while (rs.next()) {
|
||||
Object deserialized =
|
||||
serialization
|
||||
.deserialize(
|
||||
rs.getBytes("payload"),
|
||||
rs.getInt("serializer_id"),
|
||||
rs.getString("serializer_manifest"))
|
||||
.get();
|
||||
currentOffset = rs.getLong("id");
|
||||
res.add(
|
||||
new EventEnvelope(
|
||||
Offset.sequence(currentOffset),
|
||||
rs.getString("persistence_id"),
|
||||
rs.getLong("seq_nr"),
|
||||
deserialized,
|
||||
System.currentTimeMillis()));
|
||||
}
|
||||
buf = res;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
failStage(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
setHandler(
|
||||
out,
|
||||
new AbstractOutHandler() {
|
||||
@Override
|
||||
public void onPull() {
|
||||
query();
|
||||
deliver();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
// #events-by-tag-publisher
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Copyright (C) 2019-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.persistence.testkit;
|
||||
|
||||
import org.apache.pekko.actor.typed.ActorSystem;
|
||||
import org.apache.pekko.actor.typed.Behavior;
|
||||
import org.apache.pekko.persistence.testkit.PersistenceTestKitPlugin;
|
||||
import org.apache.pekko.persistence.testkit.PersistenceTestKitSnapshotPlugin;
|
||||
import org.apache.pekko.persistence.testkit.javadsl.PersistenceTestKit;
|
||||
import org.apache.pekko.persistence.testkit.javadsl.SnapshotTestKit;
|
||||
import com.typesafe.config.Config;
|
||||
import com.typesafe.config.ConfigFactory;
|
||||
|
||||
public class Configuration {
|
||||
|
||||
// #testkit-typed-conf
|
||||
public class PersistenceTestKitConfig {
|
||||
|
||||
Config conf =
|
||||
PersistenceTestKitPlugin.getInstance()
|
||||
.config()
|
||||
.withFallback(ConfigFactory.defaultApplication());
|
||||
|
||||
ActorSystem<Command> system = ActorSystem.create(new SomeBehavior(), "example", conf);
|
||||
|
||||
PersistenceTestKit testKit = PersistenceTestKit.create(system);
|
||||
}
|
||||
// #testkit-typed-conf
|
||||
|
||||
// #snapshot-typed-conf
|
||||
public class SnapshotTestKitConfig {
|
||||
|
||||
Config conf =
|
||||
PersistenceTestKitSnapshotPlugin.getInstance()
|
||||
.config()
|
||||
.withFallback(ConfigFactory.defaultApplication());
|
||||
|
||||
ActorSystem<Command> system = ActorSystem.create(new SomeBehavior(), "example", conf);
|
||||
|
||||
SnapshotTestKit testKit = SnapshotTestKit.create(system);
|
||||
}
|
||||
// #snapshot-typed-conf
|
||||
|
||||
}
|
||||
|
||||
class SomeBehavior extends Behavior<Command> {
|
||||
public SomeBehavior() {
|
||||
super(1);
|
||||
}
|
||||
}
|
||||
|
||||
class Command {}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Copyright (C) 2020-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.persistence.testkit;
|
||||
|
||||
import org.apache.pekko.actor.testkit.typed.javadsl.TestKitJunitResource;
|
||||
|
||||
import com.typesafe.config.ConfigFactory;
|
||||
import jdocs.AbstractJavaTest;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
// #imports
|
||||
import org.apache.pekko.persistence.testkit.javadsl.PersistenceInit;
|
||||
import org.apache.pekko.Done;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
// #imports
|
||||
|
||||
public class PersistenceInitTest extends AbstractJavaTest {
|
||||
@ClassRule
|
||||
public static final TestKitJunitResource testKit =
|
||||
new TestKitJunitResource(
|
||||
ConfigFactory.parseString(
|
||||
"akka.persistence.journal.plugin = \"akka.persistence.journal.inmem\" \n"
|
||||
+ "akka.persistence.journal.inmem.test-serialization = on \n"
|
||||
+ "akka.persistence.snapshot-store.plugin = \"akka.persistence.snapshot-store.local\" \n"
|
||||
+ "akka.persistence.snapshot-store.local.dir = \"target/snapshot-"
|
||||
+ UUID.randomUUID().toString()
|
||||
+ "\" \n")
|
||||
.withFallback(ConfigFactory.defaultApplication()));
|
||||
|
||||
@Test
|
||||
public void testInit() throws Exception {
|
||||
// #init
|
||||
Duration timeout = Duration.ofSeconds(5);
|
||||
CompletionStage<Done> done =
|
||||
PersistenceInit.initializeDefaultPlugins(testKit.system(), timeout);
|
||||
done.toCompletableFuture().get(timeout.getSeconds(), TimeUnit.SECONDS);
|
||||
// #init
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright (C) 2020-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.persistence.testkit;
|
||||
|
||||
import org.apache.pekko.actor.testkit.typed.javadsl.TestKitJunitResource;
|
||||
import org.apache.pekko.actor.typed.ActorRef;
|
||||
import org.apache.pekko.persistence.testkit.JournalOperation;
|
||||
import org.apache.pekko.persistence.testkit.PersistenceTestKitPlugin;
|
||||
import org.apache.pekko.persistence.testkit.ProcessingPolicy;
|
||||
import org.apache.pekko.persistence.testkit.ProcessingResult;
|
||||
import org.apache.pekko.persistence.testkit.ProcessingSuccess;
|
||||
import org.apache.pekko.persistence.testkit.StorageFailure;
|
||||
import org.apache.pekko.persistence.testkit.WriteEvents;
|
||||
import org.apache.pekko.persistence.testkit.javadsl.PersistenceTestKit;
|
||||
import org.apache.pekko.persistence.typed.PersistenceId;
|
||||
import com.typesafe.config.ConfigFactory;
|
||||
import jdocs.AbstractJavaTest;
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
|
||||
// #policy-test
|
||||
public class PersistenceTestKitPolicySampleTest extends AbstractJavaTest {
|
||||
|
||||
@ClassRule
|
||||
public static final TestKitJunitResource testKit =
|
||||
new TestKitJunitResource(
|
||||
PersistenceTestKitPlugin.getInstance()
|
||||
.config()
|
||||
.withFallback(ConfigFactory.defaultApplication()));
|
||||
|
||||
PersistenceTestKit persistenceTestKit = PersistenceTestKit.create(testKit.system());
|
||||
|
||||
@Before
|
||||
public void beforeEach() {
|
||||
persistenceTestKit.clearAll();
|
||||
persistenceTestKit.resetPolicy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
SampleEventStoragePolicy policy = new SampleEventStoragePolicy();
|
||||
persistenceTestKit.withPolicy(policy);
|
||||
|
||||
PersistenceId persistenceId = PersistenceId.ofUniqueId("some-id");
|
||||
ActorRef<YourPersistentBehavior.Cmd> ref =
|
||||
testKit.spawn(YourPersistentBehavior.create(persistenceId));
|
||||
|
||||
YourPersistentBehavior.Cmd cmd = new YourPersistentBehavior.Cmd("data");
|
||||
ref.tell(cmd);
|
||||
|
||||
persistenceTestKit.expectNothingPersisted(persistenceId.id());
|
||||
}
|
||||
|
||||
static class SampleEventStoragePolicy implements ProcessingPolicy<JournalOperation> {
|
||||
@Override
|
||||
public ProcessingResult tryProcess(String processId, JournalOperation processingUnit) {
|
||||
if (processingUnit instanceof WriteEvents) {
|
||||
return StorageFailure.create();
|
||||
} else {
|
||||
return ProcessingSuccess.getInstance();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// #policy-test
|
||||
|
|
@ -0,0 +1,125 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.persistence.testkit;
|
||||
|
||||
import org.apache.pekko.actor.testkit.typed.javadsl.TestKitJunitResource;
|
||||
import org.apache.pekko.actor.typed.ActorRef;
|
||||
import org.apache.pekko.actor.typed.Behavior;
|
||||
import org.apache.pekko.actor.typed.javadsl.Behaviors;
|
||||
import org.apache.pekko.persistence.testkit.PersistenceTestKitPlugin;
|
||||
import org.apache.pekko.persistence.testkit.javadsl.PersistenceTestKit;
|
||||
import org.apache.pekko.persistence.typed.PersistenceId;
|
||||
import org.apache.pekko.persistence.typed.javadsl.CommandHandler;
|
||||
import org.apache.pekko.persistence.typed.javadsl.EventHandler;
|
||||
import org.apache.pekko.persistence.typed.javadsl.EventSourcedBehavior;
|
||||
import org.apache.pekko.serialization.jackson.CborSerializable;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.typesafe.config.ConfigFactory;
|
||||
import jdocs.AbstractJavaTest;
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
|
||||
// #test
|
||||
public class PersistenceTestKitSampleTest extends AbstractJavaTest {
|
||||
|
||||
@ClassRule
|
||||
public static final TestKitJunitResource testKit =
|
||||
new TestKitJunitResource(
|
||||
PersistenceTestKitPlugin.getInstance()
|
||||
.config()
|
||||
.withFallback(ConfigFactory.defaultApplication()));
|
||||
|
||||
PersistenceTestKit persistenceTestKit = PersistenceTestKit.create(testKit.system());
|
||||
|
||||
@Before
|
||||
public void beforeEach() {
|
||||
persistenceTestKit.clearAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
PersistenceId persistenceId = PersistenceId.ofUniqueId("some-id");
|
||||
ActorRef<YourPersistentBehavior.Cmd> ref =
|
||||
testKit.spawn(YourPersistentBehavior.create(persistenceId));
|
||||
|
||||
YourPersistentBehavior.Cmd cmd = new YourPersistentBehavior.Cmd("data");
|
||||
ref.tell(cmd);
|
||||
YourPersistentBehavior.Evt expectedEventPersisted = new YourPersistentBehavior.Evt(cmd.data);
|
||||
|
||||
persistenceTestKit.expectNextPersisted(persistenceId.id(), expectedEventPersisted);
|
||||
}
|
||||
}
|
||||
|
||||
class YourPersistentBehavior
|
||||
extends EventSourcedBehavior<
|
||||
YourPersistentBehavior.Cmd, YourPersistentBehavior.Evt, YourPersistentBehavior.State> {
|
||||
|
||||
static final class Cmd implements CborSerializable {
|
||||
|
||||
public final String data;
|
||||
|
||||
@JsonCreator
|
||||
public Cmd(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
static final class Evt implements CborSerializable {
|
||||
|
||||
public final String data;
|
||||
|
||||
@JsonCreator
|
||||
public Evt(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Evt evt = (Evt) o;
|
||||
|
||||
return data.equals(evt.data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return data.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
static final class State implements CborSerializable {}
|
||||
|
||||
static Behavior<Cmd> create(PersistenceId persistenceId) {
|
||||
return Behaviors.setup(context -> new YourPersistentBehavior(persistenceId));
|
||||
}
|
||||
|
||||
private YourPersistentBehavior(PersistenceId persistenceId) {
|
||||
super(persistenceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public State emptyState() {
|
||||
// some state
|
||||
return new State();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandHandler<Cmd, Evt, State> commandHandler() {
|
||||
return newCommandHandlerBuilder()
|
||||
.forAnyState()
|
||||
.onCommand(Cmd.class, command -> Effect().persist(new Evt(command.data)))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public EventHandler<State, Evt> eventHandler() {
|
||||
// TODO handle events
|
||||
return newEventHandlerBuilder().forAnyState().onEvent(Evt.class, (state, evt) -> state).build();
|
||||
}
|
||||
}
|
||||
// #test
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* Copyright (C) 2020-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.persistence.testkit;
|
||||
|
||||
import org.apache.pekko.persistence.testkit.DeleteEvents;
|
||||
import org.apache.pekko.persistence.testkit.DeleteSnapshotByMeta;
|
||||
import org.apache.pekko.persistence.testkit.DeleteSnapshotsByCriteria;
|
||||
import org.apache.pekko.persistence.testkit.JournalOperation;
|
||||
import org.apache.pekko.persistence.testkit.ProcessingPolicy;
|
||||
import org.apache.pekko.persistence.testkit.ProcessingResult;
|
||||
import org.apache.pekko.persistence.testkit.ProcessingSuccess;
|
||||
import org.apache.pekko.persistence.testkit.ReadEvents;
|
||||
import org.apache.pekko.persistence.testkit.ReadSeqNum;
|
||||
import org.apache.pekko.persistence.testkit.ReadSnapshot;
|
||||
import org.apache.pekko.persistence.testkit.Reject;
|
||||
import org.apache.pekko.persistence.testkit.SnapshotOperation;
|
||||
import org.apache.pekko.persistence.testkit.StorageFailure;
|
||||
import org.apache.pekko.persistence.testkit.WriteEvents;
|
||||
import org.apache.pekko.persistence.testkit.WriteSnapshot;
|
||||
|
||||
public class TestKitExamples {
|
||||
|
||||
// #set-event-storage-policy
|
||||
class SampleEventStoragePolicy implements ProcessingPolicy<JournalOperation> {
|
||||
|
||||
// you can use internal state, it does not need to be thread safe
|
||||
int count = 1;
|
||||
|
||||
@Override
|
||||
public ProcessingResult tryProcess(String processId, JournalOperation processingUnit) {
|
||||
// check the type of operation and react with success or with reject or with failure.
|
||||
// if you return ProcessingSuccess the operation will be performed, otherwise not.
|
||||
if (count < 10) {
|
||||
count += 1;
|
||||
if (processingUnit instanceof ReadEvents) {
|
||||
ReadEvents read = (ReadEvents) processingUnit;
|
||||
if (read.batch().nonEmpty()) {
|
||||
ProcessingSuccess.getInstance();
|
||||
} else {
|
||||
return StorageFailure.create();
|
||||
}
|
||||
} else if (processingUnit instanceof WriteEvents) {
|
||||
return ProcessingSuccess.getInstance();
|
||||
} else if (processingUnit instanceof DeleteEvents) {
|
||||
return ProcessingSuccess.getInstance();
|
||||
} else if (processingUnit.equals(ReadSeqNum.getInstance())) {
|
||||
return Reject.create();
|
||||
}
|
||||
// you can set your own exception
|
||||
return StorageFailure.create(new RuntimeException("your exception"));
|
||||
} else {
|
||||
return ProcessingSuccess.getInstance();
|
||||
}
|
||||
}
|
||||
}
|
||||
// #set-event-storage-policy
|
||||
|
||||
// #set-snapshot-storage-policy
|
||||
class SnapshotStoragePolicy implements ProcessingPolicy<SnapshotOperation> {
|
||||
|
||||
// you can use internal state, it doesn't need to be thread safe
|
||||
int count = 1;
|
||||
|
||||
@Override
|
||||
public ProcessingResult tryProcess(String processId, SnapshotOperation processingUnit) {
|
||||
// check the type of operation and react with success or with failure.
|
||||
// if you return ProcessingSuccess the operation will be performed, otherwise not.
|
||||
if (count < 10) {
|
||||
count += 1;
|
||||
if (processingUnit instanceof ReadSnapshot) {
|
||||
ReadSnapshot read = (ReadSnapshot) processingUnit;
|
||||
if (read.getSnapshot().isPresent()) {
|
||||
ProcessingSuccess.getInstance();
|
||||
} else {
|
||||
return StorageFailure.create();
|
||||
}
|
||||
} else if (processingUnit instanceof WriteSnapshot) {
|
||||
return ProcessingSuccess.getInstance();
|
||||
} else if (processingUnit instanceof DeleteSnapshotsByCriteria) {
|
||||
return ProcessingSuccess.getInstance();
|
||||
} else if (processingUnit instanceof DeleteSnapshotByMeta) {
|
||||
return ProcessingSuccess.getInstance();
|
||||
}
|
||||
// you can set your own exception
|
||||
return StorageFailure.create(new RuntimeException("your exception"));
|
||||
} else {
|
||||
return ProcessingSuccess.getInstance();
|
||||
}
|
||||
}
|
||||
}
|
||||
// #set-snapshot-storage-policy
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue