1 package de.juplo.kafka;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.producer.KafkaProducer;
5 import org.apache.kafka.clients.producer.ProducerRecord;
6 import org.apache.kafka.common.serialization.StringSerializer;
7 import org.springframework.http.HttpStatus;
8 import org.springframework.web.bind.annotation.*;
9 import org.springframework.web.context.request.async.DeferredResult;
11 import javax.annotation.PreDestroy;
12 import java.math.BigInteger;
13 import java.util.Properties;
14 import java.util.concurrent.ExecutionException;
19 public class RestProducer
21 private final String id;
22 private final String topic;
23 private final Integer partition;
24 private final KafkaProducer<String, String> producer;
26 private long produced = 0;
28 public RestProducer(ApplicationProperties properties)
30 this.id = properties.getClientId();
31 this.topic = properties.getTopic();
32 this.partition = properties.getPartition();
34 Properties props = new Properties();
35 props.put("bootstrap.servers", properties.getBootstrapServer());
36 props.put("client.id", properties.getClientId());
37 props.put("acks", properties.getAcks());
38 props.put("batch.size", properties.getBatchSize());
39 props.put("delivery.timeout.ms", 20000); // 20 Sekunden
40 props.put("request.timeout.ms", 10000); // 10 Sekunden
41 props.put("linger.ms", properties.getLingerMs());
42 props.put("compression.type", properties.getCompressionType());
43 props.put("key.serializer", StringSerializer.class.getName());
44 props.put("value.serializer", StringSerializer.class.getName());
46 this.producer = new KafkaProducer<>(props);
49 @PostMapping(path = "{key}")
50 public DeferredResult<ProduceResult> send(
51 @PathVariable String key,
52 @RequestHeader(name = "X-id", required = false) Long correlationId,
53 @RequestBody String value)
55 DeferredResult<ProduceResult> result = new DeferredResult<>();
57 final long time = System.currentTimeMillis();
59 final ProducerRecord<String, String> record = new ProducerRecord<>(
61 partition, // Partition
66 record.headers().add("source", id.getBytes());
67 if (correlationId != null)
69 record.headers().add("id", BigInteger.valueOf(correlationId).toByteArray());
72 producer.send(record, (metadata, e) ->
74 long now = System.currentTimeMillis();
79 result.setResult(new ProduceSuccess(metadata.partition(), metadata.offset()));
81 "{} - Sent key={} message={} partition={}/{} timestamp={} latency={}ms",
94 result.setErrorResult(new ProduceFailure(e));
96 "{} - ERROR key={} timestamp={} latency={}ms: {}",
99 metadata == null ? -1 : metadata.timestamp(),
106 long now = System.currentTimeMillis();
108 "{} - Queued #{} key={} latency={}ms",
119 @ResponseStatus(HttpStatus.BAD_REQUEST)
120 public ErrorResponse illegalStateException(IllegalStateException e)
122 return new ErrorResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value());
126 public void destroy() throws ExecutionException, InterruptedException
128 log.info("{} - Destroy!", id);
129 log.info("{} - Closing the KafkaProducer", id);
131 log.info("{}: Produced {} messages in total, exiting!", id, produced);