[英]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返回一个
CreateTopicsResult
与KafkaFutures
作为值。 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.