title: Spring-Kafka Producer Retry when all brokers are down
body: <p>I am using Springboot 2.3.5.RELEASE along with spring-kafka 2.6.3. I am trying do a simple Kafka Producer Retry POC which should result in a producer retrying when the broker is down or if there is an exception thrown before a message is sent to broker.
  <br/>The below producer config is for Idempotent producer with retries enabled.</p>
  <pre><code>// Producer configuration
  @Bean
  public Map&lt;String, Object&gt; producerConfigs() {
     Map&lt;String, Object&gt; props = new HashMap&lt;&gt;();
     props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
     props.put(JsonDeserializer.TRUSTED_PACKAGES, &quot;*&quot;);
     props.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true);
     props.put(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION, &quot;3&quot;);
     props.put(ProducerConfig.RETRIES_CONFIG, &quot;10&quot;);
     props.put(ProducerConfig.ACKS_CONFIG, &quot;all&quot;);
  }
  @Bean
  public ProducerFactory&lt;String, Object&gt; producerFactory() {
      return new DefaultKafkaProducerFactory&lt;&gt;(producerConfigs());
  }
  @Bean
  public KafkaTemplate&lt;String, Object&gt; kafkaTemplate() {
      return new KafkaTemplate&lt;&gt;(producerFactory());
  }
  ... other configs
  </code></pre>
  <p>Iam aware that Kafka Producer Retries work only if there is a transient error in broker sending ack to producer. So, if the broker is down even before the message is sent then the above retry does not work.
  Therefore I tried to introduce @Retry with spring-kafka, unfortunately I am unable to get this to work either. Does anybody know how to solve this as this seems to be a common usecase like if the broker goes down or if there is a network glitch I don't want my producers to stop working</p>
  <pre><code>@Transaction
  @Retryable(value = Exception.class, maxAttemptsExpression = &quot;10&quot;,
          backoff = @Backoff(delayExpression = &quot;1000&quot;))
  public void sendTestMessage(String name, String number) throws RuntimeException {
      TestObject testObj = new TestObject(name + &quot;: &quot; + String.valueOf(number),
              Double.valueOf(Math.random() * 10000).longValue(),
              &quot;Blah&quot; + String.valueOf(Math.random() * 10));
      log.info(&quot;Sending sendTestMessage {}&quot;, name); 
      throwSomeException();
      // sends the message
      kafkaTemplate.send(MyConstants.SomeTOPIC, testObj);     
  }  
  // Throwing mock exception
  public void throwSomeException() throws RuntimeException {
          log.info(&quot;Throwing mock exception {}&quot;);
          throw new RuntimeException(&quot;Mock Exception thrown while sending the 
          sendTestMessage method&quot;);
  }   
  @Recover
  public void recover(Exception e, TestObject testObj) { 
      log.info(&quot;Recovered exception thrown  mock exception {} with payload as {}&quot;, e, testObj);
  }
  </code></pre>
  <p>Is there anyway a Retry could work for the above. I find it hard to believe that such a functionality is not available inbuilt by default.</p>