[英]Actor retry with back-off and retry limit
我需要在Akka演員上使用重試機制,以增加重試之間的時間和最大重試限制。 為此,我嘗試使用akka提供的BackOffSupervisor模式。 問題是,根據我的測試,退避策略和重試限制似乎不起作用。 也許問題出在測試中?
測試如下所示:
一個簡單的actor,在前5條消息中引發異常
class SomeActor extends AbstractActor {
private static int messageCounter = 0;
//return the message to sender
@Override
public void preRestart(final Throwable reason, final Optional<Object> message) {
getSelf().tell(message.get(), getSender());
}
@Override
public Receive createReceive() {
return receiveBuilder()
.matchEquals("hello", message -> {
messageCounter++;
getSender().tell("response", getSelf());
//Throw Exception at the first 5 messages
if (messageCounter < 5) {
throw new Exception();
}
})
.build();
}
}
BackOffSupervisor配置
private ActorRef createSupervisedActor(Class<? extends Actor> actorClass) {
final Props someActorProps = Props.create(actorClass);
final Props supervisorProps = BackoffSupervisor.props(
Backoff.onStop(
someActorProps, //actor to be supervised
"someActor",
Duration.ofSeconds(10), //min back-off time
Duration.ofMinutes(2), // max back-off time
0.2, // back-off increase factor
10) // max retry limit
.withSupervisorStrategy(
new OneForOneStrategy(
DeciderBuilder
.match(Exception.class, e -> SupervisorStrategy.restart())
.matchAny(o -> SupervisorStrategy.escalate())
.build())
)
);
return testSystem.actorOf(supervisorProps);
}
和測試方法
@Test
public void test() {
new TestKit(testSystem) {{
ActorRef actorRef = createSupervisedActor(SomeActor.class);
actorRef.tell("hello", getRef());
//Expect 5 responses in 1 second
receiveN(5, Duration.ofSeconds(1));
}};
}
測試完成得太快。 在不到一秒鍾的時間內,從BackoffSupervisor的配置中,我預計至少需要50秒鍾以上的時間。
該問題是由於以下原因引起的:
Backoff.onStop不處理在子actor(在我的情況下為someActor)中引發異常,因此由正常的默認監督處理,這意味着立即重啟。 -https://github.com/akka/akka/issues/23406#issuecomment-372602568
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.