有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么没有未来。得到(…)杀线?

我有一个应用程序正在使用future进行异步执行
我在get方法上设置了参数,当线程没有得到响应时,它应该在10秒后被终止:

 Future<RecordMetadata> meta = producer.send(record, new ProducerCallBack());
      RecordMetadata data = meta.get(10, TimeUnit.SECONDS);  

但是线程在60秒后被杀死:

java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.
    at org.apache.kafka.clients.producer.KafkaProducer$FutureFailure.<init>(KafkaProducer.java:1124)
    at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:823)
    at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:760)
    at io.khinkali.KafkaProducerClient.main(KafkaProducerClient.java:49)
Caused by: org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.

我做错了什么


共 (2) 个答案

  1. # 1 楼答案

    docs开始:

    The threshold for time to block is determined by max.block.ms after which it throws a TimeoutException.

    检查logback中的Kafka Appender配置。xml,查找:

    <producerConfig>max.block.ms=60000</producerConfig>
    
  2. # 2 楼答案

    I set the parameter on get method, that the thread should get killed after 10 seconds, when it does not get the response:

    如果我们谈论的是Future.get(...),那么它根本就不会“杀死”线程。引用javadocsFuture.get(...)方法:

    Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.

    如果get(...)方法超时,那么它将抛出TimeoutException,但是您的线程可以继续运行。如果要停止线程运行,则需要捕获TimeoutException,然后调用^{},但即使这样也不能保证线程将被“杀死”。这会导致线程中断,这意味着certain methods will throw ^{}或线程需要检查Thread.currentThread().isInterrupted()

    java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.

    是的,这个超时与Future.get(...)超时无关