2014-02-15 23:44:00 -05:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package sample.persistence;
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.actor.Props;
|
|
|
|
|
import akka.japi.pf.ReceiveBuilder;
|
|
|
|
|
import akka.persistence.AbstractProcessor;
|
|
|
|
|
import akka.persistence.Persistent;
|
|
|
|
|
import akka.persistence.SnapshotOffer;
|
|
|
|
|
import scala.PartialFunction;
|
|
|
|
|
import scala.runtime.BoxedUnit;
|
|
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
public class SnapshotExample {
|
2014-03-20 12:05:32 +01:00
|
|
|
public static class ExampleState implements Serializable {
|
|
|
|
|
private final ArrayList<String> received;
|
2014-02-15 23:44:00 -05:00
|
|
|
|
2014-03-20 12:05:32 +01:00
|
|
|
public ExampleState() {
|
|
|
|
|
this(new ArrayList<String>());
|
|
|
|
|
}
|
2014-02-15 23:44:00 -05:00
|
|
|
|
2014-03-20 12:05:32 +01:00
|
|
|
public ExampleState(ArrayList<String> received) {
|
|
|
|
|
this.received = received;
|
|
|
|
|
}
|
2014-02-15 23:44:00 -05:00
|
|
|
|
2014-03-20 12:05:32 +01:00
|
|
|
public ExampleState copy() {
|
|
|
|
|
return new ExampleState(new ArrayList<String>(received));
|
|
|
|
|
}
|
2014-02-15 23:44:00 -05:00
|
|
|
|
2014-03-20 12:05:32 +01:00
|
|
|
public void update(String s) {
|
|
|
|
|
received.add(s);
|
|
|
|
|
}
|
2014-02-15 23:44:00 -05:00
|
|
|
|
2014-03-20 12:05:32 +01:00
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return received.toString();
|
2014-02-15 23:44:00 -05:00
|
|
|
}
|
2014-03-20 12:05:32 +01:00
|
|
|
}
|
2014-02-15 23:44:00 -05:00
|
|
|
|
2014-03-20 12:05:32 +01:00
|
|
|
public static class ExampleProcessor extends AbstractProcessor {
|
|
|
|
|
private ExampleState state = new ExampleState();
|
2014-02-15 23:44:00 -05:00
|
|
|
|
2014-03-20 12:05:32 +01:00
|
|
|
public ExampleProcessor() {
|
|
|
|
|
receive(ReceiveBuilder.
|
|
|
|
|
match(Persistent.class, p -> state.update(String.format("%s-%d", p.payload(), p.sequenceNr()))).
|
|
|
|
|
match(SnapshotOffer.class, s -> {
|
|
|
|
|
ExampleState exState = (ExampleState) s.snapshot();
|
|
|
|
|
System.out.println("offered state = " + exState);
|
|
|
|
|
state = exState;
|
|
|
|
|
}).
|
|
|
|
|
match(String.class, s -> s.equals("print"), s -> System.out.println("current state = " + state)).
|
|
|
|
|
match(String.class, s -> s.equals("snap"), s ->
|
|
|
|
|
// IMPORTANT: create a copy of snapshot
|
|
|
|
|
// because ExampleState is mutable !!!
|
|
|
|
|
saveSnapshot(state.copy())).build()
|
|
|
|
|
);
|
2014-02-15 23:44:00 -05:00
|
|
|
}
|
2014-03-20 12:05:32 +01:00
|
|
|
}
|
2014-02-15 23:44:00 -05:00
|
|
|
|
2014-03-20 12:05:32 +01:00
|
|
|
public static void main(String... args) throws Exception {
|
|
|
|
|
final ActorSystem system = ActorSystem.create("example");
|
|
|
|
|
final ActorRef processor = system.actorOf(Props.create(ExampleProcessor.class), "processor-3-java");
|
2014-02-15 23:44:00 -05:00
|
|
|
|
2014-03-20 12:05:32 +01:00
|
|
|
processor.tell(Persistent.create("a"), null);
|
|
|
|
|
processor.tell(Persistent.create("b"), null);
|
|
|
|
|
processor.tell("snap", null);
|
|
|
|
|
processor.tell(Persistent.create("c"), null);
|
|
|
|
|
processor.tell(Persistent.create("d"), null);
|
|
|
|
|
processor.tell("print", null);
|
2014-02-15 23:44:00 -05:00
|
|
|
|
2014-03-20 12:05:32 +01:00
|
|
|
Thread.sleep(1000);
|
|
|
|
|
system.shutdown();
|
|
|
|
|
}
|
2014-02-15 23:44:00 -05:00
|
|
|
}
|