2014-02-15 23:44:00 -05:00
|
|
|
/**
|
2018-01-04 17:26:29 +00:00
|
|
|
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com>
|
2014-02-15 23:44:00 -05:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.persistence;
|
2014-02-15 23:44:00 -05:00
|
|
|
|
2015-07-09 18:01:27 +02:00
|
|
|
import akka.actor.*;
|
2015-06-24 19:58:43 +02:00
|
|
|
import akka.japi.Procedure;
|
2015-07-07 16:28:17 +02:00
|
|
|
import akka.pattern.BackoffSupervisor;
|
2014-06-03 16:40:44 +02:00
|
|
|
import akka.persistence.*;
|
2018-04-11 16:47:36 +02:00
|
|
|
import java.time.Duration;
|
2017-03-16 09:30:00 +01:00
|
|
|
|
2014-06-03 15:10:56 +02:00
|
|
|
import java.io.Serializable;
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
import java.util.Optional;
|
2014-02-15 23:44:00 -05:00
|
|
|
|
|
|
|
|
public class LambdaPersistenceDocTest {
|
|
|
|
|
|
2014-06-05 14:07:17 +02:00
|
|
|
public interface SomeOtherMessage {}
|
|
|
|
|
|
2014-12-08 11:02:14 +01:00
|
|
|
public interface PersistentActorMethods {
|
2014-06-23 14:33:35 +02:00
|
|
|
//#persistence-id
|
|
|
|
|
public String persistenceId();
|
|
|
|
|
//#persistence-id
|
2014-02-15 23:44:00 -05:00
|
|
|
//#recovery-status
|
|
|
|
|
public boolean recoveryRunning();
|
|
|
|
|
public boolean recoveryFinished();
|
|
|
|
|
//#recovery-status
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Object o2 = new Object() {
|
2014-12-08 11:02:14 +01:00
|
|
|
abstract class MyPersistentActor1 extends AbstractPersistentActor {
|
2015-06-24 19:58:43 +02:00
|
|
|
//#recovery-disabled
|
2014-02-15 23:44:00 -05:00
|
|
|
@Override
|
2015-06-24 19:58:43 +02:00
|
|
|
public Recovery recovery() {
|
|
|
|
|
return Recovery.none();
|
|
|
|
|
}
|
|
|
|
|
//#recovery-disabled
|
2014-02-15 23:44:00 -05:00
|
|
|
|
|
|
|
|
//#recover-on-restart-disabled
|
|
|
|
|
@Override
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
public void preRestart(Throwable reason, Optional<Object> message) {}
|
2014-02-15 23:44:00 -05:00
|
|
|
//#recover-on-restart-disabled
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-08 11:02:14 +01:00
|
|
|
abstract class MyPersistentActor2 extends AbstractPersistentActor {
|
2015-06-24 19:58:43 +02:00
|
|
|
//#recovery-custom
|
2014-02-15 23:44:00 -05:00
|
|
|
@Override
|
2015-06-24 19:58:43 +02:00
|
|
|
public Recovery recovery() {
|
|
|
|
|
return Recovery.create(457L);
|
2014-02-15 23:44:00 -05:00
|
|
|
}
|
2015-06-24 19:58:43 +02:00
|
|
|
//#recovery-custom
|
2014-02-15 23:44:00 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-08 11:02:14 +01:00
|
|
|
class MyPersistentActor4 extends AbstractPersistentActor implements PersistentActorMethods {
|
2014-06-23 14:33:35 +02:00
|
|
|
//#persistence-id-override
|
2014-02-15 23:44:00 -05:00
|
|
|
@Override
|
2014-06-23 14:33:35 +02:00
|
|
|
public String persistenceId() {
|
|
|
|
|
return "my-stable-persistence-id";
|
2014-02-15 23:44:00 -05:00
|
|
|
}
|
|
|
|
|
|
2014-06-23 14:33:35 +02:00
|
|
|
//#persistence-id-override
|
2014-12-08 11:02:14 +01:00
|
|
|
|
|
|
|
|
@Override
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder().
|
2014-12-08 11:02:14 +01:00
|
|
|
match(String.class, cmd -> {/* ... */}).build();
|
2014-02-15 23:44:00 -05:00
|
|
|
}
|
2014-12-08 11:02:14 +01:00
|
|
|
|
|
|
|
|
@Override
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
public Receive createReceiveRecover() {
|
|
|
|
|
return receiveBuilder().
|
2014-12-08 11:02:14 +01:00
|
|
|
match(String.class, evt -> {/* ... */}).build();
|
2015-06-01 19:03:00 +02:00
|
|
|
}
|
2014-12-08 11:02:14 +01:00
|
|
|
|
2014-02-15 23:44:00 -05:00
|
|
|
}
|
2014-03-24 15:35:54 +01:00
|
|
|
|
|
|
|
|
//#recovery-completed
|
2014-06-25 12:51:21 +02:00
|
|
|
class MyPersistentActor5 extends AbstractPersistentActor {
|
|
|
|
|
|
2015-06-01 19:03:00 +02:00
|
|
|
@Override public String persistenceId() {
|
2014-06-26 13:56:01 +02:00
|
|
|
return "my-stable-persistence-id";
|
|
|
|
|
}
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override public Receive createReceiveRecover() {
|
|
|
|
|
return receiveBuilder().
|
2014-07-03 03:59:46 +03:00
|
|
|
match(RecoveryCompleted.class, r -> {
|
2014-12-08 11:02:14 +01:00
|
|
|
// perform init after recovery, before any other messages
|
|
|
|
|
// ...
|
2014-07-03 03:59:46 +03:00
|
|
|
}).
|
2014-06-25 12:51:21 +02:00
|
|
|
match(String.class, this::handleEvent).build();
|
|
|
|
|
}
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override public Receive createReceive() {
|
|
|
|
|
return receiveBuilder().
|
2014-06-25 12:51:21 +02:00
|
|
|
match(String.class, s -> s.equals("cmd"),
|
|
|
|
|
s -> persist("evt", this::handleEvent)).build();
|
2015-06-01 19:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
2014-06-25 12:51:21 +02:00
|
|
|
private void handleEvent(String event) {
|
|
|
|
|
// update state
|
|
|
|
|
// ...
|
|
|
|
|
}
|
2015-06-01 19:03:00 +02:00
|
|
|
|
2014-03-24 15:35:54 +01:00
|
|
|
}
|
|
|
|
|
//#recovery-completed
|
2015-06-01 19:03:00 +02:00
|
|
|
|
2016-11-17 10:39:18 +01:00
|
|
|
abstract class MyPersistentActor6 extends AbstractPersistentActor {
|
|
|
|
|
//#recovery-no-snap
|
|
|
|
|
@Override
|
|
|
|
|
public Recovery recovery() {
|
|
|
|
|
return Recovery.create(SnapshotSelectionCriteria.none());
|
|
|
|
|
}
|
|
|
|
|
//#recovery-no-snap
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 19:03:00 +02:00
|
|
|
abstract class MyActor extends AbstractPersistentActor {
|
|
|
|
|
//#backoff
|
|
|
|
|
@Override
|
|
|
|
|
public void preStart() throws Exception {
|
|
|
|
|
final Props childProps = Props.create(MyPersistentActor1.class);
|
|
|
|
|
final Props props = BackoffSupervisor.props(
|
|
|
|
|
childProps,
|
|
|
|
|
"myActor",
|
2018-04-11 16:47:36 +02:00
|
|
|
Duration.ofSeconds(3),
|
|
|
|
|
Duration.ofSeconds(30),
|
2015-06-01 19:03:00 +02:00
|
|
|
0.2);
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
getContext().actorOf(props, "mySupervisor");
|
2015-06-01 19:03:00 +02:00
|
|
|
super.preStart();
|
|
|
|
|
}
|
|
|
|
|
//#backoff
|
|
|
|
|
}
|
2014-02-15 23:44:00 -05:00
|
|
|
};
|
|
|
|
|
|
2014-06-03 15:10:56 +02:00
|
|
|
static Object atLeastOnceExample = new Object() {
|
|
|
|
|
//#at-least-once-example
|
2015-06-01 19:03:00 +02:00
|
|
|
|
2014-06-03 15:10:56 +02:00
|
|
|
class Msg implements Serializable {
|
2014-12-08 11:02:14 +01:00
|
|
|
private static final long serialVersionUID = 1L;
|
2014-06-03 15:10:56 +02:00
|
|
|
public final long deliveryId;
|
|
|
|
|
public final String s;
|
2015-06-01 19:03:00 +02:00
|
|
|
|
2014-06-03 15:10:56 +02:00
|
|
|
public Msg(long deliveryId, String s) {
|
|
|
|
|
this.deliveryId = deliveryId;
|
|
|
|
|
this.s = s;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-01 19:03:00 +02:00
|
|
|
|
2014-06-03 15:10:56 +02:00
|
|
|
class Confirm implements Serializable {
|
2014-12-08 11:02:14 +01:00
|
|
|
private static final long serialVersionUID = 1L;
|
2014-06-03 15:10:56 +02:00
|
|
|
public final long deliveryId;
|
2015-06-01 19:03:00 +02:00
|
|
|
|
2014-06-03 15:10:56 +02:00
|
|
|
public Confirm(long deliveryId) {
|
|
|
|
|
this.deliveryId = deliveryId;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-01 19:03:00 +02:00
|
|
|
|
|
|
|
|
|
2014-06-03 15:10:56 +02:00
|
|
|
class MsgSent implements Serializable {
|
2014-12-08 11:02:14 +01:00
|
|
|
private static final long serialVersionUID = 1L;
|
2014-06-03 15:10:56 +02:00
|
|
|
public final String s;
|
2015-06-01 19:03:00 +02:00
|
|
|
|
2014-06-03 15:10:56 +02:00
|
|
|
public MsgSent(String s) {
|
|
|
|
|
this.s = s;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
class MsgConfirmed implements Serializable {
|
2014-12-08 11:02:14 +01:00
|
|
|
private static final long serialVersionUID = 1L;
|
2014-06-03 15:10:56 +02:00
|
|
|
public final long deliveryId;
|
2015-06-01 19:03:00 +02:00
|
|
|
|
2014-06-03 15:10:56 +02:00
|
|
|
public MsgConfirmed(long deliveryId) {
|
|
|
|
|
this.deliveryId = deliveryId;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-01 19:03:00 +02:00
|
|
|
|
2014-06-03 15:10:56 +02:00
|
|
|
class MyPersistentActor extends AbstractPersistentActorWithAtLeastOnceDelivery {
|
2015-07-09 18:01:27 +02:00
|
|
|
private final ActorSelection destination;
|
2014-06-03 15:10:56 +02:00
|
|
|
|
2015-07-09 18:01:27 +02:00
|
|
|
public MyPersistentActor(ActorSelection destination) {
|
2014-06-03 15:10:56 +02:00
|
|
|
this.destination = destination;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 19:03:00 +02:00
|
|
|
@Override public String persistenceId() {
|
2014-12-08 11:02:14 +01:00
|
|
|
return "persistence-id";
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-03 15:10:56 +02:00
|
|
|
@Override
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder().
|
2014-06-03 15:10:56 +02:00
|
|
|
match(String.class, s -> {
|
|
|
|
|
persist(new MsgSent(s), evt -> updateState(evt));
|
|
|
|
|
}).
|
|
|
|
|
match(Confirm.class, confirm -> {
|
|
|
|
|
persist(new MsgConfirmed(confirm.deliveryId), evt -> updateState(evt));
|
|
|
|
|
}).
|
|
|
|
|
build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
public Receive createReceiveRecover() {
|
|
|
|
|
return receiveBuilder().
|
2014-06-03 15:10:56 +02:00
|
|
|
match(Object.class, evt -> updateState(evt)).build();
|
2015-06-01 19:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
2014-06-03 15:10:56 +02:00
|
|
|
void updateState(Object event) {
|
|
|
|
|
if (event instanceof MsgSent) {
|
|
|
|
|
final MsgSent evt = (MsgSent) event;
|
|
|
|
|
deliver(destination, deliveryId -> new Msg(deliveryId, evt.s));
|
|
|
|
|
} else if (event instanceof MsgConfirmed) {
|
|
|
|
|
final MsgConfirmed evt = (MsgConfirmed) event;
|
|
|
|
|
confirmDelivery(evt.deliveryId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MyDestination extends AbstractActor {
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(Msg.class, msg -> {
|
2014-06-03 15:10:56 +02:00
|
|
|
// ...
|
2017-03-16 09:30:00 +01:00
|
|
|
getSender().tell(new Confirm(msg.deliveryId), getSelf());
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
})
|
|
|
|
|
.build();
|
2014-06-03 15:10:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#at-least-once-example
|
2015-06-01 19:03:00 +02:00
|
|
|
|
2014-02-15 23:44:00 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static Object o4 = new Object() {
|
2014-12-08 11:02:14 +01:00
|
|
|
class MyPersistentActor extends AbstractPersistentActor {
|
|
|
|
|
|
2017-03-14 21:30:32 +09:00
|
|
|
private void updateState(String evt){}
|
|
|
|
|
|
2014-12-08 11:02:14 +01:00
|
|
|
//#save-snapshot
|
2014-02-15 23:44:00 -05:00
|
|
|
private Object state;
|
2017-03-14 21:30:32 +09:00
|
|
|
private int snapShotInterval = 1000;
|
2014-02-15 23:44:00 -05:00
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override public Receive createReceive() {
|
|
|
|
|
return receiveBuilder().
|
2014-03-20 12:05:32 +01:00
|
|
|
match(SaveSnapshotSuccess.class, ss -> {
|
|
|
|
|
SnapshotMetadata metadata = ss.metadata();
|
|
|
|
|
// ...
|
|
|
|
|
}).
|
|
|
|
|
match(SaveSnapshotFailure.class, sf -> {
|
|
|
|
|
SnapshotMetadata metadata = sf.metadata();
|
|
|
|
|
// ...
|
2017-03-14 21:30:32 +09:00
|
|
|
}).
|
|
|
|
|
match(String.class, cmd -> {
|
|
|
|
|
persist( "evt-" + cmd, e -> {
|
|
|
|
|
updateState(e);
|
|
|
|
|
if (lastSequenceNr() % snapShotInterval == 0 && lastSequenceNr() != 0)
|
|
|
|
|
saveSnapshot(state);
|
|
|
|
|
});
|
2014-12-08 11:02:14 +01:00
|
|
|
}).build();
|
2014-02-15 23:44:00 -05:00
|
|
|
}
|
2014-12-08 11:02:14 +01:00
|
|
|
//#save-snapshot
|
|
|
|
|
|
2015-06-01 19:03:00 +02:00
|
|
|
@Override public String persistenceId() {
|
2014-12-08 11:02:14 +01:00
|
|
|
return "persistence-id";
|
|
|
|
|
}
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override public Receive createReceiveRecover() {
|
|
|
|
|
return receiveBuilder().
|
2014-12-08 11:02:14 +01:00
|
|
|
match(RecoveryCompleted.class, r -> {/* ...*/}).build();
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-15 23:44:00 -05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static Object o5 = new Object() {
|
2015-06-01 19:03:00 +02:00
|
|
|
|
2014-12-08 11:02:14 +01:00
|
|
|
class MyPersistentActor extends AbstractPersistentActor {
|
2015-07-02 00:44:10 +02:00
|
|
|
|
|
|
|
|
//#snapshot-criteria
|
|
|
|
|
@Override
|
|
|
|
|
public Recovery recovery() {
|
|
|
|
|
return Recovery.create(
|
|
|
|
|
SnapshotSelectionCriteria
|
|
|
|
|
.create(457L, System.currentTimeMillis()));
|
|
|
|
|
}
|
|
|
|
|
//#snapshot-criteria
|
|
|
|
|
|
2014-12-08 11:02:14 +01:00
|
|
|
//#snapshot-offer
|
2014-02-15 23:44:00 -05:00
|
|
|
private Object state;
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override public Receive createReceiveRecover() {
|
|
|
|
|
return receiveBuilder().
|
2014-03-20 12:05:32 +01:00
|
|
|
match(SnapshotOffer.class, s -> {
|
|
|
|
|
state = s.snapshot();
|
|
|
|
|
// ...
|
|
|
|
|
}).
|
2014-12-08 11:02:14 +01:00
|
|
|
match(String.class, s -> {/* ...*/}).build();
|
|
|
|
|
}
|
|
|
|
|
//#snapshot-offer
|
|
|
|
|
|
2015-06-01 19:03:00 +02:00
|
|
|
@Override public String persistenceId() {
|
2014-12-08 11:02:14 +01:00
|
|
|
return "persistence-id";
|
2014-02-15 23:44:00 -05:00
|
|
|
}
|
2014-12-08 11:02:14 +01:00
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override public Receive createReceive() {
|
|
|
|
|
return receiveBuilder().
|
2014-12-08 11:02:14 +01:00
|
|
|
match(String.class, s -> {/* ...*/}).build();
|
2015-06-01 19:03:00 +02:00
|
|
|
}
|
2014-02-15 23:44:00 -05:00
|
|
|
}
|
2015-06-01 19:03:00 +02:00
|
|
|
|
2014-02-15 23:44:00 -05:00
|
|
|
|
|
|
|
|
class MyActor extends AbstractActor {
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
private final ActorRef persistentActor =
|
|
|
|
|
getContext().actorOf(Props.create(MyPersistentActor.class));
|
2014-02-15 23:44:00 -05:00
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(Object.class, o -> {/* ... */})
|
|
|
|
|
.build();
|
2014-02-15 23:44:00 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static Object o9 = new Object() {
|
2014-06-03 16:40:44 +02:00
|
|
|
//#persist-async
|
|
|
|
|
class MyPersistentActor extends AbstractPersistentActor {
|
|
|
|
|
|
2015-06-01 19:03:00 +02:00
|
|
|
@Override public String persistenceId() {
|
2014-06-26 13:56:01 +02:00
|
|
|
return "my-stable-persistence-id";
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-03 16:40:44 +02:00
|
|
|
private void handleCommand(String c) {
|
2017-03-16 09:30:00 +01:00
|
|
|
getSender().tell(c, getSelf());
|
2014-06-03 16:40:44 +02:00
|
|
|
|
|
|
|
|
persistAsync(String.format("evt-%s-1", c), e -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
getSender().tell(e, getSelf());
|
2014-06-03 16:40:44 +02:00
|
|
|
});
|
|
|
|
|
persistAsync(String.format("evt-%s-2", c), e -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
getSender().tell(e, getSelf());
|
2014-06-03 16:40:44 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override public Receive createReceiveRecover() {
|
|
|
|
|
return receiveBuilder().
|
2014-06-03 16:40:44 +02:00
|
|
|
match(String.class, this::handleCommand).build();
|
|
|
|
|
}
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override public Receive createReceive() {
|
|
|
|
|
return receiveBuilder().
|
2014-06-03 16:40:44 +02:00
|
|
|
match(String.class, this::handleCommand).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#persist-async
|
|
|
|
|
|
|
|
|
|
public void usage() {
|
|
|
|
|
final ActorSystem system = ActorSystem.create("example");
|
|
|
|
|
//#persist-async-usage
|
2014-12-08 11:02:14 +01:00
|
|
|
final ActorRef persistentActor = system.actorOf(Props.create(MyPersistentActor.class));
|
|
|
|
|
persistentActor.tell("a", null);
|
|
|
|
|
persistentActor.tell("b", null);
|
2014-06-03 16:40:44 +02:00
|
|
|
|
|
|
|
|
// possible order of received messages:
|
|
|
|
|
// a
|
|
|
|
|
// b
|
|
|
|
|
// evt-a-1
|
|
|
|
|
// evt-a-2
|
|
|
|
|
// evt-b-1
|
|
|
|
|
// evt-b-2
|
|
|
|
|
//#persist-async-usage
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Object o10 = new Object() {
|
|
|
|
|
//#defer
|
|
|
|
|
class MyPersistentActor extends AbstractPersistentActor {
|
|
|
|
|
|
2015-06-01 19:03:00 +02:00
|
|
|
@Override public String persistenceId() {
|
2014-06-26 13:56:01 +02:00
|
|
|
return "my-stable-persistence-id";
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-03 16:40:44 +02:00
|
|
|
private void handleCommand(String c) {
|
|
|
|
|
persistAsync(String.format("evt-%s-1", c), e -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
getSender().tell(e, getSelf());
|
2014-06-03 16:40:44 +02:00
|
|
|
});
|
|
|
|
|
persistAsync(String.format("evt-%s-2", c), e -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
getSender().tell(e, getSelf());
|
2014-06-03 16:40:44 +02:00
|
|
|
});
|
|
|
|
|
|
2015-05-28 01:37:38 +02:00
|
|
|
deferAsync(String.format("evt-%s-3", c), e -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
getSender().tell(e, getSelf());
|
2014-06-03 16:40:44 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override public Receive createReceiveRecover() {
|
|
|
|
|
return receiveBuilder().
|
2014-06-03 16:40:44 +02:00
|
|
|
match(String.class, this::handleCommand).build();
|
|
|
|
|
}
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override public Receive createReceive() {
|
|
|
|
|
return receiveBuilder().
|
2014-06-03 16:40:44 +02:00
|
|
|
match(String.class, this::handleCommand).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#defer
|
|
|
|
|
|
|
|
|
|
public void usage() {
|
|
|
|
|
final ActorSystem system = ActorSystem.create("example");
|
|
|
|
|
final ActorRef sender = null; // your imaginary sender here
|
|
|
|
|
//#defer-caller
|
2014-12-08 11:02:14 +01:00
|
|
|
final ActorRef persistentActor = system.actorOf(Props.create(MyPersistentActor.class));
|
|
|
|
|
persistentActor.tell("a", sender);
|
|
|
|
|
persistentActor.tell("b", sender);
|
2014-06-03 16:40:44 +02:00
|
|
|
|
|
|
|
|
// order of received messages:
|
|
|
|
|
// a
|
|
|
|
|
// b
|
|
|
|
|
// evt-a-1
|
|
|
|
|
// evt-a-2
|
|
|
|
|
// evt-a-3
|
|
|
|
|
// evt-b-1
|
|
|
|
|
// evt-b-2
|
|
|
|
|
// evt-b-3
|
|
|
|
|
//#defer-caller
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-03-30 18:34:09 +09:00
|
|
|
static Object o100 = new Object() {
|
|
|
|
|
//#defer-with-persist
|
|
|
|
|
class MyPersistentActor extends AbstractPersistentActor {
|
|
|
|
|
|
|
|
|
|
@Override public String persistenceId() {
|
|
|
|
|
return "my-stable-persistence-id";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleCommand(String c) {
|
|
|
|
|
persist(String.format("evt-%s-1", c), e -> {
|
|
|
|
|
sender().tell(e, self());
|
|
|
|
|
});
|
|
|
|
|
persist(String.format("evt-%s-2", c), e -> {
|
|
|
|
|
sender().tell(e, self());
|
|
|
|
|
});
|
|
|
|
|
|
2018-02-05 22:36:26 +01:00
|
|
|
defer(String.format("evt-%s-3", c), e -> {
|
2017-03-30 18:34:09 +09:00
|
|
|
sender().tell(e, self());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override public Receive createReceiveRecover() {
|
|
|
|
|
return receiveBuilder().
|
|
|
|
|
match(String.class, this::handleCommand).build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override public Receive createReceive() {
|
|
|
|
|
return receiveBuilder().
|
|
|
|
|
match(String.class, this::handleCommand).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#defer-with-persist
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2014-06-03 16:40:44 +02:00
|
|
|
static Object o11 = new Object() {
|
2015-06-24 19:58:43 +02:00
|
|
|
|
|
|
|
|
class MyPersistentActor extends AbstractPersistentActor {
|
|
|
|
|
@Override
|
|
|
|
|
public String persistenceId() {
|
|
|
|
|
return "my-stable-persistence-id";
|
|
|
|
|
}
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override public Receive createReceive() {
|
|
|
|
|
return receiveBuilder().matchAny(event -> {}).build();
|
2015-06-24 19:58:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#nested-persist-persist
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override public Receive createReceiveRecover() {
|
2017-03-16 09:30:00 +01:00
|
|
|
final Procedure<String> replyToSender = event -> getSender().tell(event, getSelf());
|
2015-06-24 19:58:43 +02:00
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
return receiveBuilder()
|
2015-06-24 19:58:43 +02:00
|
|
|
.match(String.class, msg -> {
|
|
|
|
|
persist(String.format("%s-outer-1", msg), event -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
getSender().tell(event, getSelf());
|
2015-06-24 19:58:43 +02:00
|
|
|
persist(String.format("%s-inner-1", event), replyToSender);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
persist(String.format("%s-outer-2", msg), event -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
getSender().tell(event, getSelf());
|
2015-06-24 19:58:43 +02:00
|
|
|
persist(String.format("%s-inner-2", event), replyToSender);
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
//#nested-persist-persist
|
|
|
|
|
|
|
|
|
|
void usage(ActorRef persistentActor) {
|
|
|
|
|
//#nested-persist-persist-caller
|
|
|
|
|
persistentActor.tell("a", ActorRef.noSender());
|
|
|
|
|
persistentActor.tell("b", ActorRef.noSender());
|
|
|
|
|
|
|
|
|
|
// order of received messages:
|
|
|
|
|
// a
|
|
|
|
|
// a-outer-1
|
|
|
|
|
// a-outer-2
|
|
|
|
|
// a-inner-1
|
|
|
|
|
// a-inner-2
|
|
|
|
|
// and only then process "b"
|
|
|
|
|
// b
|
|
|
|
|
// b-outer-1
|
|
|
|
|
// b-outer-2
|
|
|
|
|
// b-inner-1
|
|
|
|
|
// b-inner-2
|
|
|
|
|
|
|
|
|
|
//#nested-persist-persist-caller
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MyPersistAsyncActor extends AbstractPersistentActor {
|
|
|
|
|
@Override
|
|
|
|
|
public String persistenceId() {
|
|
|
|
|
return "my-stable-persistence-id";
|
|
|
|
|
}
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceiveRecover() {
|
|
|
|
|
return receiveBuilder().matchAny(event -> {}).build();
|
2015-06-24 19:58:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#nested-persistAsync-persistAsync
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
2017-03-16 09:30:00 +01:00
|
|
|
final Procedure<String> replyToSender = event -> getSender().tell(event, getSelf());
|
2015-06-24 19:58:43 +02:00
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
return receiveBuilder()
|
2015-06-24 19:58:43 +02:00
|
|
|
.match(String.class, msg -> {
|
|
|
|
|
persistAsync(String.format("%s-outer-1", msg ), event -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
getSender().tell(event, getSelf());
|
2015-06-24 19:58:43 +02:00
|
|
|
persistAsync(String.format("%s-inner-1", event), replyToSender);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
persistAsync(String.format("%s-outer-2", msg ), event -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
getSender().tell(event, getSelf());
|
2015-06-24 19:58:43 +02:00
|
|
|
persistAsync(String.format("%s-inner-1", event), replyToSender);
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
//#nested-persistAsync-persistAsync
|
|
|
|
|
|
|
|
|
|
void usage(ActorRef persistentActor) {
|
|
|
|
|
//#nested-persistAsync-persistAsync-caller
|
2017-03-16 09:30:00 +01:00
|
|
|
persistentActor.tell("a", getSelf());
|
|
|
|
|
persistentActor.tell("b", getSelf());
|
2015-06-24 19:58:43 +02:00
|
|
|
|
|
|
|
|
// order of received messages:
|
|
|
|
|
// a
|
|
|
|
|
// b
|
|
|
|
|
// a-outer-1
|
|
|
|
|
// a-outer-2
|
|
|
|
|
// b-outer-1
|
|
|
|
|
// b-outer-2
|
|
|
|
|
// a-inner-1
|
|
|
|
|
// a-inner-2
|
|
|
|
|
// b-inner-1
|
|
|
|
|
// b-inner-2
|
|
|
|
|
|
|
|
|
|
// which can be seen as the following causal relationship:
|
|
|
|
|
// a -> a-outer-1 -> a-outer-2 -> a-inner-1 -> a-inner-2
|
|
|
|
|
// b -> b-outer-1 -> b-outer-2 -> b-inner-1 -> b-inner-2
|
|
|
|
|
|
|
|
|
|
//#nested-persistAsync-persistAsync-caller
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-19 18:20:13 +02:00
|
|
|
static Object o14 = new Object() {
|
|
|
|
|
//#safe-shutdown
|
|
|
|
|
final class Shutdown {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MyPersistentActor extends AbstractPersistentActor {
|
|
|
|
|
@Override
|
|
|
|
|
public String persistenceId() {
|
|
|
|
|
return "some-persistence-id";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
2015-08-19 18:20:13 +02:00
|
|
|
.match(Shutdown.class, shutdown -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
getContext().stop(getSelf());
|
2015-08-19 18:20:13 +02:00
|
|
|
})
|
|
|
|
|
.match(String.class, msg -> {
|
|
|
|
|
System.out.println(msg);
|
|
|
|
|
persist("handle-" + msg, e -> System.out.println(e));
|
|
|
|
|
})
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
public Receive createReceiveRecover() {
|
|
|
|
|
return receiveBuilder().matchAny(any -> {}).build();
|
2015-08-19 18:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//#safe-shutdown
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void usage() {
|
|
|
|
|
final ActorSystem system = ActorSystem.create("example");
|
|
|
|
|
final ActorRef persistentActor = system.actorOf(Props.create(MyPersistentActor.class));
|
|
|
|
|
//#safe-shutdown-example-bad
|
|
|
|
|
// UN-SAFE, due to PersistentActor's command stashing:
|
|
|
|
|
persistentActor.tell("a", ActorRef.noSender());
|
|
|
|
|
persistentActor.tell("b", ActorRef.noSender());
|
|
|
|
|
persistentActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
|
|
|
|
|
// order of received messages:
|
|
|
|
|
// a
|
|
|
|
|
// # b arrives at mailbox, stashing; internal-stash = [b]
|
|
|
|
|
// # PoisonPill arrives at mailbox, stashing; internal-stash = [b, Shutdown]
|
|
|
|
|
// PoisonPill is an AutoReceivedMessage, is handled automatically
|
|
|
|
|
// !! stop !!
|
|
|
|
|
// Actor is stopped without handling `b` nor the `a` handler!
|
|
|
|
|
//#safe-shutdown-example-bad
|
|
|
|
|
|
|
|
|
|
//#safe-shutdown-example-good
|
|
|
|
|
// SAFE:
|
|
|
|
|
persistentActor.tell("a", ActorRef.noSender());
|
|
|
|
|
persistentActor.tell("b", ActorRef.noSender());
|
|
|
|
|
persistentActor.tell(new Shutdown(), ActorRef.noSender());
|
|
|
|
|
// order of received messages:
|
|
|
|
|
// a
|
|
|
|
|
// # b arrives at mailbox, stashing; internal-stash = [b]
|
|
|
|
|
// # Shutdown arrives at mailbox, stashing; internal-stash = [b, Shutdown]
|
|
|
|
|
// handle-a
|
|
|
|
|
// # unstashing; internal-stash = [Shutdown]
|
|
|
|
|
// b
|
|
|
|
|
// handle-b
|
|
|
|
|
// # unstashing; internal-stash = []
|
|
|
|
|
// Shutdown
|
|
|
|
|
// -- stop --
|
|
|
|
|
//#safe-shutdown-example-good
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-02-15 23:44:00 -05:00
|
|
|
}
|