@Slf4j
public class SimpleProducer
{
- public static void main(String[] args) throws Exception
+ private final String id;
+ private final String topic;
+ private final KafkaProducer<String, String> producer;
+
+ private long produced = 0;
+ private volatile boolean running = true;
+ private volatile boolean done = false;
+
+ public SimpleProducer(String broker, String topic, String clientId)
{
- // tag::create[]
Properties props = new Properties();
- props.put("bootstrap.servers", "localhost:9092");
+ props.put("bootstrap.servers", broker);
+ props.put("client.id", clientId); // Nur zur Wiedererkennung
props.put("key.serializer", StringSerializer.class.getName());
props.put("value.serializer", StringSerializer.class.getName());
- KafkaProducer<String, String> producer = new KafkaProducer<>(props);
- // end::create[]
+ producer = new KafkaProducer<>(props);
- String id = "P";
+ this.topic = topic;
+ this.id = clientId;
+ }
+
+ public void run()
+ {
long i = 0;
try
{
- for (; i < 100 ; i++)
+ for (; running ; i++)
{
- final long time = System.currentTimeMillis();
+ send(Long.toString(i%10), Long.toString(i));
+ Thread.sleep(500);
+ }
+ }
+ catch (InterruptedException e) {}
+ finally
+ {
+ log.info("{}: Closing the KafkaProducer", id);
+ producer.close();
+ log.info("{}: Produced {} messages in total, exiting!", id, produced);
+ done = true;
+ }
+ }
- final ProducerRecord<String, String> record = new ProducerRecord<>(
- "test", // Topic
- Long.toString(i%10), // Key
- Long.toString(i) // Value
- );
+ void send(String key, String value)
+ {
+ final long time = System.currentTimeMillis();
- producer.send(record, (metadata, e) ->
- {
- long now = System.currentTimeMillis();
- if (e == null)
- {
- // HANDLE SUCCESS
- log.debug(
- "{} - Sent key={} message={} partition={}/{} timestamp={} latency={}ms",
- id,
- record.key(),
- record.value(),
- metadata.partition(),
- metadata.offset(),
- metadata.timestamp(),
- now - time
- );
- }
- else
- {
- // HANDLE ERROR
- log.error(
- "{} - ERROR key={} timestamp={} latency={}ms: {}",
- id,
- record.key(),
- metadata == null ? -1 : metadata.timestamp(),
- now - time,
- e.toString()
- );
- }
- });
-
- long now = System.currentTimeMillis();
- log.trace(
- "{} - Queued #{} key={} latency={}ms",
+ final ProducerRecord<String, String> record = new ProducerRecord<>(
+ topic, // Topic
+ key, // Key
+ value // Value
+ );
+
+ producer.send(record, (metadata, e) ->
+ {
+ long now = System.currentTimeMillis();
+ if (e == null)
+ {
+ // HANDLE SUCCESS
+ produced++;
+ log.debug(
+ "{} - Sent key={} message={} partition={}/{} timestamp={} latency={}ms",
id,
- i,
record.key(),
+ record.value(),
+ metadata.partition(),
+ metadata.offset(),
+ metadata.timestamp(),
now - time
);
}
- }
- finally
+ else
+ {
+ // HANDLE ERROR
+ log.error(
+ "{} - ERROR key={} timestamp={} latency={}ms: {}",
+ id,
+ record.key(),
+ metadata == null ? -1 : metadata.timestamp(),
+ now - time,
+ e.toString()
+ );
+ }
+ });
+
+ long now = System.currentTimeMillis();
+ log.trace(
+ "{} - Queued #{} key={} latency={}ms",
+ id,
+ value,
+ record.key(),
+ now - time
+ );
+ }
+
+
+ public static void main(String[] args) throws Exception
+ {
+ String broker = ":9092";
+ String topic = "test";
+ String clientId = "DEV";
+
+ switch (args.length)
{
- log.info("{}: Closing the KafkaProducer", id);
- producer.close();
- log.info("{}: Exiting!", id);
+ case 3:
+ clientId = args[2];
+ case 2:
+ topic = args[1];
+ case 1:
+ broker = args[0];
}
+
+ SimpleProducer instance = new SimpleProducer(broker, topic, clientId);
+
+ Runtime.getRuntime().addShutdownHook(new Thread(() ->
+ {
+ instance.running = false;
+ while (!instance.done)
+ {
+ log.info("Waiting for main-thread...");
+ try
+ {
+ Thread.sleep(1000);
+ }
+ catch (InterruptedException e) {}
+ }
+ log.info("Shutdown completed.");
+ }));
+
+ log.info(
+ "Running SimpleProducer: broker={}, topic={}, client-id={}",
+ broker,
+ topic,
+ clientId);
+ instance.run();
}
}