title: Send an acknowledgment to the method call using a Boolean variable (here sent) in Kafka Producer
body: <p>I'm implementing a sendStringMessage method that returns true if the message is being sent to the topic. But I'm getting error( as indicated in code) Local variable sent defined in an enclosing scope must be final or effectively final.
  Or can we create a custom callback function that returns a boolean when the message is sent to the topic.</p>
  <pre><code>    private KafkaProducer&lt;String, String&gt; kafkaProducerFactory(String message){
          Properties producerConfigs = new Properties();
          producerConfigs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, Bootstrap_Server);
          producerConfigs.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);  
          producerConfigs.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); 
          KafkaProducer&lt;String, String&gt; producer = new KafkaProducer&lt;String, String&gt;(producerConfigs);
          return producer;
      }
      
      private boolean sendStringMessage (String topic , String message) throws InterruptedException, ExecutionException {
          KafkaProducer&lt;String, String&gt; producer = kafkaProducerFactory(message);
          ProducerRecord&lt;String, String&gt; record = new ProducerRecord&lt;String, String&gt;(topic ,  message);
          boolean sent = false;
          try {
              producer.send(record,  new Callback() {
                  public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                      if (e == null) {
                          sent = true;    // Error   --&gt;  Local variable sent defined in an enclosing scope must be final or effectively final 
                          logger.info(&quot;Kafka Message Successfully received the details by the topic : \n&quot; 
                          + &quot;\tTopic : {}\n&quot;+ &quot;\tPartition : {}\n&quot; + &quot;\tOffset : {}\n&quot; + &quot;\tTimestamp : {}&quot;,
                                  recordMetadata.topic(), recordMetadata.partition(), recordMetadata.offset(),
                                  recordMetadata.timestamp());
                          logger.info(&quot;kafka Message send by the producer : &quot;+ message );
                      }
                      else {
                          sent = false;   // Error   --&gt;  Local variable sent defined in an enclosing scope must be final or effectively final
                          logger.info(&quot;Kafka Message Not received - System Error Message :&quot;);
                      }
                  }
              }).get();
          }
          finally {
              producer.flush();
              producer.close();   
          }   
          return sent;
      }
  </code></pre>