Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legg til mulighet for kafka-header til statistikk #1359

Merged
merged 1 commit into from
Apr 12, 2024
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
@@ -1,9 +1,12 @@
package no.nav.vedtak.felles.integrasjon.kafka;

import java.util.Optional;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.header.internals.RecordHeader;

import no.nav.vedtak.exception.IntegrasjonException;

Expand All @@ -21,16 +24,30 @@ public String getTopicName() {
return topicName;
}

public record KafkaHeader(String key, byte[] value) {}

public RecordMetadata send(String key, String message) {
if (topicName == null) {
throw kafkaPubliseringException("null", new IllegalArgumentException());
}
return send(key, message, this.topicName);
return send(null, key, message, topicName);
}

public RecordMetadata send(KafkaHeader header, String key, String message) {
jolarsen marked this conversation as resolved.
Show resolved Hide resolved
if (topicName == null) {
throw kafkaPubliseringException("null", new IllegalArgumentException());
}
return send(header, key, message, this.topicName);
}

public RecordMetadata send(String key, String message, String topic) {
return send(null, key, message, topic);
}

public RecordMetadata send(KafkaHeader header, String key, String message, String topic) {
try {
var record = new ProducerRecord<>(topic, key, message);
Optional.ofNullable(header).ifPresent(h -> record.headers().add(new RecordHeader(h.key(), h.value())));
return producer.send(record).get();
} catch (Exception e) {
if (e instanceof InterruptedException) {
Expand Down