pxchek / kafka-streams

Code implementation of various event streaming scenarios running on a cluster.
0 stars 0 forks source link

ClassCastException - Unable to send message to Consumer #2

Closed pxchek closed 2 years ago

pxchek commented 2 years ago
Exception in thread "main" org.apache.kafka.common.errors.SerializationException: Can't convert key of class com.example.trace.Alert to class org.apache.kafka.common.serialization.StringSerializer specified in key.serializer
Caused by: java.lang.ClassCastException: class com.example.trace.Alert cannot be cast to class java.lang.String (com.example.trace.Alert is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
    at org.apache.kafka.common.serialization.StringSerializer.serialize(StringSerializer.java:28)
    at org.apache.kafka.common.serialization.Serializer.serialize(Serializer.java:62)
    at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:918)
    at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:886)
    at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:774)
    at com.example.productreviews.KafkaJsonReviewDataProducerWorker.main(KafkaJsonReviewDataProducerWorker.java:40)
pxchek commented 2 years ago

kafkaProps.put("key.serializer", "com.example.serializers.AlertSerializer");

The correct serializer was not configured as the Key on both producer and consumer end. It was looking for String serializer, updating to the above AlertSerializer resolved the issue.