WIP
[demos/kafka/training] / src / main / java / de / juplo / kafka / RestGateway.java
1 package de.juplo.kafka;
2
3 import lombok.RequiredArgsConstructor;
4 import lombok.extern.slf4j.Slf4j;
5 import org.apache.kafka.clients.producer.ProducerRecord;
6 import org.apache.kafka.clients.producer.RecordMetadata;
7 import org.springframework.http.HttpStatus;
8 import org.springframework.kafka.core.KafkaTemplate;
9 import org.springframework.kafka.support.SendResult;
10 import org.springframework.util.concurrent.ListenableFuture;
11 import org.springframework.web.bind.annotation.*;
12 import org.springframework.web.context.request.async.DeferredResult;
13
14
15 @Slf4j
16 @RequestMapping
17 @ResponseBody
18 @RequiredArgsConstructor
19 public class RestGateway
20 {
21   private final String id;
22   private final String topic;
23   private final Integer partition;
24   private final KafkaTemplate<String, Integer> kafkaTemplate;
25
26   private long produced = 0;
27
28   @PostMapping(path = "{key}")
29   public DeferredResult<ProduceResult> send(
30       @PathVariable String key,
31       @RequestHeader(name = "X-id", required = false) Long correlationId,
32       @RequestBody Integer value)
33   {
34     DeferredResult<ProduceResult> result = new DeferredResult<>();
35
36     final long time = System.currentTimeMillis();
37
38     ListenableFuture<SendResult<String, Integer>> future =
39         kafkaTemplate.send(topic, partition, key, value);
40
41     long now = System.currentTimeMillis();
42
43     future.addCallback(
44         sendResult ->
45         {
46           // HANDLE SUCCESS
47           produced++;
48           RecordMetadata metadata = sendResult.getRecordMetadata();
49           ProducerRecord<String, Integer> record = sendResult.getProducerRecord();
50           result.setResult(new ProduceSuccess(metadata.partition(), metadata.offset()));
51           log.debug(
52               "{} - Sent key={} message={} partition={}/{} timestamp={} latency={}ms",
53               id,
54               record.key(),
55               record.value(),
56               metadata.partition(),
57               metadata.offset(),
58               metadata.timestamp(),
59               now - time
60           );
61         },
62         e->
63         {
64           // HANDLE ERROR
65           result.setErrorResult(new ProduceFailure(e));
66           log.error(
67               "{} - ERROR key={} latency={}ms: {}",
68               id,
69               key,
70               now - time,
71               e.toString()
72           );
73         });
74
75     log.trace(
76         "{} - Queued message with key={} latency={}ms",
77         id,
78         key,
79         now - time
80     );
81
82     return result;
83   }
84
85   @ExceptionHandler
86   @ResponseStatus(HttpStatus.BAD_REQUEST)
87   public ErrorResponse illegalStateException(IllegalStateException e)
88   {
89     return new ErrorResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value());
90   }
91 }