亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何檢測akka actor終止是由于系統關閉并避免重新啟動它

如何檢測akka actor終止是由于系統關閉并避免重新啟動它

holdtom 2023-06-04 17:41:48
我有一個 Spring 應用程序,它使用一個小型 Akka actor 系統(使用 Java),其中我有一個MasterActor擴展 Akka 的系統AbstractActor,它初始化 aRouter并設置一些 worker actor。它還監視工人的生命周期。我想重啟一個 Worker 演員,如果它恰好因為某些原因而死亡Exception。 public MasterActor(ActorPropsFactory actorPropsFactory) {    this.actorPropsFactory = actorPropsFactory;    int workers = Runtime.getRuntime().availableProcessors() - 1;    List<Routee> routees = Stream.generate(this::createActorRefRoutee).limit(workers).collect(Collectors.toList());    this.router = new Router(new ConsistentHashingRoutingLogic(getContext().system()), routees);  }  private ActorRefRoutee createActorRefRoutee() {    ActorRef worker = getContext().actorOf(actorPropsFactory.create(getWorkerActorClass()));    getContext().watch(worker);    return new ActorRefRoutee(worker);  }  private void route(Object message, Supplier<String> routingKeySupplier) {    String routingKey = routingKeySupplier.get();    RouterEnvelope envelope = new ConsistentHashingRouter.ConsistentHashableEnvelope(message, routingKey);    router.route(envelope, getSender());  } @Override  public Receive createReceive() {    return receiveBuilder()        .match(            EventMessage.class,            message -> this.route(message, () -> message.getEvent().getId().toString()))        .match(            Terminated.class,            message -> {              logger.info("WorkerActor {} terminated, restarting", message.getActor());              // todo: detect whether the system is shutting down before restarting the actor              router = router.removeRoutee(message.actor())                             .addRoutee(createActorRefRoutee());            })        .build();  }我遇到的問題是,如果 Spring 應用程序無法啟動。(例如,它無法連接到數據庫,或者某些憑據不正確等等),我收到了Terminated所有工作人員的消息,Master actor 嘗試啟動新的,這也會Terminated立即進入,進入無限循環。檢測這種情況的正確方法是什么?有沒有辦法讓 Master actor 檢測到 actor 系統正在關閉,這樣 workers 就不會再次重啟?
查看完整描述

1 回答

?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

難道你不能只為你的路由器設置一個監督策略,這樣你就可以檢查導致失敗的異常類型嗎?這樣你也不需要手動重啟你的工人。


編輯:


SupervisorStrategy你這樣設置:


private static SupervisorStrategy strategy=

    new OneForOneStrategy(

    10,

    Duration.ofMinutes(1),

    DeciderBuilder.match(ArithmeticException.class,e->SupervisorStrategy.resume())

    .match(NullPointerException.class,e->SupervisorStrategy.restart())

    .match(IllegalArgumentException.class,e->SupervisorStrategy.stop())

    .matchAny(o->SupervisorStrategy.escalate())

    .build());

final ActorRef router=

        system.actorOf(

        new RoundRobinPool(5).withSupervisorStrategy(strategy).props(Props.create(Echo.class)));


查看完整回答
反對 回復 2023-06-04
  • 1 回答
  • 0 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號