title: How to detect if kafka broker is not available from consumer in java?
body: <p>I have a simple Kafka consumer in Java. I'm trying to catch an exception if Kafka broker is not available. I need it to interrupt the thread.</p>
  
  <p>I have code like this:</p>
  
  <pre><code>KafkaConsumer&lt;String, String&gt; kafkaConsumer = new KafkaConsumer&lt;&gt;(kafkaConsumerProperties());
  kafkaConsumer.subscribe(Arrays.asList(topic));
  try {
      ConsumerRecords&lt;String, String&gt; records = kafkaConsumer.poll(500);
      // records handling
  } catch(Exception e) {
      System.out.println(e.getMessage());
  }finally {
      kafkaConsumer.close();
  }
  </code></pre>
  
  <p>If Kafka server is down I don't catch any exceptions but following messages are displayed in log:</p>
  
  <pre><code>18/03/28 13:33:39 WARN clients.NetworkClient: [Consumer clientId=consumer-3, groupId=JAVA] Connection to node -1 could not be established. Broker may not be available.
  18/03/28 13:33:40 WARN clients.NetworkClient: [Consumer clientId=consumer-1, groupId=JAVA] Connection to node -1 could not be established. Broker may not be available.
  </code></pre>
  
  <p>Is there a way to handle it in my thread?</p>