pekko/akka-samples/akka-sample-persistence-java8/src/main/java/sample/persistence/ProcessorChannelExample.java
Nabeel Ali Memon a0a541eda7 +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.
2014-03-04 16:14:57 +01:00

58 lines
2.1 KiB
Java

/**
* 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.runtime.BoxedUnit;
public class ProcessorChannelExample {
public static class ExampleProcessor extends AbstractProcessor {
private ActorRef destination;
private ActorRef channel;
public ExampleProcessor(ActorRef destination) {
this.destination = destination;
this.channel = context().actorOf(Channel.props(), "channel");
}
@Override public PartialFunction<Object, BoxedUnit> receive() {
return ReceiveBuilder.
match(Persistent.class, p -> {
System.out.println("processed " + p.payload());
channel.tell(Deliver.create(p.withPayload("processed " + p.payload()), destination.path()), self());
}).
match(String.class, s -> System.out.println("reply = " + s)).build();
}
}
public static class ExampleDestination extends AbstractActor {
@Override public PartialFunction<Object, BoxedUnit> receive() {
return ReceiveBuilder.
match(ConfirmablePersistent.class, cp -> {
System.out.println("received " + cp.payload());
sender().tell(String.format("re: %s (%d)", cp.payload(), cp.sequenceNr()), null);
cp.confirm();
}).build();
}
}
public static void main(String... args) throws Exception {
final ActorSystem system = ActorSystem.create("example");
final ActorRef destination = system.actorOf(Props.create(ExampleDestination.class));
final ActorRef processor = system.actorOf(Props.create(ExampleProcessor.class, destination), "processor-1");
processor.tell(Persistent.create("a"), null);
processor.tell(Persistent.create("b"), null);
Thread.sleep(1000);
system.shutdown();
}
}