I am new to Kafka and trying to do some testing with a producer/consumer pair.
I have a producer that looks like the below code.
String key = "key1";
String message = "{}"; // has valid json
CloudEventBuilder ceb = new CloudEventBuilder().withId("123").withSource(xxxxxxxx)
.withType("test").withData(message.getBytes());
topicPublisher.publish(key, ceb.build());
On my consumer side, I am trying to retrieve the message.
@KafkaListener(topics = {"quickstart-events"},groupId = "group1")
public void listen(ConsumerRecord<String, CloudEvent> cloudEventRecord) {
eventHandler.processEvent(cloudEventRecord);
System.out.println("received: "+ " key->"+ cloudEventRecord.key() + " value-> "+cloudEventRecord.value().getId());
}
An error is thrown at this line.
cloudEventRecord.value().getId()
This is the reason:
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to io.cloudevents.CloudEvent