Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import java.util.Map;

import org.apache.kafka.clients.producer.ProducerConfig;

import org.eventhub.infrastructure.messaging.TypeIdInterceptor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -47,4 +46,9 @@ public ProducerFactory<String, Object> producerFactory(KafkaProperties kafkaProp
public KafkaTemplate<String, Object> kafkaTemplate(KafkaProperties kafkaProperties) {
return new KafkaTemplate<>(producerFactory(kafkaProperties));
}

@Bean
public PartitionFinder finder(@Qualifier("producerFactory") ProducerFactory<String, Object> producerFactory) {
return new PartitionFinder(producerFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.eventhub.config;

import org.apache.kafka.clients.producer.Producer;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class PartitionFinder {

private final ProducerFactory<String, Object> producerFactory;

public String[] partitions(String topic) {
try (Producer<String, Object> producer = producerFactory.createProducer()) {
return producer.partitionsFor(topic).stream()
.map(pi -> "" + pi.partition())
.toArray(String[]::new);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.eventhub.application.port.CycleInfoEventPublisher;
import org.eventhub.domain.CycleInfoList;
import org.eventhub.infrastructure.messaging.command.CycleInfoListCommand;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.TopicPartition;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
Expand All @@ -19,6 +21,8 @@ public class KafkaCycleInfoEventPublisher implements CycleInfoEventPublisher {

@Override
@Async
@KafkaListener(topicPartitions = @TopicPartition(topic = "cycleInfo-json-topic",
partitions = "#{@finder.partitions('cycleInfo-json-topic')}"))
public void publishEvent(CycleInfoList cycleInfoList) {
CycleInfoListCommand message = CycleInfoListCommand.from(cycleInfoList);

Expand Down