Complete the code to initialize the Kafka producer with transactions enabled.
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("enable.idempotence", [1]); KafkaProducer<String, String> producer = new KafkaProducer<>(props);
Complete the code to initialize the transaction with a unique transactional ID.
props.put("transactional.id", [1]); producer.initTransactions();
Fix the error in starting a transaction before sending messages.
producer.[1](); producer.send(new ProducerRecord<>("my-topic", "key", "value")); producer.commitTransaction();
Fill both blanks to handle exceptions properly during a transaction.
try { producer.[1](); producer.send(new ProducerRecord<>("my-topic", "key", "value")); producer.commitTransaction(); } catch (ProducerFencedException e) { producer.close(); } catch (Exception e) { producer.[2](); }
Fill all three blanks to create a transactional producer, send a message, and commit the transaction.
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("enable.idempotence", [1]); props.put("transactional.id", [2]); KafkaProducer<String, String> producer = new KafkaProducer<>(props); producer.[3](); producer.beginTransaction(); producer.send(new ProducerRecord<>("my-topic", "key", "value")); producer.commitTransaction();
