title: How to get Kafka connection state without parsing the log file? To be able to take action on "Connection to node terminated during authentication."
body: <p>When authenticating with incorrect credentials, I'm getting the following expected message during authentication.</p>
  
  <blockquote>
    <p>[Consumer clientId=consumer-1, groupId=xxx] Connection to node -1
    terminated during authentication. This may indicate that
    authentication failed due to invalid credentials.</p>
  </blockquote>
  
  <p>That's all nice, but right now I can only find this information in the log file, while I would like to take action on this message.</p>
  
  <p>I also looked at the Kafka source code where this message is originating from:</p>
  
  <p>(In 1.1) <a href="https://github.com/apache/kafka/blob/1.1/clients/src/main/java/org/apache/kafka/clients/NetworkClient.java" rel="nofollow noreferrer">https://github.com/apache/kafka/blob/1.1/clients/src/main/java/org/apache/kafka/clients/NetworkClient.java</a>
  (or in trunk) <a href="https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/clients/NetworkClient.java" rel="nofollow noreferrer">https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/clients/NetworkClient.java</a>)</p>
  
  <p>There is a bit of code like this:</p>
  
  <pre><code>switch (disconnectState.state()) {
              case AUTHENTICATION_FAILED:
                  connectionStates.authenticationFailed(nodeId, now, disconnectState.exception());
                  log.error("Connection to node {} failed authentication due to: {}", nodeId, disconnectState.exception().getMessage());
                  break;
              case AUTHENTICATE:
                  // This warning applies to older brokers which dont provide feedback on authentication failures
                  log.warn("Connection to node {} terminated during authentication. This may indicate " +
                          "that authentication failed due to invalid credentials.", nodeId);
                  break;
  </code></pre>
  
  <p><strong>Important note</strong>: I override the authentication mechanism in Kafka with a custom module based on SASL_PLAIN and while I was just debugging I realized something else failed in this module during authentication, causing a different exception from this module, which meant it triggered this 'older broker' code path. But still, I want to take some action on this state.</p>
  
  <p>I have tried creating a listener, like this:</p>
  
  <pre><code>    final ContainerProperties containerProperties = new ContainerProperties("TEST_TOPIC");
      containerProperties.setAckMode(AbstractMessageListenerContainer.AckMode.MANUAL_IMMEDIATE);
      containerProperties.setMessageListener((AcknowledgingMessageListener&lt;GenericRecord, GenericRecord&gt;) (record, ack) -&gt; logger.info("Got record on test topic"));
      KafkaMessageListenerContainer&lt;GenericRecord, GenericRecord&gt; container = new KafkaMessageListenerContainer&lt;&gt;(kafkaConsumerFactory, containerProperties);
      container.start();
  </code></pre>
  
  <p>But the code doesn't throw any authentication exception, it just keeps printing the above message in the log.</p>
  
  <p>I have also tried creating a simple consumer, like this:</p>
  
  <pre><code>    Consumer&lt;GenericRecord, GenericRecord&gt; consumer = kafkaConsumerFactory.createConsumer();
      consumer.subscribe(Collections.singleton("BX_TEST_TOPIC"));
      try {
          ConsumerRecords&lt;GenericRecord, GenericRecord&gt; poll = consumer.poll(10);
      }
      catch (Exception e) {
          logger.info("Exception during polling", e);
      }
  </code></pre>
  
  <p>But this code does not throw any exception either, it also just keeps printing the message in the log. Even though according to <a href="https://kafka.apache.org/10/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html#poll-long-" rel="nofollow noreferrer">https://kafka.apache.org/10/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html#poll-long-</a> the poll method possibly throws an AuthenticationException. This is probably because of the 'older broker' kind of message, which in the source code only shows a warning in the log.</p>
  
  <p>So, how to actually catch any AuthenticationException or get the connection state in some meaningful way instead of parsing the log, if the server doesn't return an authentication exception but still fails to log in?</p>
  
  <p>Note exactly the same problem occurs when authentication is not the problem, but something else fails. For instance, sometimes my Kafka cluster doesn't start correctly, in which case I get this message: </p>
  
  <blockquote>
    <p>Connection to node -1 could not be established. Broker may not be
    available.</p>
  </blockquote>
  
  <p>which I can also not check on. Instead the log just keeps filling up with this message as the client gets in some infinite loop trying to connect.</p>