akka.actor.OneForOneStrategy Java Examples

The following examples show how to use akka.actor.OneForOneStrategy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StoppingSupervisorWithoutLoggingActorKilledExceptionStrategy.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public SupervisorStrategy create() {
	return new OneForOneStrategy(
		false,
		new PFBuilder<Throwable, SupervisorStrategy.Directive>()
			.match(
				Exception.class,
				(Exception e) -> {
					if (e instanceof ActorKilledException) {
						LOG.debug("Actor was killed. Stopping it now.", e);
					} else {
						LOG.error("Actor failed with exception. Stopping it now.", e);
					}
					return SupervisorStrategy.Stop$.MODULE$;
				})
			.build());
}
 
Example #2
Source File: StoppingSupervisorWithoutLoggingActorKilledExceptionStrategy.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public SupervisorStrategy create() {
	return new OneForOneStrategy(
		false,
		new PFBuilder<Throwable, SupervisorStrategy.Directive>()
			.match(
				Exception.class,
				(Exception e) -> {
					if (e instanceof ActorKilledException) {
						LOG.debug("Actor was killed. Stopping it now.", e);
					} else {
						LOG.error("Actor failed with exception. Stopping it now.", e);
					}
					return SupervisorStrategy.Stop$.MODULE$;
				})
			.build());
}
 
Example #3
Source File: AbstractProxyActor.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public SupervisorStrategy supervisorStrategy() {
    return new OneForOneStrategy(true, DeciderBuilder
            .match(NullPointerException.class, e -> {
                log.error(e, "NullPointer in child actor - restarting it...", e.getMessage());
                log.info("Restarting child...");
                return SupervisorStrategy.restart();
            })
            .match(ActorKilledException.class, e -> {
                log.error(e.getCause(), "ActorKilledException in child actor - stopping it...");
                return SupervisorStrategy.stop();
            })
            .matchAny(e -> SupervisorStrategy.escalate())
            .build());
}
 
Example #4
Source File: ClusterSingleton.java    From ts-reaktive with MIT License 6 votes vote down vote up
/**
 * Starts and returns the an {@link ActorRef} to the {@link ClusterSingletonManager} (and backoff supervisor)
 * that manage this actor singleton.
 *
 * Note that you can't send this ActorRef messages that should go to your actor: use {@link StartProxy} for that.
 *
 * @param constructor Constructor to pass to Props in order to actually create the actor
 */
public ActorRef start(ActorSystem system, Supplier<T> constructor) {
    Config config = system.settings().config().getConfig("ts-reaktive.actors.singleton");
    Props backoffSupervisorProps = BackoffSupervisor.props(
        Backoff.onStop(
            Props.create(type, () -> constructor.get()), "actor",
            FiniteDuration.create(config.getDuration("min-backoff", SECONDS), SECONDS),
            FiniteDuration.create(config.getDuration("max-backoff", SECONDS), SECONDS),
            0.2
        ).withSupervisorStrategy(new OneForOneStrategy(
            DeciderBuilder .matchAny(e -> {
                log.info("{}: Stopping and awaiting restart.", name, e);
                return stop();
            })
            .build()
        ))
    );

    return system.actorOf(
        ClusterSingletonManager.props(backoffSupervisorProps,
            PoisonPill.getInstance(),
            ClusterSingletonManagerSettings.create(system)
        ), name);
}
 
Example #5
Source File: SessionActor.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
@Override
public SupervisorStrategy supervisorStrategy() {
  return new OneForOneStrategy(-1, Duration.Inf(), throwable -> {
    logger.error(throwable, "Unknown session error");
    if (throwable instanceof Error) {
      return OneForOneStrategy.escalate();
    } else {
      return OneForOneStrategy.resume();
    }
  });
}
 
Example #6
Source File: EscalatingSupervisorStrategy.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public SupervisorStrategy create() {
	return new OneForOneStrategy(
		false,
		new PFBuilder<Throwable, SupervisorStrategy.Directive>()
			.matchAny(
				(ignored) -> SupervisorStrategy.escalate())
			.build());
}
 
Example #7
Source File: ClusterSingletonSupervisorActor.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
private OneForOneStrategy buildDefaultSupervisorStrategy() {
    return new OneForOneStrategy(true, DeciderBuilder
            .match(NullPointerException.class, e -> {
                log.error(e, "NullPointer in singleton actor: {}", e.getMessage());
                return restartChild();
            }).match(IllegalArgumentException.class, e -> {
                log.warning("Illegal Argument in singleton actor: {}", e.getMessage());

                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);

                log.warning("Illegal Argument in singleton actor: {}", sw.toString());
                return SupervisorStrategy.resume();
            }).match(IllegalStateException.class, e -> {
                log.warning("Illegal State in singleton actor: {}", e.getMessage());
                return SupervisorStrategy.resume();
            }).match(IndexOutOfBoundsException.class, e -> {
                log.warning("IndexOutOfBounds in singleton actor: {}", e.getMessage());
                return SupervisorStrategy.resume();
            }).match(NoSuchElementException.class, e -> {
                log.warning("NoSuchElement in singleton actor: {}", e.getMessage());
                return SupervisorStrategy.resume();
            }).match(AskTimeoutException.class, e -> {
                log.warning("AskTimeoutException in singleton actor: {}", e.getMessage());
                return SupervisorStrategy.resume();
            }).match(ConnectException.class, e -> {
                log.warning("ConnectException in singleton actor: {}", e.getMessage());
                return restartChild();
            }).match(InvalidActorNameException.class, e -> {
                log.warning("InvalidActorNameException in singleton actor: {}", e.getMessage());
                return SupervisorStrategy.resume();
            }).match(ActorKilledException.class, e -> {
                log.error(e, "ActorKilledException in singleton actor: {}", e.message());
                return restartChild();
            }).match(DittoRuntimeException.class, e -> {
                log.error(e,
                        "DittoRuntimeException '{}' should not be escalated to SupervisorActor. Simply resuming Actor.",
                        e.getErrorCode());
                return SupervisorStrategy.resume();
            }).match(UnsupportedOperationException.class, e -> {
                log.error(e, "UnsupportedOperationException in singleton actor: {}",
                        e.getMessage());
                terminateActorSystem();
                return SupervisorStrategy.stop(); // only stopping as ActorSystem is terminated anyways
            }).match(Throwable.class, e -> {
                log.error(e, "Escalating above root actor!");
                terminateActorSystem();
                return SupervisorStrategy.stop(); // only stopping as ActorSystem is terminated anyways
            }).matchAny(e -> {
                log.error("Unknown message:'{}'! Escalating above root actor!", e);
                terminateActorSystem();
                return SupervisorStrategy.stop(); // only stopping as ActorSystem is terminated anyways
            }).build());
}
 
Example #8
Source File: RootSupervisorStrategyFactory.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
public static OneForOneStrategy createStrategy(final LoggingAdapter log) {
    return new OneForOneStrategy(true, DeciderBuilder
            .match(NullPointerException.class, e -> {
                log.error(e, "NullPointer in child actor: {}", e.getMessage());
                log.info(RESTARTING_CHILD_MSG);
                return SupervisorStrategy.restart();
            }).match(IllegalArgumentException.class, e -> {
                log.warning("Illegal Argument in child actor: {}", e.getMessage());
                return SupervisorStrategy.resume();
            }).match(IndexOutOfBoundsException.class, e -> {
                log.warning("IndexOutOfBounds in child actor: {}", e.getMessage());
                return SupervisorStrategy.resume();
            }).match(IllegalStateException.class, e -> {
                log.warning("Illegal State in child actor: {}", e.getMessage());
                return SupervisorStrategy.resume();
            }).match(NoSuchElementException.class, e -> {
                log.warning("NoSuchElement in child actor: {}", e.getMessage());
                return SupervisorStrategy.resume();
            }).match(AskTimeoutException.class, e -> {
                log.warning("AskTimeoutException in child actor: {}", e.getMessage());
                return SupervisorStrategy.resume();
            }).match(ConnectException.class, e -> {
                log.warning("ConnectException in child actor: {}", e.getMessage());
                log.info(RESTARTING_CHILD_MSG);
                return SupervisorStrategy.restart();
            }).match(InvalidActorNameException.class, e -> {
                log.warning("InvalidActorNameException in child actor: {}", e.getMessage());
                return SupervisorStrategy.resume();
            }).match(ActorKilledException.class, e -> {
                log.error(e, "ActorKilledException in child actor: {}", e.message());
                log.info(RESTARTING_CHILD_MSG);
                return SupervisorStrategy.restart();
            }).match(DittoRuntimeException.class, e -> {
                log.error(e,
                        "DittoRuntimeException '{}' should not be escalated to RootActor. Simply resuming Actor.",
                        e.getErrorCode());
                return SupervisorStrategy.resume();
            }).match(Throwable.class, e -> {
                log.error(e, "Escalating above root actor!");
                return SupervisorStrategy.escalate();
            }).matchAny(e -> {
                log.error("Unknown message:'{}'! Escalating above root actor!", e);
                return SupervisorStrategy.escalate();
            }).build());
}
 
Example #9
Source File: PolicyPersistenceActorTest.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void checkForActivityOfNonexistentPolicy() {
    new TestKit(actorSystem) {
        {
            // GIVEN: a PolicyPersistenceActor is created in a parent that forwards all messages to us
            final PolicyId policyId = PolicyId.of("test.ns", "nonexistent.policy");
            final Props persistentActorProps =
                    PolicyPersistenceActor.props(policyId, new PolicyMongoSnapshotAdapter(), pubSubMediator);

            final TestProbe errorsProbe = TestProbe.apply(actorSystem);

            final Props parentProps = Props.create(Actor.class, () -> new AbstractActor() {

                @Override
                public void preStart() {
                    getContext().actorOf(persistentActorProps);
                }

                @Override
                public SupervisorStrategy supervisorStrategy() {
                    return new OneForOneStrategy(true,
                            DeciderBuilder.matchAny(throwable -> {
                                errorsProbe.ref().tell(throwable, getSelf());
                                return SupervisorStrategy.restart();
                            }).build());
                }

                @Override
                public Receive createReceive() {
                    return ReceiveBuilder.create()
                            .matchAny(message -> {
                                if (getTestActor().equals(getSender())) {
                                    getContext().actorSelection(getSelf().path().child("*"))
                                            .forward(message, getContext());
                                } else {
                                    getTestActor().forward(message, getContext());
                                }
                            })
                            .build();
                }
            });

            // WHEN: CheckForActivity is sent to a persistence actor of nonexistent policy after startup
            final ActorRef underTest = actorSystem.actorOf(parentProps);

            final Object checkForActivity = AbstractShardedPersistenceActor.checkForActivity(1L);
            underTest.tell(checkForActivity, getRef());
            underTest.tell(checkForActivity, getRef());
            underTest.tell(checkForActivity, getRef());

            // THEN: persistence actor requests shutdown
            expectMsg(PolicySupervisorActor.Control.PASSIVATE);

            // THEN: persistence actor should not throw anything.
            errorsProbe.expectNoMessage(scala.concurrent.duration.Duration.create(3, TimeUnit.SECONDS));
        }
    };
}