/** * Creates a Receive for recovery such that exceptions are logged as warnings * and not thrown. Recovery messages causing exceptions have no effect. * * @param log The Akka logger to write warnings to. * @param receiveRecover The Receive to wrap around. * * @return the created Receive. */ public static AbstractActor.Receive wrapReceive( @Nullable final DiagnosticLoggingAdapter log, @Nonnull final AbstractActor.Receive receiveRecover) { return ReceiveBuilder.create().matchAny(x -> { try { receiveRecover.onMessage().apply(x); } catch (final Exception error) { if (log != null) { log.warning("Failed to recover from the following message (it is ignored): {}", x); } } }).build(); }
@Override public Receive createReceive() { final Collection<ReceiveStrategy<?>> receiveStrategies = initReceiveStrategies(); final StrategyAwareReceiveBuilder strategyAwareReceiveBuilder = new StrategyAwareReceiveBuilder(); receiveStrategies.forEach(strategyAwareReceiveBuilder::match); strategyAwareReceiveBuilder.matchAny(new MatchAnyStrategy()); return shutdownNamespaceBehavior.createReceive().build().orElse(strategyAwareReceiveBuilder.build()); }
@Override public Receive createReceiveRecover() { return handleThingEvents.orElse(ReceiveBuilder.create()
@Override public Receive createReceiveRecover() { // defines how state is updated during recovery return handleThingEvents.orElse(ReceiveBuilder.create() // # Snapshot handling .match(SnapshotOffer.class, ss -> { log.debug("Got SnapshotOffer: {}", ss); thing = thingSnapshotter.recoverThingFromSnapshotOffer(ss); }) // # Recovery handling .match(RecoveryCompleted.class, rc -> { if (thing != null) { thing = enhanceThingWithLifecycle(thing); log.debug("Thing <{}> was recovered.", thingId); if (isThingActive()) { becomeThingCreatedHandler(); } else { // expect life cycle to be DELETED. if it's not, then act as if this thing is deleted. if (!isThingDeleted()) { // life cycle isn't known, act as log.error("Unknown lifecycle state <{}> for Thing <{}>.", thing.getLifecycle(), thingId); } becomeThingDeletedHandler(); } } }) .matchAny(m -> log.warning("Unknown recover message: {}", m)) .build()); }
@Override public Receive createReceiveRecover() { // defines how state is updated during recovery return handlePolicyEvents.orElse(ReceiveBuilder.create() // # Snapshot handling .match(SnapshotOffer.class, ss -> { policy = snapshotAdapter.fromSnapshotStore(ss); lastSnapshotSequenceNr = ss.metadata().sequenceNr(); }) // # Recovery handling .match(RecoveryCompleted.class, rc -> { if (policy != null) { log.debug("Policy <{}> was recovered.", policyId); if (isPolicyActive()) { becomePolicyCreatedHandler(); } else if (isPolicyDeleted()) { becomePolicyDeletedHandler(); } else { log.error("Unknown lifecycle state <{}> for Policy <{}>.", policy.getLifecycle(), policyId); } } }) // # Handle unknown .matchAny(m -> log.warning("Unknown recover message: {}", m)) .build()); }
@Override public Receive createReceiveRecover() { // defines how state is updated during recovery return handlePolicyEvents.orElse(ReceiveBuilder.create() // # Snapshot handling .match(SnapshotOffer.class, ss -> { policy = snapshotAdapter.fromSnapshotStore(ss); lastSnapshotSequenceNr = ss.metadata().sequenceNr(); }) // # Recovery handling .match(RecoveryCompleted.class, rc -> { if (policy != null) { log.debug("Policy <{}> was recovered.", policyId); if (isPolicyActive()) { becomePolicyCreatedHandler(); } else if (isPolicyDeleted()) { becomePolicyDeletedHandler(); } else { log.error("Unknown lifecycle state <{}> for Policy <{}>.", policy.getLifecycle(), policyId); } } }) // # Handle unknown .matchAny(m -> log.warning("Unknown recover message: {}", m)) .build()); }
/** * Creates a Receive for recovery such that exceptions are logged as warnings * and not thrown. Recovery messages causing exceptions have no effect. * * @param log The Akka logger to write warnings to. * @param receiveRecover The Receive to wrap around. * * @return the created Receive. */ public static AbstractActor.Receive wrapReceive( @Nullable final DiagnosticLoggingAdapter log, @Nonnull final AbstractActor.Receive receiveRecover) { return ReceiveBuilder.create().matchAny(x -> { try { receiveRecover.onMessage().apply(x); } catch (final Exception error) { if (log != null) { log.warning("Failed to recover from the following message (it is ignored): {}", x); } } }).build(); }
private Receive lockBehavior(final Receive receive) { checkNotNull(receive, "actor's message handler"); return ReceiveBuilder.create() .matchEquals(Control.UNLOCK, unlock -> { locked = false; cancelLockTimeout(); unstashAll(); }) .matchAny(message -> { final PartialFunction<Object, ?> handler = receive.onMessage(); if (locked) { stash(); } else if (handler.isDefinedAt(message)) { handler.apply(message); } else { unhandled(message); } }) .build(); }
private <E extends PolicyEvent> void processEvent(final E event, final Procedure<E> handler) { log.debug("About to persist Event <{}>.", event.getType()); persist(event, persistedEvent -> { log.info("Successfully persisted Event <{}>.", event.getType()); // after the event was persisted, apply the event on the current actor state handlePolicyEvents.onMessage().apply(persistedEvent); /* * The event has to be applied before creating the snapshot, otherwise a snapshot with new * sequence no (e.g. 2), but old thing revision no (e.g. 1) will be created. This can lead to serious * aftereffects. */ handler.apply(persistedEvent); // save a snapshot if there were too many changes since the last snapshot if ((lastSequenceNr() - lastSnapshotSequenceNr) > snapshotThreshold) { doSaveSnapshot(null); } notifySubscribers(event); }); }
private <E extends PolicyEvent> void processEvent(final E event, final Procedure<E> handler) { log.debug("About to persist Event <{}>.", event.getType()); persist(event, persistedEvent -> { log.info("Successfully persisted Event <{}>.", event.getType()); // after the event was persisted, apply the event on the current actor state handlePolicyEvents.onMessage().apply(persistedEvent); /* * The event has to be applied before creating the snapshot, otherwise a snapshot with new * sequence no (e.g. 2), but old thing revision no (e.g. 1) will be created. This can lead to serious * aftereffects. */ handler.apply(persistedEvent); // save a snapshot if there were too many changes since the last snapshot if ((lastSequenceNr() - lastSnapshotSequenceNr) > snapshotThreshold) { doSaveSnapshot(null); } notifySubscribers(event); }); }
private <A extends ThingModifiedEvent> void applyEvent(final A event) { handleThingEvents.onMessage().apply(event); notifySubscribers(event); }
private <A extends ThingModifiedEvent> void applyEvent(final A event) { handleThingEvents.onMessage().apply(event); notifySubscribers(event); }
@Override public Receive createReceive() { final Collection<ReceiveStrategy<?>> receiveStrategies = initReceiveStrategies(); final StrategyAwareReceiveBuilder strategyAwareReceiveBuilder = new StrategyAwareReceiveBuilder(log); strategyAwareReceiveBuilder.matchEach(receiveStrategies); strategyAwareReceiveBuilder.matchAny(new MatchAnyStrategy()); return shutdownNamespaceBehavior.createReceive().build().orElse(strategyAwareReceiveBuilder.build()); }
@Override public Receive createReceive() { return handleEvents().orElse(ReceiveBuilder.create() .match(DistributedPubSubMediator.SubscribeAck.class, subscribeAck -> log.debug("Got SubscribeAck about topic <{}> for group <{}>", subscribeAck.subscribe().topic(), subscribeAck.subscribe().group())) .matchAny(message -> { log.warning("Unhandled message <{}>", message); unhandled(message); }) .build()); }
private AbstractActor.Receive applyPeekStepIfSet(final AbstractActor.Receive receive) { if (null != peekStep) { final PartialFunction<Object, BoxedUnit> onMessage = peekStep.andThen(receive.onMessage()); return new AbstractActor.Receive(onMessage); } return receive; }
private AbstractActor.Receive applyPeekStepIfSet(final AbstractActor.Receive receive) { if (null != peekStep) { final PartialFunction<Object, BoxedUnit> onMessage = peekStep.andThen(receive.onMessage()); return new AbstractActor.Receive(onMessage); } return receive; }
/** * Build a {@link scala.PartialFunction} from this builder. After this call the builder will be * reset. * * @return a PartialFunction for this builder. */ public Receive build() { PartialFunction<Object, BoxedUnit> empty = CaseStatement.empty(); if (statements == null) return new Receive(empty); else return new Receive(statements.orElse(empty)); // FIXME why no new Receive(statements)? }
/** * Build a {@link scala.PartialFunction} from this builder. After this call the builder will be * reset. * * @return a PartialFunction for this builder. */ public Receive build() { PartialFunction<Object, BoxedUnit> empty = CaseStatement.empty(); if (statements == null) return new Receive(empty); else return new Receive(statements.orElse(empty)); // FIXME why no new Receive(statements)? }