0
我將探索Akka
生命週期和Akka
演員監督策略。我正在創建我自己的例外情況,並重寫public SupervisorStrategy supervisorStrategy()
探索阿卡生命週期及其恢復策略的方法。以下是我的代碼:Akka:未使用Java註冊的自定義akka監督策略
public class JavaActor extends AbstractActor {
@Override
public PartialFunction<Object, BoxedUnit> receive() {
System.out.println("---- In the receive method "+Thread.currentThread().getName());
return ReceiveBuilder.
matchEquals("Ping" , s -> {
System.out.println("$$$ Ping Match Successfully");
throw new EsclateException("Might be esclate");
}).
matchAny(x -> {
System.out.println("### Matched value is : "+ x);
sender().tell(new Status.Failure(new Exception("unknown message")), self());
}).build();
}
@Override
public void preRestart(Throwable reason, Option<Object> message) throws Exception {
super.preRestart(reason, message);
System.out.println(">>> Actor preRestart method calls : "+Thread.currentThread().getName());
}
@Override
public void postRestart(Throwable reason) throws Exception {
super.postRestart(reason);
System.out.println(">>> Actor postRestart method calls : "+Thread.currentThread().getName());
}
@Override
public void preStart() throws Exception {
super.preStart();
System.out.println(">>> Actor preStart method calls "+Thread.currentThread().getName());
}
@Override
public void postStop() throws Exception {
super.postStop();
System.out.println(">>> Actor postStop method calls "+Thread.currentThread().getName());
}
@Override
public SupervisorStrategy supervisorStrategy() {
System.out.println("**** SupervisorStrategy Override Successfully ****");
return new OneForOneStrategy(5, Duration.create(1, TimeUnit.MINUTES),
DeciderBuilder.match(ResumeException.class, e -> SupervisorStrategy.resume())
.match(RestartException.class, e -> SupervisorStrategy.restart())
.match(StopException.class, e -> SupervisorStrategy.stop())
.match(EsclateException.class, e -> SupervisorStrategy.escalate())
.matchAny(e -> SupervisorStrategy.escalate()).build());
}}
在方法時,我扔EsclateException
日誌繼續而不是終止演員。但是當我用throw new Error()
替換異常代碼時,演員停止工作並終止。從輸出結果看來,我的SupervisorStrategy
未註冊,演員運行默認SupervisorStrategy
。我如何註冊我的策略?
下面是我的調用代碼:
public class JavaActorTest {
public static ActorSystem system;
@BeforeClass
public static void start() {
system = ActorSystem.create("ActorLifeCycleTest");
}
@AfterClass
public static void cleanup() {
JavaTestKit.shutdownActorSystem(system);
system = null;
}
@Test
public void testActorCreation(){
TestActorRef<JavaActor> actorRef = TestActorRef.create(system, Props.create(JavaActor.class));
actorRef.tell("Ping", ActorRef.noSender());
assertThat(true, is(true));
}}
嘿@johanandren是否有可能改變監護人的監督策略? –
是的,它在文檔中提到。但我建議你引入你自己的主管演員,而不是改變用戶監護人的策略。 – johanandren
這很酷,你可以請發送鏈接,他們提到改變監護人策略?我只想探索更多,那就是試圖改變監護人戰略的乳清。 –