2013-10-09 13:11:53 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2013-09-14 14:19:18 +02:00
|
|
|
package docs.persistence;
|
|
|
|
|
|
|
|
|
|
import scala.Option;
|
|
|
|
|
|
|
|
|
|
import akka.actor.*;
|
|
|
|
|
import akka.persistence.*;
|
|
|
|
|
|
2013-10-27 08:01:14 +01:00
|
|
|
import static java.util.Arrays.asList;
|
|
|
|
|
|
2013-09-14 14:19:18 +02:00
|
|
|
public class PersistenceDocTest {
|
|
|
|
|
|
|
|
|
|
public interface ProcessorMethods {
|
|
|
|
|
//#processor-id
|
|
|
|
|
public String processorId();
|
|
|
|
|
//#processor-id
|
|
|
|
|
//#recovery-status
|
|
|
|
|
public boolean recoveryRunning();
|
|
|
|
|
public boolean recoveryFinished();
|
|
|
|
|
//#recovery-status
|
|
|
|
|
//#current-message
|
|
|
|
|
public Persistent getCurrentPersistentMessage();
|
|
|
|
|
//#current-message
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Object o1 = new Object() {
|
|
|
|
|
//#definition
|
|
|
|
|
class MyProcessor extends UntypedProcessor {
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
|
|
|
|
if (message instanceof Persistent) {
|
2013-10-08 11:46:02 +02:00
|
|
|
// message successfully written to journal
|
2013-09-14 14:19:18 +02:00
|
|
|
Persistent persistent = (Persistent)message;
|
|
|
|
|
Object payload = persistent.payload();
|
|
|
|
|
Long sequenceNr = persistent.sequenceNr();
|
|
|
|
|
// ...
|
2013-10-08 11:46:02 +02:00
|
|
|
} else if (message instanceof PersistenceFailure) {
|
|
|
|
|
// message failed to be written to journal
|
|
|
|
|
PersistenceFailure failure = (PersistenceFailure)message;
|
|
|
|
|
Object payload = failure.payload();
|
|
|
|
|
Long sequenceNr = failure.sequenceNr();
|
|
|
|
|
Throwable cause = failure.cause();
|
|
|
|
|
// ...
|
2013-09-14 14:19:18 +02:00
|
|
|
} else {
|
2013-10-08 11:46:02 +02:00
|
|
|
// message not written to journal
|
2013-09-14 14:19:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#definition
|
|
|
|
|
|
|
|
|
|
class MyActor extends UntypedActor {
|
|
|
|
|
ActorRef processor;
|
|
|
|
|
|
|
|
|
|
public MyActor() {
|
|
|
|
|
//#usage
|
|
|
|
|
processor = getContext().actorOf(Props.create(MyProcessor.class), "myProcessor");
|
|
|
|
|
|
|
|
|
|
processor.tell(Persistent.create("foo"), null);
|
|
|
|
|
processor.tell("bar", null);
|
|
|
|
|
//#usage
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
|
|
|
|
// ...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void recover() {
|
|
|
|
|
//#recover-explicit
|
|
|
|
|
processor.tell(Recover.create(), null);
|
|
|
|
|
//#recover-explicit
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static Object o2 = new Object() {
|
|
|
|
|
abstract class MyProcessor1 extends UntypedProcessor {
|
|
|
|
|
//#recover-on-start-disabled
|
|
|
|
|
@Override
|
2013-09-15 09:04:05 +02:00
|
|
|
public void preStart() {}
|
2013-09-14 14:19:18 +02:00
|
|
|
//#recover-on-start-disabled
|
|
|
|
|
|
|
|
|
|
//#recover-on-restart-disabled
|
|
|
|
|
@Override
|
2013-09-15 09:04:05 +02:00
|
|
|
public void preRestart(Throwable reason, Option<Object> message) {}
|
2013-09-14 14:19:18 +02:00
|
|
|
//#recover-on-restart-disabled
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
abstract class MyProcessor2 extends UntypedProcessor {
|
|
|
|
|
//#recover-on-start-custom
|
|
|
|
|
@Override
|
2013-09-15 09:04:05 +02:00
|
|
|
public void preStart() {
|
2013-09-14 14:19:18 +02:00
|
|
|
getSelf().tell(Recover.create(457L), null);
|
|
|
|
|
}
|
|
|
|
|
//#recover-on-start-custom
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
abstract class MyProcessor3 extends UntypedProcessor {
|
|
|
|
|
//#deletion
|
|
|
|
|
@Override
|
2013-09-15 09:04:05 +02:00
|
|
|
public void preRestart(Throwable reason, Option<Object> message) {
|
2013-09-14 14:19:18 +02:00
|
|
|
if (message.isDefined() && message.get() instanceof Persistent) {
|
2013-09-26 09:14:43 +02:00
|
|
|
deleteMessage((Persistent) message.get());
|
2013-09-14 14:19:18 +02:00
|
|
|
}
|
2013-09-15 09:04:05 +02:00
|
|
|
super.preRestart(reason, message);
|
2013-09-14 14:19:18 +02:00
|
|
|
}
|
|
|
|
|
//#deletion
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MyProcessor4 extends UntypedProcessor implements ProcessorMethods {
|
|
|
|
|
//#processor-id-override
|
|
|
|
|
@Override
|
|
|
|
|
public String processorId() {
|
|
|
|
|
return "my-stable-processor-id";
|
|
|
|
|
}
|
|
|
|
|
//#processor-id-override
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Object message) throws Exception {}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static Object o3 = new Object() {
|
|
|
|
|
//#channel-example
|
|
|
|
|
class MyProcessor extends UntypedProcessor {
|
|
|
|
|
private final ActorRef destination;
|
|
|
|
|
private final ActorRef channel;
|
|
|
|
|
|
|
|
|
|
public MyProcessor() {
|
|
|
|
|
this.destination = getContext().actorOf(Props.create(MyDestination.class));
|
|
|
|
|
this.channel = getContext().actorOf(Channel.props(), "myChannel");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
|
|
|
|
if (message instanceof Persistent) {
|
|
|
|
|
Persistent p = (Persistent)message;
|
|
|
|
|
Persistent out = p.withPayload("done " + p.payload());
|
|
|
|
|
channel.tell(Deliver.create(out, destination), getSelf());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MyDestination extends UntypedActor {
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
|
|
|
|
if (message instanceof Persistent) {
|
|
|
|
|
Persistent p = (Persistent)message;
|
|
|
|
|
System.out.println("received " + p.payload());
|
|
|
|
|
p.confirm();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#channel-example
|
|
|
|
|
|
|
|
|
|
class MyProcessor2 extends UntypedProcessor {
|
|
|
|
|
private final ActorRef destination;
|
|
|
|
|
private final ActorRef channel;
|
|
|
|
|
|
|
|
|
|
public MyProcessor2(ActorRef destination) {
|
|
|
|
|
this.destination = getContext().actorOf(Props.create(MyDestination.class));
|
|
|
|
|
//#channel-id-override
|
|
|
|
|
this.channel = getContext().actorOf(Channel.props("my-stable-channel-id"));
|
|
|
|
|
//#channel-id-override
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
|
|
|
|
if (message instanceof Persistent) {
|
|
|
|
|
Persistent p = (Persistent)message;
|
|
|
|
|
Persistent out = p.withPayload("done " + p.payload());
|
|
|
|
|
channel.tell(Deliver.create(out, destination), getSelf());
|
|
|
|
|
|
|
|
|
|
//#channel-example-reply
|
|
|
|
|
channel.tell(Deliver.create(out, getSender()), getSelf());
|
|
|
|
|
//#channel-example-reply
|
|
|
|
|
//#resolve-destination
|
|
|
|
|
channel.tell(Deliver.create(out, getSender(), Resolve.destination()), getSelf());
|
|
|
|
|
//#resolve-destination
|
|
|
|
|
//#resolve-sender
|
|
|
|
|
channel.tell(Deliver.create(out, destination, Resolve.sender()), getSender());
|
|
|
|
|
//#resolve-sender
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2013-09-26 09:14:43 +02:00
|
|
|
|
|
|
|
|
static Object o4 = new Object() {
|
|
|
|
|
//#save-snapshot
|
|
|
|
|
class MyProcessor extends UntypedProcessor {
|
|
|
|
|
private Object state;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
|
|
|
|
if (message.equals("snap")) {
|
|
|
|
|
saveSnapshot(state);
|
2013-10-08 11:46:02 +02:00
|
|
|
} else if (message instanceof SaveSnapshotSuccess) {
|
|
|
|
|
SnapshotMetadata metadata = ((SaveSnapshotSuccess)message).metadata();
|
2013-09-26 09:14:43 +02:00
|
|
|
// ...
|
2013-10-08 11:46:02 +02:00
|
|
|
} else if (message instanceof SaveSnapshotFailure) {
|
|
|
|
|
SnapshotMetadata metadata = ((SaveSnapshotFailure)message).metadata();
|
2013-09-26 09:14:43 +02:00
|
|
|
// ...
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#save-snapshot
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static Object o5 = new Object() {
|
|
|
|
|
//#snapshot-offer
|
|
|
|
|
class MyProcessor extends UntypedProcessor {
|
|
|
|
|
private Object state;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
|
|
|
|
if (message instanceof SnapshotOffer) {
|
|
|
|
|
state = ((SnapshotOffer)message).snapshot();
|
|
|
|
|
// ...
|
|
|
|
|
} else if (message instanceof Persistent) {
|
|
|
|
|
// ...
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#snapshot-offer
|
|
|
|
|
|
|
|
|
|
class MyActor extends UntypedActor {
|
|
|
|
|
ActorRef processor;
|
|
|
|
|
|
|
|
|
|
public MyActor() {
|
|
|
|
|
processor = getContext().actorOf(Props.create(MyProcessor.class));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
|
|
|
|
// ...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void recover() {
|
|
|
|
|
//#snapshot-criteria
|
|
|
|
|
processor.tell(Recover.create(SnapshotSelectionCriteria.create(457L, System.currentTimeMillis())), null);
|
|
|
|
|
//#snapshot-criteria
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2013-10-27 08:01:14 +01:00
|
|
|
|
|
|
|
|
static Object o6 = new Object() {
|
|
|
|
|
//#batch-write
|
|
|
|
|
class MyProcessor extends UntypedProcessor {
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
|
|
|
|
if (message instanceof Persistent) {
|
|
|
|
|
Persistent p = (Persistent)message;
|
|
|
|
|
if (p.payload().equals("a")) { /* ... */ }
|
|
|
|
|
if (p.payload().equals("b")) { /* ... */ }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Example {
|
|
|
|
|
final ActorSystem system = ActorSystem.create("example");
|
|
|
|
|
final ActorRef processor = system.actorOf(Props.create(MyProcessor.class));
|
|
|
|
|
|
|
|
|
|
public void batchWrite() {
|
|
|
|
|
processor.tell(PersistentBatch.create(asList(
|
|
|
|
|
Persistent.create("a"),
|
|
|
|
|
Persistent.create("b"))), null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
}
|
|
|
|
|
//#batch-write
|
|
|
|
|
};
|
2013-09-14 14:19:18 +02:00
|
|
|
}
|