I set up a kafka service in the virtual machine of my computer and use the Java client to access it locally. Now if the service on the virtual machine is turned off by me, my local code will not be able to push messages to kafka and will report a timeout error 1 minute later. Can I control the time of this timeout? How do I know that the kafka service on the server is in active status? The following is my local client code, such as pushing messages on the kafka server deployed on the virtual machine. If I turn off the kafka service on the server side or directly open the firewall on the server side, the program will be stuck until an error is reported:
org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.
public static void main(String[] args) throws InterruptedException, ExecutionException {
Properties props = new Properties();
props.put("bootstrap.servers", "192.168.88.131:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
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<>(props);
for (int i = 0; i < 100; iPP){
// Future<RecordMetadata> future = producer.send(new ProducerRecord<String, String>("topicTe1st", Integer.toString(i), Integer.toString(i)));
producer.send(new ProducerRecord<String, String>("topicTest", Integer.toString(i), Integer.toString(i)),
new Callback() {
public void onCompletion(RecordMetadata metadata, Exception e) {
if(e != null) {
e.printStackTrace();
} else {
System.out.println("The offset of the record we just sent is: " + metadata.partition());
}
}
});
//System.out.println(future.get().toString());
System.out.println("send one ");
}
producer.close();
}
then I should know in advance whether the kafka service is turned on or not if I use the code to know in advance.