Skip to content

Commit bcc5251

Browse files
authored
Merge branch 'master' into feature/TFP-5750
2 parents b3a8081 + 27aa641 commit bcc5251

File tree

1 file changed

+18
-1
lines changed
  • integrasjon/kafka-properties/src/main/java/no/nav/vedtak/felles/integrasjon/kafka

1 file changed

+18
-1
lines changed

integrasjon/kafka-properties/src/main/java/no/nav/vedtak/felles/integrasjon/kafka/KafkaSender.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package no.nav.vedtak.felles.integrasjon.kafka;
22

3+
import java.util.Optional;
4+
35
import org.apache.kafka.clients.producer.KafkaProducer;
46
import org.apache.kafka.clients.producer.Producer;
57
import org.apache.kafka.clients.producer.ProducerRecord;
68
import org.apache.kafka.clients.producer.RecordMetadata;
9+
import org.apache.kafka.common.header.internals.RecordHeader;
710

811
import no.nav.vedtak.exception.IntegrasjonException;
912

@@ -21,16 +24,30 @@ public String getTopicName() {
2124
return topicName;
2225
}
2326

27+
public record KafkaHeader(String key, byte[] value) {}
28+
2429
public RecordMetadata send(String key, String message) {
2530
if (topicName == null) {
2631
throw kafkaPubliseringException("null", new IllegalArgumentException());
2732
}
28-
return send(key, message, this.topicName);
33+
return send(null, key, message, topicName);
34+
}
35+
36+
public RecordMetadata send(KafkaHeader header, String key, String message) {
37+
if (topicName == null) {
38+
throw kafkaPubliseringException("null", new IllegalArgumentException());
39+
}
40+
return send(header, key, message, this.topicName);
2941
}
3042

3143
public RecordMetadata send(String key, String message, String topic) {
44+
return send(null, key, message, topic);
45+
}
46+
47+
public RecordMetadata send(KafkaHeader header, String key, String message, String topic) {
3248
try {
3349
var record = new ProducerRecord<>(topic, key, message);
50+
Optional.ofNullable(header).ifPresent(h -> record.headers().add(new RecordHeader(h.key(), h.value())));
3451
return producer.send(record).get();
3552
} catch (Exception e) {
3653
if (e instanceof InterruptedException) {

0 commit comments

Comments
 (0)