0

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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Nesan Mano
  • 1,892
  • 2
  • 26
  • 43
  • [This discussion](https://github.com/cloudevents/sdk-java/issues/196) may have something useful for you. – ryyker Oct 11 '22 at 18:10
  • Please share your Spring properties where you've set `value.deserializer` to something other than StringDeserializer – OneCricketeer Oct 11 '22 at 19:18
  • @ryyker That [shouldn't be necessary with Kafka](https://github.com/cloudevents/sdk-java/blob/master/kafka/src/main/java/io/cloudevents/kafka/CloudEventMessageDeserializer.java) – OneCricketeer Oct 11 '22 at 19:19
  • @OneCricketeer - LOL, Mine was a complete shot in the dark. But thanks for linking the small example code. Kafka looks powerful. – ryyker Oct 11 '22 at 20:50
  • @ryyker It's not example code. Kafka always stores bytes. Spring-Kafka library defaults to consume as Strings, I believe, so that needs overwritten with the linked class. – OneCricketeer Oct 11 '22 at 20:51
  • Hi guys, thank you all, Found that I did not use the right deserializer. I does not figure in the code above. – Nesan Mano Oct 12 '22 at 13:04

0 Answers0