akka.actor.SupervisorStrategy Java Examples

The following examples show how to use akka.actor.SupervisorStrategy. 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 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: ClusterUtil.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Start a cluster singleton actor.
 *
 * @param system the actor system.
 * @param actorRefFactory where the cluster singleton should be created.
 * @param role role of this cluster member.
 * @param actorName name of the singleton actor.
 * @param props Props of the singleton actor.
 * @param supervisorStrategy the {@link SupervisorStrategy} for the singleton actor.
 * @return reference of the singleton actor.
 */
public static ActorRef startSingleton(final ActorSystem system,
        final ActorRefFactory actorRefFactory,
        final String role,
        final String actorName,
        final Props props,
        final SupervisorStrategy supervisorStrategy) {

    final ClusterSingletonManagerSettings settings =
            ClusterSingletonManagerSettings.create(system).withRole(role);

    final Props supervisorProps = ClusterSingletonSupervisorActor.props(props, supervisorStrategy);
    final Props singletonManagerProps =
            ClusterSingletonManager.props(supervisorProps, PoisonPill.getInstance(), settings);
    return actorRefFactory.actorOf(singletonManagerProps, actorName);
}
 
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: 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 #5
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 #6
Source File: AppActor.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
@Override
public Directive apply(Throwable t) {
  logger.error(t, "Unknown failure");
  if (t instanceof RuntimeException) {
    return SupervisorStrategy.restart();
  } else {
    return SupervisorStrategy.stop();
  }
}
 
Example #7
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 #8
Source File: AbstractPubSubSupervisor.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public SupervisorStrategy supervisorStrategy() {
    return new AllForOneStrategy(
            DeciderBuilder.matchAny(error -> {
                final Duration restartDelay = config.getRestartDelay();
                log.error(error, "Child <{}> crashed. Restarting all children after <{}>",
                        getSender(), restartDelay);
                getTimers().startSingleTimer(Control.RESTART, Control.RESTART, restartDelay);
                onChildFailure();
                return SupervisorStrategy.stop();
            }).build());
}
 
Example #9
Source File: ConnectivityRootActor.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected PartialFunction<Throwable, SupervisorStrategy.Directive> getSupervisionDecider() {
    return DeciderBuilder.match(JMSRuntimeException.class, e -> {
        log.warning("JMSRuntimeException '{}' occurred.", e.getMessage());
        return restartChild();
    }).match(NamingException.class, e -> {
        log.warning("NamingException '{}' occurred.", e.getMessage());
        return restartChild();
    }).build().orElse(super.getSupervisionDecider());
}
 
Example #10
Source File: SupervisorActor.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public PartialFunction<Throwable, Directive> decider() {
	return DeciderBuilder.match(
		Exception.class, e -> SupervisorStrategy.stop()
	).build();
}
 
Example #11
Source File: AppActor.java    From iotplatform with Apache License 2.0 4 votes vote down vote up
@Override
public SupervisorStrategy supervisorStrategy() {
  return strategy;
}
 
Example #12
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));
        }
    };
}
 
Example #13
Source File: AmqpClientActor.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public SupervisorStrategy supervisorStrategy() {
    return SupervisorStrategy.stoppingStrategy();
}
 
Example #14
Source File: ConnectionSupervisorActor.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public SupervisorStrategy supervisorStrategy() {
    return SUPERVISOR_STRATEGY;
}
 
Example #15
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 #16
Source File: SearchUpdaterRootActor.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public SupervisorStrategy supervisorStrategy() {
    return supervisorStrategy;
}
 
Example #17
Source File: StreamingActor.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public SupervisorStrategy supervisorStrategy() {
    return strategy;
}
 
Example #18
Source File: SetupDocumentTypeWorkerActor.java    From searchanalytics-bigdata with MIT License 4 votes vote down vote up
@Override
public SupervisorStrategy supervisorStrategy() {
	LOG.debug("Custom supervisorStrategy is used for SetupDocumentTypeWorkerActor!");
	return strategy;
}
 
Example #19
Source File: ClusterSingletonSupervisorActor.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public SupervisorStrategy supervisorStrategy() {
    return supervisorStrategy;
}
 
Example #20
Source File: ClusterSingletonSupervisorActor.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
private SupervisorStrategy.Directive restartChild() {
    log.info("Restarting child ...");
    return SupervisorStrategy.restart();
}
 
Example #21
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 #22
Source File: ClusterSingletonSupervisorActor.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@SuppressWarnings("unused")
ClusterSingletonSupervisorActor(final Props childProps, final SupervisorStrategy supervisorStrategy) {
    this.supervisorStrategy = supervisorStrategy;
    this.child = getContext().actorOf(childProps, "supervised-child");
}
 
Example #23
Source File: DittoRootActor.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public SupervisorStrategy supervisorStrategy() {
    return strategy;
}
 
Example #24
Source File: Master.java    From akka-tutorial with Apache License 2.0 4 votes vote down vote up
@Override
public SupervisorStrategy supervisorStrategy() {
	return Master.strategy;
}
 
Example #25
Source File: SupervisorActor.java    From flower with Apache License 2.0 4 votes vote down vote up
@Override
public SupervisorStrategy supervisorStrategy() {
  return DEFAULT_STRATEGY;
}
 
Example #26
Source File: OrderEntity.java    From spring-boot-akka-event-sourcing-starter with Apache License 2.0 4 votes vote down vote up
@Override
public SupervisorStrategy supervisorStrategy() {
	return strategy;
}
 
Example #27
Source File: OrderManager.java    From spring-boot-akka-event-sourcing-starter with Apache License 2.0 4 votes vote down vote up
/**
 * @return supervisorStrategy the actor supervisor strategy
 */
@Override
public SupervisorStrategy supervisorStrategy() {
	return strategy;
}
 
Example #28
Source File: ClusterUtil.java    From ditto with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Start a cluster singleton actor.
 *
 * @param context context of the actor whose child the cluster singleton manager shall be.
 * @param role role of this cluster member.
 * @param actorName name of the singleton actor.
 * @param props Props of the singleton actor.
 * @param supervisorStrategy the {@link SupervisorStrategy} for the singleton actor.
 * @return reference of the singleton actor.
 */
public static ActorRef startSingleton(final ActorContext context,
        final String role,
        final String actorName,
        final Props props,
        final SupervisorStrategy supervisorStrategy) {

    return startSingleton(context.system(), context, role, actorName, props, supervisorStrategy);
}
 
Example #29
Source File: ClusterSingletonSupervisorActor.java    From ditto with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Creates Akka configuration object Props for this Actor.
 *
 * @param childProps the Props of the child to create (the actual singleton).
 * @param supervisorStrategy the {@link SupervisorStrategy} to apply.
 * @return the Akka configuration Props object.
 */
public static Props props(final Props childProps, final SupervisorStrategy supervisorStrategy) {

    return Props.create(ClusterSingletonSupervisorActor.class, childProps, supervisorStrategy);
}
 
Example #30
Source File: AbstractPersistenceSupervisor.java    From ditto with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Return a preferably static supervisor strategy for this actor. By default, child actor is stopped when killed
 * or failing, triggering restart after exponential back-off.
 * Overriding method should return a static object if possible to conserve memory.
 *
 * @return The default supervisor strategy.
 */
@Override
public SupervisorStrategy supervisorStrategy() {
    return SupervisorStrategy.stoppingStrategy();
}