Skip to content

Instantly share code, notes, and snippets.

@stillya
Last active November 22, 2021 11:09
Show Gist options
  • Save stillya/0356d455f89aa4ed39777b9015c4c43b to your computer and use it in GitHub Desktop.
Save stillya/0356d455f89aa4ed39777b9015c4c43b to your computer and use it in GitHub Desktop.
Async fire and forget sending message doesn't works correctly, it's block on waiting for metadata request, i guess. P.S. It's not actually bug, maybe this feature will be introduce in the future releases, look at KIP-286.
package org.stilya.kafka;
import java.util.Properties;
import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
public class Main {
public static void main(String[] args) {
String test = "test";
String test1 = "test1";
Properties props = new Properties();
props.put("bootstrap.servers", "localhost: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);
producer.send(new ProducerRecord<>(test,
"test", "test"));
System.out.println("First message in topic test");
System.out.println("STOP BROKER"); // MAKE BREAKPOINT HERE AND STOP BROKER
producer.send(new ProducerRecord<>(test,
"test", "test"));
System.out.println("Second message in topic test");
producer.send(new ProducerRecord<>(test1,
"test", "test"));
System.out.println("First message in topic test1");
producer.close();
}
}
//
//class MyProducerCallback implements Callback {
//
// @Override
// public void onCompletion(RecordMetadata recordMetadata, Exception e) {
// if (e != null)
// System.out.println("AsynchronousProducer failed with an exception " + e.getMessage());
// else
// System.out.println("AsynchronousProducer call Success:");
// }
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment