|
| 1 | +package com.baeldung.kafka; |
| 2 | + |
| 3 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 4 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 5 | +import org.apache.kafka.clients.consumer.KafkaConsumer; |
| 6 | +import org.apache.kafka.clients.consumer.OffsetAndMetadata; |
| 7 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 8 | +import org.apache.kafka.clients.producer.ProducerConfig; |
| 9 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 10 | +import org.apache.kafka.common.KafkaException; |
| 11 | +import org.apache.kafka.common.TopicPartition; |
| 12 | + |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Properties; |
| 17 | + |
| 18 | +import static java.time.Duration.ofSeconds; |
| 19 | +import static java.util.Collections.singleton; |
| 20 | +import static org.apache.kafka.clients.consumer.ConsumerConfig.*; |
| 21 | +import static org.apache.kafka.clients.consumer.ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG; |
| 22 | +import static org.apache.kafka.clients.producer.ProducerConfig.*; |
| 23 | + |
| 24 | +public class TransactionalApp { |
| 25 | + |
| 26 | + private static final String CONSUMER_GROUP_ID = "test"; |
| 27 | + private static final String OUTPUT_TOPIC = "output"; |
| 28 | + private static final String INPUT_TOPIC = "input"; |
| 29 | + |
| 30 | + public static void main(String[] args) { |
| 31 | + |
| 32 | + KafkaConsumer<String, String> consumer = initConsumer(); |
| 33 | + KafkaProducer<String, String> producer = initProducer(); |
| 34 | + |
| 35 | + producer.initTransactions(); |
| 36 | + |
| 37 | + try { |
| 38 | + |
| 39 | + while (true) { |
| 40 | + |
| 41 | + ConsumerRecords<String, String> records = consumer.poll(ofSeconds(20)); |
| 42 | + |
| 43 | + producer.beginTransaction(); |
| 44 | + |
| 45 | + for (ConsumerRecord record : records) |
| 46 | + producer.send(new ProducerRecord(OUTPUT_TOPIC, record)); |
| 47 | + |
| 48 | + Map<TopicPartition, OffsetAndMetadata> offsetsToCommit = new HashMap<>(); |
| 49 | + |
| 50 | + for (TopicPartition partition : records.partitions()) { |
| 51 | + List<ConsumerRecord<String, String>> partitionedRecords = records.records(partition); |
| 52 | + long offset = partitionedRecords.get(partitionedRecords.size() - 1).offset(); |
| 53 | + |
| 54 | + offsetsToCommit.put(partition, new OffsetAndMetadata(offset)); |
| 55 | + } |
| 56 | + |
| 57 | + producer.sendOffsetsToTransaction(offsetsToCommit, CONSUMER_GROUP_ID); |
| 58 | + producer.commitTransaction(); |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + } catch (KafkaException e) { |
| 63 | + |
| 64 | + producer.abortTransaction(); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + private static KafkaConsumer<String, String> initConsumer() { |
| 72 | + Properties props = new Properties(); |
| 73 | + props.put(BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); |
| 74 | + props.put(GROUP_ID_CONFIG, CONSUMER_GROUP_ID); |
| 75 | + props.put(ENABLE_AUTO_COMMIT_CONFIG, "false"); |
| 76 | + props.put(KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer"); |
| 77 | + props.put(VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer"); |
| 78 | + |
| 79 | + KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); |
| 80 | + consumer.subscribe(singleton(INPUT_TOPIC)); |
| 81 | + return consumer; |
| 82 | + } |
| 83 | + |
| 84 | + private static KafkaProducer<String, String> initProducer() { |
| 85 | + |
| 86 | + Properties props = new Properties(); |
| 87 | + props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); |
| 88 | + props.put(ACKS_CONFIG, "all"); |
| 89 | + props.put(RETRIES_CONFIG, 3); |
| 90 | + props.put(BATCH_SIZE_CONFIG, 16384); |
| 91 | + props.put(LINGER_MS_CONFIG, 1); |
| 92 | + props.put(BUFFER_MEMORY_CONFIG, 33554432); |
| 93 | + props.put(ENABLE_IDEMPOTENCE_CONFIG, "true"); |
| 94 | + props.put(TRANSACTIONAL_ID_CONFIG, "prod-1"); |
| 95 | + props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer"); |
| 96 | + props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer"); |
| 97 | + |
| 98 | + return new KafkaProducer(props); |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments