Kafka producer sync send

Earlier example, we have sent a message without knowing the result of a sent message.

In this example will send a message will wait until the result comes(either success or failure),this mechanism called as a synchronous message send

producer synchronous example

CopiedCopy Code
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
public class KafkaSyncSend {
	public static void main(String[] args) {
		ProducerRecord<String , String> record = new ProducerRecord<>("Producer","Producer");
		try {
			Properties props = new Properties();
			props.put("bootstrap.servers", "localhost:9092");
			props.put("client.id", "KafkaProducer");
			props.put("key.serializer", StringSerializer.class.getName());
			props.put("value.serializer", StringSerializer.class.getName());
			Producer<String , String> producer=new KafkaProducer(props);
			RecordMetadata metadata=producer.send(record).get();
		} catch (Exception e) {

The above example will wait until the result appears, it can be either a success or a failure.