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());
-
- Producer<String, String> producer = new KafkaProducer<>(props);
- // end::create[]
+ // TODO: Konfiguration für Producer zusammenstellen
this.id = clientId;
this.topic = topic;
- this.producer = producer;
+ this.producer = null; // TODO: Eine Instanz von KafkaProducer erzeugen
}
public void run()
{
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()
- );
- }
- });
+ final ProducerRecord<String, String> record = null; // TODO:
+ // Instanz von ProducerRecord mit key & value aus den Parametern
+ // der Methode erzeugen und diesen an das Topic aus dem Attribut
+ // this.topic versenden.
+ // Dabei den Callback für die Ausgabe von Erfolg/Fehlern nutzen.
+ // Für ganz Schnelle: Versanddauer im Callback messen und ausgeben.
long now = System.currentTimeMillis();
log.trace(