@Slf4j
public class SimpleConsumer
{
- private long consumed = 0;
private KafkaConsumer<String, String> consumer;
private Lock lock = new ReentrantLock();
private Condition stopped = lock.newCondition();
public SimpleConsumer()
{
- // tag::create[]
- Properties props = new Properties();
- props.put("bootstrap.servers", ":9092");
- props.put("group.id", "my-consumer"); // << Used for Offset-Commits
- // end::create[]
- props.put("auto.offset.reset", "earliest");
- // tag::create[]
- props.put("key.deserializer", StringDeserializer.class.getName());
- props.put("value.deserializer", StringDeserializer.class.getName());
-
- KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
- // end::create[]
- this.consumer = consumer;
+ this.consumer = null; // TODO: Instanziierung
}
public void run()
{
- String id = "C";
-
try
{
- log.info("{} - Subscribing to topic test", id);
- consumer.subscribe(Arrays.asList("test"));
-
- // tag::loop[]
- while (true)
- {
- ConsumerRecords<String, String> records =
- consumer.poll(Duration.ofSeconds(1));
-
- // Do something with the data...
- // end::loop[]
- log.info("{} - Received {} messages", id, records.count());
- for (ConsumerRecord<String, String> record : records)
- {
- consumed++;
- log.info(
- "{} - {}: {}/{} - {}={}",
- id,
- record.offset(),
- record.topic(),
- record.partition(),
- record.key(),
- record.value()
- );
- }
- // tag::loop[]
- }
- // end::loop[]
- }
- catch(WakeupException e)
- {
- log.info("{} - RIIING!", id);
- }
- catch(Exception e)
- {
- log.error("{} - Unexpected error: {}", id, e.toString());
+ // TODO: Nachrichten konsumieren
}
+ // TODO: Exception-Handling
finally
{
this.lock.lock();
try
{
- log.info("{} - Closing the KafkaConsumer", id);
- consumer.close();
- log.info("C - DONE!");
+ // TODO: Clean-Up
stopped.signal();
}
finally
{
this.lock.unlock();
- log.info("{}: Consumed {} messages in total, exiting!", id, consumed);
}
}
}
instance.lock.lock();
try
{
- instance.consumer.wakeup();
+ // TODO: Consumer beenden
instance.stopped.await();
}
catch (InterruptedException e)
private final String topic;
private final KafkaProducer<String, String> producer;
- private long produced = 0;
-
public SimpleProducer(String clientId, String topic)
{
- // tag::create[]
- Properties props = new Properties();
- props.put("bootstrap.servers", "localhost:9092");
- props.put("key.serializer", StringSerializer.class.getName());
- props.put("value.serializer", StringSerializer.class.getName());
-
- KafkaProducer<String, String> producer = new KafkaProducer<>(props);
- // end::create[]
-
this.id = clientId;
this.topic = topic;
- this.producer = producer;
+ this.producer = null; // TODO: Instanziierung
}
public void run()
{
- long i = 0;
-
- try
- {
- for (; i < 100 ; i++)
- {
- send(Long.toString(i%10), Long.toString(i));
- }
-
- log.info("{} - Done", id);
- }
- finally
- {
- log.info("{}: Closing the KafkaProducer", id);
- producer.close();
- log.info("{}: Produced {} messages in total, exiting!", id, produced);
- }
+ // TODO: Nachrichten versenden
}
- void send(String key, String value)
- {
- final long time = System.currentTimeMillis();
-
- 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,
- 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",
- id,
- value,
- record.key(),
- now - time
- );
- }
public static void main(String[] args) throws Exception