1 package de.juplo.kafka.outbox.delivery;
3 import com.google.common.primitives.Longs;
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.common.PartitionInfo;
8 import org.apache.kafka.common.TopicPartition;
9 import org.apache.kafka.common.serialization.StringDeserializer;
10 import org.apache.kafka.common.serialization.StringSerializer;
12 import java.time.Clock;
13 import java.time.Duration;
14 import java.time.LocalTime;
17 import org.apache.kafka.clients.producer.KafkaProducer;
18 import org.apache.kafka.clients.producer.ProducerRecord;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.springframework.scheduling.annotation.Scheduled;
23 import javax.annotation.PreDestroy;
25 import static org.apache.kafka.clients.CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG;
26 import static org.apache.kafka.clients.consumer.ConsumerConfig.*;
27 import static org.apache.kafka.clients.producer.ProducerConfig.*;
30 public class OutboxProducer
32 final static Logger LOG = LoggerFactory.getLogger(OutboxProducer.class);
34 public final static String HEADER = "#";
36 private final OutboxRepository repository;
37 private final KafkaProducer<String, String> producer;
38 private final String topic;
39 private final Set<Long> send = new HashSet<>();
40 private final Clock clock;
41 private final Duration cleanupInterval;
43 private long sequenceNumber = 0l;
44 private LocalTime nextCleanup;
46 public OutboxProducer(
47 ApplicationProperties properties,
48 OutboxRepository repository,
51 this.repository = repository;
53 Properties props = new Properties();
54 props.put(BOOTSTRAP_SERVERS_CONFIG, properties.bootstrapServers);
55 props.put(KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
56 props.put(VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
57 props.put(ENABLE_IDEMPOTENCE_CONFIG, true);
59 this.producer = new KafkaProducer<>(props);
60 this.topic = properties.topic;
62 props = new Properties();
63 props.put(BOOTSTRAP_SERVERS_CONFIG, properties.bootstrapServers);
64 props.put(GROUP_ID_CONFIG, "outbox");
65 props.put(KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
66 props.put(VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
68 KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
69 consumer.subscribe(Arrays.asList(this.topic));
70 List<PartitionInfo> partitions = consumer.listTopics().get(this.topic);
71 Set<TopicPartition> assignment = new HashSet<>();
72 for (PartitionInfo info : partitions)
74 LOG.debug("Found {}/{} (ISR: {})", info.topic(), info.partition(), info.inSyncReplicas());
75 assignment.add(new TopicPartition(info.topic(), info.partition()));
78 LOG.info("Using topic {} with {} partitions", topic, partitions);
80 long[] currentOffsets = new long[partitions.size()];
81 for (Map.Entry<TopicPartition, OffsetAndMetadata> entry : consumer.committed(assignment).entrySet())
83 LOG.info("Found current offset {} for partition {}", entry.getValue(), entry.getKey());
84 currentOffsets[entry.getKey().partition()] = entry.getValue().offset() - 1l;
86 LOG.info("Current offsets: {}", currentOffsets);
88 long[] endOffsets = new long[partitions.size()];
89 for (Map.Entry<TopicPartition, Long> entry : consumer.endOffsets(assignment).entrySet())
91 LOG.info("Found next offset {} for partition {}", entry.getValue(), entry.getKey());
92 endOffsets[entry.getKey().partition()] = entry.getValue() - 1l;
94 LOG.info("End-offsets: {}", endOffsets);
97 while(!Arrays.equals(currentOffsets, endOffsets))
99 ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1));
100 LOG.debug("Fetched {} records", records.count());
101 records.forEach(record ->
103 long recordSequenceNumber = Longs.fromByteArray(record.headers().lastHeader(HEADER).value());
105 "Found message #{} on offset {} of partition {}",
106 recordSequenceNumber,
109 send.add(recordSequenceNumber);
110 currentOffsets[record.partition()] = record.offset();
112 deleted += cleanUp();
113 LOG.debug("Current offsets: {}", currentOffsets);
116 LOG.info("Cleaned up {} already send entries from outbox table", deleted);
121 this.cleanupInterval = properties.cleanupInterval;
122 this.nextCleanup = LocalTime.now(clock).plus(this.cleanupInterval);
125 @Scheduled(fixedDelayString = "${de.juplo.kafka.outbox.interval}")
128 List<OutboxItem> items;
131 items = repository.fetch(sequenceNumber);
132 LOG.debug("Polled {} new items", items.size());
133 for (OutboxItem item : items)
135 if (nextCleanup.isBefore(LocalTime.now(clock)))
138 nextCleanup = LocalTime.now(clock).plus(cleanupInterval);
139 LOG.debug("Next clean-up: {}", nextCleanup);
142 while (items.size() > 0);
147 int deleted = repository.delete(send);
148 LOG.debug("Cleaned up {}/{} entries from outbox, next clean-up: {}", deleted, send.size());
153 void send(OutboxItem item)
155 final ProducerRecord<String, String> record =
156 new ProducerRecord<>(topic, item.getKey(), item.getValue());
158 sequenceNumber = item.getSequenceNumber();
159 record.headers().add(HEADER, Longs.toByteArray(sequenceNumber));
161 producer.send(record, (metadata, e) ->
163 if (metadata != null)
165 send.add(item.getSequenceNumber());
167 "{}/{}:{} - {}:{}={}",
169 metadata.partition(),
171 item.getSequenceNumber(),
179 "{}/{} - {}:{}={} -> ",
182 item.getSequenceNumber(),
194 producer.close(Duration.ofSeconds(5));