X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FRestProducer.java;h=b430e35f99911af6668d2d64f6155e710e674001;hb=0d99ee9ce803ea239c6d7746a87eb52ccef99f26;hp=bfea3cadc4b71271ab8b11b8c369ae2ff7e2a069;hpb=c3668d0d15f9c91fa3dcdd75fde5019363c9caff;p=demos%2Fkafka%2Ftraining diff --git a/src/main/java/de/juplo/kafka/RestProducer.java b/src/main/java/de/juplo/kafka/RestProducer.java index bfea3ca..b430e35 100644 --- a/src/main/java/de/juplo/kafka/RestProducer.java +++ b/src/main/java/de/juplo/kafka/RestProducer.java @@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.context.request.async.DeferredResult; import javax.annotation.PreDestroy; +import java.math.BigInteger; import java.util.Properties; import java.util.concurrent.ExecutionException; @@ -19,6 +20,7 @@ public class RestProducer { private final String id; private final String topic; + private final Integer partition; private final KafkaProducer producer; private long produced = 0; @@ -27,6 +29,7 @@ public class RestProducer { this.id = properties.getClientId(); this.topic = properties.getTopic(); + this.partition = properties.getPartition(); Properties props = new Properties(); props.put("bootstrap.servers", properties.getBootstrapServer()); @@ -55,10 +58,17 @@ public class RestProducer final ProducerRecord record = new ProducerRecord<>( topic, // Topic + partition, // Partition key, // Key value // Value ); + record.headers().add("source", id.getBytes()); + if (correlationId != null) + { + record.headers().add("id", BigInteger.valueOf(correlationId).toByteArray()); + } + producer.send(record, (metadata, e) -> { long now = System.currentTimeMillis(); @@ -95,9 +105,8 @@ public class RestProducer long now = System.currentTimeMillis(); log.trace( - "{} - Queued #{} key={} latency={}ms", + "{} - Queued message with key={} latency={}ms", id, - value, record.key(), now - time );