Skip to content
Merged
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 @@ -28,6 +28,33 @@
public class KafkaEventServiceImpl implements KafkaEventService {

private static final Logger LOGGER = LoggerFactory.getLogger(KafkaEventServiceImpl.class);
private static KafkaProducer kafkaProducer;

private static synchronized <V> KafkaProducer<String, V> getKafkaProducer(
KafkaProperties kafkaProperties) throws Exception {
if (kafkaProducer == null) {
Properties producerConfig = new Properties();
producerConfig.put(
ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProperties.getBootstrapServers());
producerConfig.put(ProducerConfig.ACKS_CONFIG, "all");
producerConfig.put(
ProducerConfig.CLIENT_ID_CONFIG, InetAddress.getLocalHost().getHostName());
producerConfig.put(
ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

KafkaJsonSerializer<V> jsonSerializer = new KafkaJsonSerializer<>();
try {
KafkaProducer<String, V> producer =
new KafkaProducer<>(producerConfig, new StringSerializer(), jsonSerializer);
return producer;
} catch (Exception e) {
LOGGER.error("Failed to create producer.", e);
throw e;
}
}

return kafkaProducer;
}

@Autowired private final KafkaProperties kafkaProperties;

Expand All @@ -37,42 +64,20 @@ public KafkaEventServiceImpl(KafkaProperties kafkaProperties) {

@Override
public <V> void produce(String key, V message, String topic) throws Exception {
KafkaProducer<String, V> producer = getKafkaProducer(kafkaProperties);
final ProducerRecord<String, V> record = new ProducerRecord<>(topic, key, message);

Properties producerConfig = new Properties();
producerConfig.put(
ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProperties.getBootstrapServers());
producerConfig.put(ProducerConfig.ACKS_CONFIG, "all");
producerConfig.put(
ProducerConfig.CLIENT_ID_CONFIG, InetAddress.getLocalHost().getHostName());
producerConfig.put(
ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

KafkaJsonSerializer<V> jsonSerializer = new KafkaJsonSerializer<>();

try (KafkaProducer<String, V> producer =
new KafkaProducer<>(producerConfig, new StringSerializer(), jsonSerializer)) {

final ProducerRecord<String, V> record = new ProducerRecord<>(topic, key, message);

producer.send(
record,
(metadata, exception) -> {
if (exception != null) {
LOGGER.error(
"Failed to send message to Kafka topic: {}", topic, exception);
} else {
LOGGER.info(
"Message sent to topic: {} with offset: {}",
topic,
metadata.offset());
}
});

producer.flush();

} catch (Exception e) {
LOGGER.error("Failed to create producer.", e);
throw e;
}
producer.send(
record,
(metadata, exception) -> {
if (exception != null) {
LOGGER.error("Failed to send message to Kafka topic: {}", topic, exception);
} else {
LOGGER.info(
"Message sent to topic: {} with offset: {}",
topic,
metadata.offset());
}
});
}
}
Loading