1 package de.juplo.kafka;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.producer.Producer;
5 import org.apache.kafka.clients.producer.KafkaProducer;
6 import org.apache.kafka.clients.producer.ProducerRecord;
7 import org.apache.kafka.common.serialization.StringSerializer;
9 import java.util.Properties;
10 import java.util.concurrent.CompletableFuture;
14 public class SimpleProducer
16 private final String id;
17 private final String topic;
18 private final Producer<String, String> producer;
20 private long produced = 0;
21 private volatile boolean running = true;
22 private volatile boolean done = false;
24 public SimpleProducer(String broker, String topic, String clientId)
26 Properties props = new Properties();
27 props.put("bootstrap.servers", broker);
28 props.put("client.id", clientId); // Nur zur Wiedererkennung
29 props.put("transactional.id", clientId); // Aktiviert außerdem enable.idempotence=true
30 props.put("key.serializer", StringSerializer.class.getName());
31 props.put("value.serializer", StringSerializer.class.getName());
35 producer = new KafkaProducer<>(props);
46 send(Long.toString(i%10), Long.toString(i));
50 catch (InterruptedException e) {}
53 log.info("{}: Closing the KafkaProducer", id);
55 log.info("{}: Produced {} messages in total, exiting!", id, produced);
60 CompletableFuture<Long> send(String key, String value)
62 final CompletableFuture<Long> result = new CompletableFuture<>();
63 final long time = System.currentTimeMillis();
65 final ProducerRecord<String, String> record = new ProducerRecord<>(
71 producer.send(record, (metadata, e) ->
73 long now = System.currentTimeMillis();
79 "{} - Sent key={} message={} partition={}/{} timestamp={} latency={}ms",
88 result.complete(metadata.offset());
94 "{} - ERROR key={} timestamp={} latency={}ms: {}",
97 metadata == null ? -1 : metadata.timestamp(),
101 result.completeExceptionally(e);
105 long now = System.currentTimeMillis();
107 "{} - Queued #{} key={} latency={}ms",
118 public static void main(String[] args) throws Exception
120 String broker = ":9092";
121 String topic = "test";
122 String clientId = "DEV";
134 SimpleProducer instance = new SimpleProducer(broker, topic, clientId);
136 Runtime.getRuntime().addShutdownHook(new Thread(() ->
138 instance.running = false;
139 while (!instance.done)
141 log.info("Waiting for main-thread...");
146 catch (InterruptedException e) {}
148 log.info("Shutdown completed.");
152 "Running SimpleProducer: broker={}, topic={}, client-id={}",