问题
My Kafka consumer throws an exception when trying to process messages in a batch(i.e process list of messages)
Error Message
is java.lang.ClassCastException: class kafka.psmessage.PMessage cannot be cast to class org.apache.kafka.clients.consumer.ConsumerRecord (kafka.psmessage.pMessage and org.apache.kafka.clients.consumer.ConsumerRecord are in unnamed module of loader 'app'); nested exception is java.lang.ClassCastException: class kafka.psmessage.PMessage cannot be cast to class org.apache.kafka.clients.consumer.ConsumerRecord (kafka.psmessage.PMessage and org.apache.kafka.clients.consumer.ConsumerRecord are in unnamed module of loader 'app')
Code snippet
public void receive(List<ConsumerRecord<String, PMessage>> records) {
List<PMessage> msgList = records.stream().map(message -> message.value()).collect(Collectors.toList());
PMessage test = records.get(0).value();
ConsumerRecord<String, PMessage> firstMessage = records.get(0);
All 3 statements above giving ClassCastException
@Override
protected DefaultKafkaConsumerFactory<String, PMessage> createConsumerFactory(String groupId) {
Map<String, Object> props = new HashMap<>();
props.putAll(kafkaProperties.buildConsumerProperties());
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, serviceInfo.getBrokersAuthUrl());
props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 10000);
props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
props.putAll(KafkaSaslUtils.getSaslProperties(serviceInfo));
return new DefaultKafkaConsumerFactory<>(props,
new StringDeserializer(),
new MessageDeserializer());
}
MessageDeserializer:
@Override
public PMessage deserialize(String topic, byte[] data) {
if (data == null) {
throw new SerializationException("Can not deserialize. data is null for topic: '" + topic + "'");
}
try {
SeekableByteArrayInput seekableByteArrayInput = new SeekableByteArrayInput(data);
GenericDatumReader reader = new GenericDatumReader<GenericRecord>();
DataFileReader<GenericRecord> dataFileReader = new
DataFileReader(seekableByteArrayInput, reader);
GenericRecord genericRecord = extractGenericRecord(dataFileReader);
Any pointer will be appreciated. Thanks!
来源:https://stackoverflow.com/questions/59869681/kafka-consumer-classcastexception-java