1. Idempotent producers
enable.idempotence=true yapılır. Böylece gönderilen mesajların birer birer artan ve ardışık olması garanti edilir. Producer Ayarları - idempotent yazısına bakabilirsiniz
transactional.id alanına değer string değer atanır
Örnek
Şöyle yaparız
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 MyKafkaProducerForExactlyOnceDelivery { public static void main(String[] args) { Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); // enables idempotence props.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true); // enables transaction guarantee props.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "my-transaction-id"); Producer<String, String> producer = new KafkaProducer<>(props, new StringSerializer(), new StringSerializer()); producer.initTransactions(); try { // Start the transaction producer.beginTransaction(); for (int i = 0; i <= 100; i++){ producer.send(new ProducerRecord<>("my-producer-topic", 0, Integer.toString(i), Integer.toString(i))); } // Commit the transaction producer.commitTransaction(); } catch (Exception e) { // Abort the transaction on error producer.abortTransaction(); } finally { producer.close(); } } }
No comments:
Post a Comment