[英]Getting Akka actor to wait for external inputs without hanging other actors
[英]Akka actors are getting stopped
我正在使用Akka actor來實現對某些http請求的並行處理。 我使用RoundRobinPool
來RoundRobinPool
演員池,例如:
ActorRef myActorPool = actorSystem.actorOf(new RoundRobinPool(200).props(Props.create(MyActor.class, args)), MyActor.class.getSimpleName());
一切正常。 但是在該程序運行了一段時間后,出現以下錯誤
java.util.concurrent.CompletionException:akka.pattern.AskTimeoutException:Recipient [Actor [akka:// web_server / user / MyActor#-769383443]]已被終止。 Sender [null]發送了類型為“ com.data.model.Request”的消息。
因此,我重寫了postStop
方法,並在其中添加了一條日志語句。
@Override
public void postStop() {
log.warn("Actor is stopped");
}
現在,我可以在日志中看到參與者正在停止。 但是我不確定它是針對哪個請求的。 一旦池中的所有參與者終止(我設置的池大小為200),我將得到AskTimeoutException
。 無論如何,有沒有調試演員為什么被解雇的原因?
編輯1
在控制器中,我使用的是像
CompletableFuture<Object> s = ask(myActorPool, request, 1000000000).toCompletableFuture();
return s.join();
參與者僅處理一種消息。
@Override
public AbstractActor.Receive createReceive() {
return receiveBuilder()
.match(Request.class, this::process)
.build();
}
private void process(Request request) {
try {
// code here
} catch (Exception e) {
log.error(e.getMessage(), e);
getSender().tell(new akka.actor.Status.Failure(e), getSelf());
}
}
就描述探針而言,似乎您正在Ask呼叫內處理數據,並且花費的時間比askTimeout多,並且您正在收到錯誤。
您可以做的是增加askTimeout或在Ask呼叫內進行更少的處理。
您不應在Ask調用內執行CPU綁定操作,否則可能導致系統變慢,建議您在Ask調用內執行I / O綁定操作。 這樣,您可以利用演員。
例如:
val x=x+1 // CPU bound operation should not do inside the ask call
您可以在Ask調用內執行一些數據庫調用,這是更好的選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.