+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,100 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package sample.persistence;
|
||||
|
||||
import akka.actor.AbstractActor;
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.actor.Props;
|
||||
import akka.japi.pf.ReceiveBuilder;
|
||||
import akka.persistence.*;
|
||||
import scala.PartialFunction;
|
||||
import scala.concurrent.duration.Duration;
|
||||
import scala.runtime.BoxedUnit;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ViewExample {
|
||||
public static class ExampleProcessor extends AbstractProcessor {
|
||||
@Override
|
||||
public String processorId() {
|
||||
return "processor-5";
|
||||
}
|
||||
|
||||
@Override public PartialFunction<Object, BoxedUnit> receive() {
|
||||
return ReceiveBuilder.
|
||||
match(Persistent.class,
|
||||
p -> System.out.println(String.format("processor received %s (sequence nr = %d)",
|
||||
p.payload(),
|
||||
p.sequenceNr()))).build();
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExampleView extends AbstractView {
|
||||
private final ActorRef destination = context().actorOf(Props.create(ExampleDestination.class));
|
||||
private final ActorRef channel = context().actorOf(Channel.props("channel"));
|
||||
|
||||
private int numReplicated = 0;
|
||||
|
||||
@Override
|
||||
public String viewId() {
|
||||
return "view-5";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String processorId() {
|
||||
return "processor-5";
|
||||
}
|
||||
|
||||
@Override public PartialFunction<Object, BoxedUnit> receive() {
|
||||
return ReceiveBuilder.
|
||||
match(Persistent.class, p -> {
|
||||
numReplicated += 1;
|
||||
System.out.println(String.format("view received %s (sequence nr = %d, num replicated = %d)",
|
||||
p.payload(),
|
||||
p.sequenceNr(),
|
||||
numReplicated));
|
||||
channel.tell(Deliver.create(p.withPayload("replicated-" + p.payload()), destination.path()),
|
||||
self());
|
||||
}).
|
||||
match(SnapshotOffer.class, so -> {
|
||||
numReplicated = (Integer) so.snapshot();
|
||||
System.out.println(String.format("view received snapshot offer %s (metadata = %s)",
|
||||
numReplicated,
|
||||
so.metadata()));
|
||||
}).
|
||||
match(String.class, s -> s.equals("snap"), s -> saveSnapshot(numReplicated)).build();
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExampleDestination extends AbstractActor {
|
||||
|
||||
@Override public PartialFunction<Object, BoxedUnit> receive() {
|
||||
return ReceiveBuilder.
|
||||
match(ConfirmablePersistent.class, cp -> {
|
||||
System.out.println(String.format("destination received %s (sequence nr = %s)",
|
||||
cp.payload(),
|
||||
cp.sequenceNr()));
|
||||
cp.confirm();
|
||||
}).build();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String... args) throws Exception {
|
||||
final ActorSystem system = ActorSystem.create("example");
|
||||
final ActorRef processor = system.actorOf(Props.create(ExampleProcessor.class));
|
||||
final ActorRef view = system.actorOf(Props.create(ExampleView.class));
|
||||
|
||||
system.scheduler()
|
||||
.schedule(Duration.Zero(),
|
||||
Duration.create(2, TimeUnit.SECONDS),
|
||||
processor,
|
||||
Persistent.create("scheduled"),
|
||||
system.dispatcher(),
|
||||
null);
|
||||
system.scheduler()
|
||||
.schedule(Duration.Zero(), Duration.create(5, TimeUnit.SECONDS), view, "snap", system.dispatcher(), null);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue