简体   繁体   English

为什么在 Kafka 集群未运行的情况下创建主题时 AdminClient 不会失败?

[英]Why is AdminClient not failing when creating a topic with Kafka cluster not running?

Why don't I get an error message when I run the following producer code and Kafka is not even running?当我运行以下生产者代码并且 Kafka 甚至没有运行时,为什么我没有收到错误消息?

I would expect the createTopics method to throw an exception, but it doesn't happen.我希望createTopics方法抛出异常,但它不会发生。 Why?为什么?

final Properties properties = new Properties();
properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
properties.put(AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG, "1000");

AdminClient adminClient = AdminClient.create(properties);
NewTopic newTopic = new NewTopic("events", 1, (short) 1);
adminClient.createTopics(Arrays.asList(newTopic));
adminClient.close();

The method createTopics returns a CreateTopicsResult with KafkaFutures as values.方法createTopics返回一个CreateTopicsResultKafkaFutures作为值。 As you are currently not blocking your code for this action to be finished (using get ) and not catching any Exception, your code will just run fine without any notification that the broker is not available.由于您目前没有阻止完成此操作的代码(使用get )并且没有捕获任何异常,因此您的代码将正常运行,而不会收到任何代理不可用的通知。

The following code will throw an ExecutionException when your broker is not available:当您的经纪人不可用时,以下代码将抛出ExecutionException

final Properties properties = new Properties();
properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
properties.put(AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG, "1000");
properties.put(AdminClientConfig.DEFAULT_API_TIMEOUT_MS_CONFIG, "5000");

AdminClient adminClient = AdminClient.create(properties);
NewTopic newTopic = new NewTopic("events-test", 1, (short) 1);
CreateTopicsResult topicResult = adminClient.createTopics(Arrays.asList(newTopic));
KafkaFuture<Void> resultFuture = topicResult.all();
try {
    resultFuture.get();
} catch (InterruptedException e) {
    e.printStackTrace();
} catch (ExecutionException e) {
    e.printStackTrace();
}
adminClient.close();

I tested it using Kafka client 2.5.0 and here is the Excpetion:我使用 Kafka 客户端 2.5.0 对其进行了测试,这是 Excpetion:

java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.TimeoutException: Call(callName=createTopics, deadlineMs=1601136227182) timed out at 1601136227183 after 1 attempt(s)
    at org.apache.kafka.common.internals.KafkaFutureImpl.wrapAndThrow(KafkaFutureImpl.java:45)
    at org.apache.kafka.common.internals.KafkaFutureImpl.access$000(KafkaFutureImpl.java:32)
    at org.apache.kafka.common.internals.KafkaFutureImpl$SingleWaiter.await(KafkaFutureImpl.java:89)
    at org.apache.kafka.common.internals.KafkaFutureImpl.get(KafkaFutureImpl.java:260)
    at org.michael.big.data.kafka.java.BasicAdminClient.main(BasicAdminClient.java:27)
Caused by: org.apache.kafka.common.errors.TimeoutException: Call(callName=createTopics, deadlineMs=1601136227182) timed out at 1601136227183 after 1 attempt(s)
Caused by: org.apache.kafka.common.errors.TimeoutException: Timed out waiting for a node assignment.

Be aware that the class Admin (the super class of AdminClient ) is annotated as Evolving and might be changed in future releases.请注意, Admin类( AdminClient的超类)被注释为Evolving并且可能会在未来版本中更改。

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

相关问题 当发送到通过AdminClient createTopics方法创建的主题时,Kafka生产者抛出“接收到的未知主题或分区错误” - Kafka producer throws “Received unknown topic or partition error” when sending to topic created via AdminClient createTopics method kafka.admin.AdminClient.listAllConsumerGroups()有时会失败 - kafka.admin.AdminClient.listAllConsumerGroups() failing sometimes Java Kafka adminClient主题配置。 配置值被覆盖 - Java Kafka adminClient topic configuration. Configuration values are overwritten 创建主题时如何从 Java Kafka AdminClient 获取详细的错误消息 - How to get detailed error messages from Java Kafka AdminClient when creating topics Kafka adminClient 抛出 TimeoutException - Kafka adminClient throws TimeoutException 使用 SASL_SSL / PLAIN 连接时出现 Kafka AdminClient 错误 - Kafka AdminClient error when connecting using SASL_SSL / PLAIN 创建主题时在Kafka生产者中获取错误错误,但该主题在Kafka服务器上创建 - Gets an error error in Kafka producer when creating topic but the topic is created on the Kafka server 在Apache Kafka中创建主题时出错 - Error creating topic in Apache Kafka Kafka AdminClient 似乎启动了两次 - Kafka AdminClient seems to start twice 使用分区在 kafka -.81 中创建/更新主题 - creating/updating topic in kafka -.81 with partitions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM