The connection problem of kafka, how can I know whether the kafka service on the server side is turned on through the code?

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.

Oct.28,2021

Don't make a program big and comprehensive, divide and conquer. Service monitoring does not need to be considered in your program code. Your program only needs to do two things:

  • connection kafka has a timeout setting
  • if you fail, you can try again

both of the above points are configured with the official Kafka client, which can be configured directly.

as for the kafka service, it is left to the service monitoring system. One of the simplest strategies is to turn kafka into a system service (inherent in the CDH version) and leave it to the system's service management system to manage. You can see the running status of the service with service kafka status . If there is an alarm system, give it to the alarm system to report to the police.

your program should not take matters into its own hands and do the work of service management.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b2babd-2bac4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b2babd-2bac4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?