{
private final String id;
private final String topic;
- private final KafkaProducer<String, ClientMessage> producer;
+ private final KafkaProducer<String, Object> producer;
private long produced = 0;
props.put("compression.type", properties.getCompressionType());
props.put("key.serializer", StringSerializer.class.getName());
props.put("value.serializer", JsonSerializer.class.getName());
- props.put(JsonSerializer.TYPE_MAPPINGS, "message:" + ClientMessage.class.getName());
+ props.put(JsonSerializer.TYPE_MAPPINGS,
+ "message:" + ClientMessage.class.getName() + "," +
+ "greeting:" + Greeting.class.getName());
this.producer = new KafkaProducer<>(props);
}
@PostMapping(path = "{key}")
- public DeferredResult<ProduceResult> send(
+ public DeferredResult<ProduceResult> message(
@PathVariable String key,
@RequestBody String value)
{
- DeferredResult<ProduceResult> result = new DeferredResult<>();
-
- final long time = System.currentTimeMillis();
-
- final ProducerRecord<String, ClientMessage> record = new ProducerRecord<>(
+ final ProducerRecord<String, Object> record = new ProducerRecord<>(
topic, // Topic
key, // Key
new ClientMessage(key, value) // Value
);
+ return send(record);
+ }
+
+ @PostMapping(path = "/")
+ public DeferredResult<ProduceResult> greeting(
+ @RequestBody String name)
+ {
+ final ProducerRecord<String, Object> record = new ProducerRecord<>(
+ topic, // Topic
+ name, // Key
+ new Greeting(name) // Value
+ );
+
+ return send(record);
+ }
+
+ private DeferredResult<ProduceResult> send(ProducerRecord<String, Object> record)
+ {
+ DeferredResult<ProduceResult> result = new DeferredResult<>();
+
+ final long time = System.currentTimeMillis();
+
producer.send(record, (metadata, e) ->
{
long now = System.currentTimeMillis();
long now = System.currentTimeMillis();
log.trace(
- "{} - Queued #{} key={} latency={}ms",
+ "{} - Queued key={} latency={}ms",
id,
- value,
record.key(),
now - time
);