Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the Kafka producer with transactions enabled.
Kafka
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);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string "true" instead of boolean true
Setting 'enable.idempotence' to false
Omitting the property entirely
✗ Incorrect
To enable transactions, idempotence must be enabled by setting 'enable.idempotence' to true (boolean).
2fill in blank
mediumComplete the code to initialize the transaction with a unique transactional ID.
Kafka
props.put("transactional.id", [1]); producer.initTransactions();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the transactional ID string
Using an invalid or empty transactional ID
✗ Incorrect
The transactional ID must be a string, so it needs to be enclosed in quotes.
3fill in blank
hardFix the error in starting a transaction before sending messages.
Kafka
producer.[1](); producer.send(new ProducerRecord<>("my-topic", "key", "value")); producer.commitTransaction();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'startTransaction' which does not exist
Using plural 'beginTransactions'
Using 'initTransaction' which is incorrect
✗ Incorrect
The correct method to start a transaction is 'beginTransaction()'.
4fill in blank
hardFill both blanks to handle exceptions properly during a transaction.
Kafka
try { producer.[1](); producer.send(new ProducerRecord<>("my-topic", "key", "value")); producer.commitTransaction(); } catch (ProducerFencedException e) { producer.close(); } catch (Exception e) { producer.[2](); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling commitTransaction() in the catch block instead of abortTransaction()
Not handling ProducerFencedException properly
✗ Incorrect
You start the transaction with 'beginTransaction()' and abort it with 'abortTransaction()' on exceptions.
5fill in blank
hardFill all three blanks to create a transactional producer, send a message, and commit the transaction.
Kafka
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();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'beginTransaction()' instead of 'initTransactions()' to initialize
Not quoting the transactional ID string
Setting enable.idempotence as a string instead of boolean
✗ Incorrect
Enable idempotence with true, set transactional.id as a string, and initialize transactions before use.