我正在尝试手动处理与kafka的连接问题。
当我尝试使用kafka无法访问的代码时:
public class Launcher {
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<String, String>(props);
for(int i = 0; i < 10; i++) {
producer.send(new ProducerRecord<String, String>("topicTest", Integer.toString(i), Integer.toString(i)),
(metadata, exception) -> {
if (exception != null)
System.out.println("Exception:" + exception.getMessage());
}
);
}
producer.close();
}
}
我陷入无限循环:
2017-03-09 16:54:33 DEBUG NetworkClient:570 - Node -1 disconnected.
2017-03-09 16:54:33 DEBUG NetworkClient:680 - Give up sending metadata request since no node is available
2017-03-09 16:54:33 DEBUG NetworkClient:767 - Initialize connection to node -1 for sending metadata request
2017-03-09 16:54:33 DEBUG NetworkClient:627 - Initiating connection to node -1 at localhost:9092.
2017-03-09 16:54:33 DEBUG Selector:339 - Created socket with SO_RCVBUF = 32768, SO_SNDBUF = 131072, SO_TIMEOUT = 0 to node -1
2017-03-09 16:54:33 DEBUG NetworkClient:590 - Completed connection to node -1. Fetching API versions.
2017-03-09 16:54:33 DEBUG NetworkClient:603 - Initiating API versions fetch from node -1.
2017-03-09 16:54:33 DEBUG Selector:375 - Connection with localhost/127.0.0.1 disconnected
java.io.EOFException
at org.apache.kafka.common.network.NetworkReceive.readFromReadableChannel(NetworkReceive.java:83)
at org.apache.kafka.common.network.NetworkReceive.readFrom(NetworkReceive.java:71)
at org.apache.kafka.common.network.KafkaChannel.receive(KafkaChannel.java:169)
at org.apache.kafka.common.network.KafkaChannel.read(KafkaChannel.java:150)
at org.apache.kafka.common.network.Selector.pollSelectionKeys(Selector.java:355)
at org.apache.kafka.common.network.Selector.poll(Selector.java:303)
at org.apache.kafka.clients.NetworkClient.poll(NetworkClient.java:349)
at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:225)
at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:126)
at java.lang.Thread.run(Thread.java:745)
即使使用回调,我也无法访问异常。 处理连接错误的正确方法是什么?