+per #3906 add Java8 support for Persistence
- Provided new interfaces for akka-persistence to be usable directly through ReceiveBuilder/PartialFunction. Added a sample java project to showcase the usage of these API's with akka-persistence. - Fixed a minor comment block in javadoc code snippet. - Renamed java event persistor and fixed a documentation typo. - Put back java event persistence methods in UntypedEventsourcedProcessor and copied them into AbstractEventsourcedProcessor for the sake of clarity in javadocs. Also corrected some doc punctuations. - Documentation for akka-persistence java 8 lambda expressions support. - Moved code examples referred from within lambda-persistence.rst to java8 compatible sample project. - Removed remaining unwanted java8 compatible source files.
This commit is contained in:
parent
8e32b4577c
commit
a0a541eda7
15 changed files with 1860 additions and 0 deletions
|
|
@ -0,0 +1,79 @@
|
|||
/**
|
||||
* 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 {
|
||||
public static class ExampleState implements Serializable {
|
||||
private final ArrayList<String> received;
|
||||
|
||||
public ExampleState() {
|
||||
this(new ArrayList<String>());
|
||||
}
|
||||
|
||||
public ExampleState(ArrayList<String> received) {
|
||||
this.received = received;
|
||||
}
|
||||
|
||||
public ExampleState copy() {
|
||||
return new ExampleState(new ArrayList<String>(received));
|
||||
}
|
||||
|
||||
public void update(String s) {
|
||||
received.add(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return received.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExampleProcessor extends AbstractProcessor {
|
||||
private ExampleState state = new ExampleState();
|
||||
|
||||
@Override public PartialFunction<Object, BoxedUnit> receive() {
|
||||
return 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();
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
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);
|
||||
|
||||
Thread.sleep(1000);
|
||||
system.shutdown();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue