简体   繁体   English

如何修复“Futures 超时 [5 秒]”和 [akkaDeadLetter] 错误?

[英]How can I fix "Futures timed out [5 seconds]" and [akkaDeadLetter] errors?

I'm trying to realize numbersPrinter class (the code:我正在尝试实现 numbersPrinter 类(代码:

package org.example.actors;

import akka.actor.AbstractActor;
import akka.actor.Props;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import akka.japi.Procedure;

import java.util.ArrayList;

public class numbersPrinter extends AbstractActor {
    // fields
    LoggingAdapter log = Logging.getLogger(getContext().system(), this);

    // the method
    public void returnNumber(Object message){
        if (message.equals(String.valueOf(1))){
            getSender().tell(1, getSelf());
        } else if (message instanceof String) {
            log.info((String) message);
        } else {
            unhandled(message);
            log.info("Unhandled message");
        }
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .create()
                .match(numbersPrinter.class, this::returnNumber)
                .build();
    }

}

Main class code fragment:主类代码片段:

package org.example;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.pattern.Patterns;
import akka.util.Timeout;
import org.example.actors.numbersPrinter;
import scala.concurrent.Await;
import scala.concurrent.duration.Duration;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

// actors system
        final ActorSystem system = ActorSystem.create(
                "ActorsSystem");

        // the actor for numbers output
        ActorRef numbersPrinter = system.actorOf(Props.create(numbersPrinter.class, numbersPrinter::new), "numbersPrinter");
        // number 1 output test
        System.out.println("numbersPrinter actor test");
        final Timeout timeout = new Timeout(Duration.create(5, TimeUnit.SECONDS));
        scala.concurrent.Future<Object> rt = Patterns.ask(numbersPrinter, String.valueOf(1), timeout);
        try {
            String result = (String) Await.result(rt, timeout.duration());
            System.out.println(result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

The following errors are occuring:出现以下错误:

[INFO] [akkaDeadLetter][12/22/2022 20:10:42.028] [ActorsSystem-akka.actor.default-dispatcher-6] [akka://ActorsSystem/user/numbersPrinter] Message [java.lang.String] from Actor[akka://ActorsSystem/temp/numbersPrinter$a] to Actor[akka://ActorsSystem/user/numbersPrinter#-1051654874] was unhandled. [信息] [akkaDeadLetter] [12/22/2022 20:10:42.028] [ActorsSystem-akka.actor.default-dispatcher-6] [akka://ActorsSystem/user/numbersPrinter] 消息 [java.lang.String]从 Actor[akka://ActorsSystem/temp/numbersPrinter$a] 到 Actor[akka://ActorsSystem/user/numbersPrinter#-1051654874] 未处理。 [1] dead letters encountered. [1] 遇到死信。 This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.可以使用配置设置“akka.log-dead-letters”和“akka.log-dead-letters-during-shutdown”关闭或调整此日志记录。

Exception in thread "main" java.lang.RuntimeException:
java.util.concurrent.TimeoutException: Future timed out after [5 seconds]
    at org.example.Main.main(Main.java:63)
Caused by: java.util.concurrent.TimeoutException: Future timed out after [5 seconds]
at scala.concurrent.impl.Promise$DefaultPromise.tryAwait0(Promise.scala:248)
at scala.concurrent.impl.Promise$DefaultPromise.result(Promise.scala:261)
at scala.concurrent.Await$.$anonfun$result$1(package.scala:201)
at scala.concurrent.BlockContext$DefaultBlockContext$.blockOn(BlockContext.scala:62)
at scala.concurrent.Await$.result(package.scala:124)
at scala.concurrent.Await.result(package.scala)
at org.example.Main.main(Main.java:60)

Tell me please, how to fix it.请告诉我,如何解决它。

Futures timed out and messages going to DeadLetter are generally indications that something is wrong, but not what is wrong. Futures timed out和发送到DeadLetter的消息通常表明出现问题,但并不是什么地方出了问题。

The future timing out is an indication that your actor isn't sending a reply to the asker (more properly, that the temporary actor created for the ask pattern hasn't received the reply by the timeout, but especially in the local case with an application where there aren't a lot of actors "chatting away", this is equivalent to the reply not being sent).未来超时表明您的演员没有向提问者发送回复(更准确地说,为提问模式创建的临时演员没有在超时前收到回复,但特别是在本地情况下没有很多演员“闲聊”的应用程序,这相当于没有发送回复)。

The dead letters indicate that your numbersPrinter actor isn't handling the message of type String .死信表示您的numbersPrinter actor 没有处理String类型的消息。

returnNumber certainly appears to handle that message and reply, but the culprit is the Receive that you're building: returnNumber显然似乎可以处理该消息并进行回复,但罪魁祸首是您正在构建的Receive

receiveBuilder().create().match(numbersPrinter.class, this::returnNumber)

sets up the Receive to only pass messages of type numbersPrinter to returnNumber .Receive设置为仅将numbersPrinter returnNumber Since that's the only match call in that Receive , messages of all other types are ignored and will go to dead letters.由于这是该Receive中唯一的match调用,因此所有其他类型的消息都将被忽略并变成死信。

Since your returnNumber method is total for all Object s, it probably makes sense to replace numbersPrinter.class in the match with Object.class ;由于您的returnNumber方法是所有Object的总和,因此将match中的numbersPrinter.class替换为Object.class可能是有意义的; replacing with String.class would also work.String.class替换也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM