/** * 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(); }
/** * 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); }
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; }