2014-02-03 16:08:19 +01:00
|
|
|
package sample.persistence;
|
2013-09-14 14:19:18 +02:00
|
|
|
|
|
|
|
|
import akka.actor.*;
|
|
|
|
|
import akka.persistence.*;
|
|
|
|
|
|
|
|
|
|
public class ProcessorChannelExample {
|
|
|
|
|
public static class ExampleProcessor extends UntypedProcessor {
|
|
|
|
|
private ActorRef destination;
|
|
|
|
|
private ActorRef channel;
|
|
|
|
|
|
|
|
|
|
public ExampleProcessor(ActorRef destination) {
|
|
|
|
|
this.destination = destination;
|
|
|
|
|
this.channel = getContext().actorOf(Channel.props(), "channel");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
|
|
|
|
if (message instanceof Persistent) {
|
|
|
|
|
Persistent msg = (Persistent)message;
|
|
|
|
|
System.out.println("processed " + msg.payload());
|
2014-01-17 06:58:25 +01:00
|
|
|
channel.tell(Deliver.create(msg.withPayload("processed " + msg.payload()), destination.path()), getSelf());
|
|
|
|
|
} else if (message instanceof String) {
|
|
|
|
|
System.out.println("reply = " + message);
|
2013-09-14 14:19:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class ExampleDestination extends UntypedActor {
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Object message) throws Exception {
|
2013-11-07 10:45:02 +01:00
|
|
|
if (message instanceof ConfirmablePersistent) {
|
|
|
|
|
ConfirmablePersistent msg = (ConfirmablePersistent)message;
|
2013-09-14 14:19:18 +02:00
|
|
|
System.out.println("received " + msg.payload());
|
2014-01-17 06:58:25 +01:00
|
|
|
getSender().tell(String.format("re: %s (%d)", msg.payload(), msg.sequenceNr()), null);
|
|
|
|
|
msg.confirm();
|
2013-09-14 14:19:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String... args) throws Exception {
|
|
|
|
|
final ActorSystem system = ActorSystem.create("example");
|
|
|
|
|
final ActorRef destination = system.actorOf(Props.create(ExampleDestination.class));
|
2013-09-26 09:14:43 +02:00
|
|
|
final ActorRef processor = system.actorOf(Props.create(ExampleProcessor.class, destination), "processor-1");
|
2013-09-14 14:19:18 +02:00
|
|
|
|
|
|
|
|
processor.tell(Persistent.create("a"), null);
|
|
|
|
|
processor.tell(Persistent.create("b"), null);
|
|
|
|
|
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
system.shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|