I have spring program for read avro messages from Kafka topic. When message reading occur exception:
org.springframework.messaging.converter.MessageConversionException: Cannot convert from [com.mycompany.myapp.domain.avro.GreetingRecord] to [com.mycompany.myapp.domain.avro.GreetingRecord]
In debug i see that ClassUtils.isAssignable return false for same class:
Class <?> targetClass = parameter.getParameterType();
Class <?> payloadClass = payload.getClass();
if (ClassUtils.isAssignable(targetClass, payloadClass)) { // return false for same class
this.validate(message, parameter, payload);
return payload;
} else {
if (this.converter instanceof SmartMessageConverter) {
SmartMessageConverter smartConverter = (SmartMessageConverter)this.converter;
payload = smartConverter.fromMessage(message, targetClass, parameter);
} else {
payload = this.converter.fromMessage(message, targetClass);
}
}
I found that classloaders in targetClass and payloadClass different (RestartClassLoader and Launcher$AppClassLoader)
Target (payload) Class definition:
/**
* Autogenerated by Avro
*
* DO NOT EDIT DIRECTLY
*/
package com.mycompany.myapp.domain.avro;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.message.BinaryMessageEncoder;
import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.message.SchemaStore;
@SuppressWarnings("all")
@org.apache.avro.specific.AvroGenerated
public class GreetingRecord extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
private static final long serialVersionUID = -39777975679985249L;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"GreetingRecord\",\"namespace\":\"com.mycompany.myapp.domain.avro\",\"fields\":[{\"name\":\"message\",\"type\":\"string\"}]}");
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
private static SpecificData MODEL$ = new SpecificData();
private static final BinaryMessageEncoder<GreetingRecord> ENCODER =
new BinaryMessageEncoder<GreetingRecord>(MODEL$, SCHEMA$);
private static final BinaryMessageDecoder<GreetingRecord> DECODER =
new BinaryMessageDecoder<GreetingRecord>(MODEL$, SCHEMA$);
/**
* Return the BinaryMessageDecoder instance used by this class.
*/
public static BinaryMessageDecoder<GreetingRecord> getDecoder() {
return DECODER;
}
/**
* Create a new BinaryMessageDecoder instance for this class that uses the specified {@link SchemaStore}.
* @param resolver a {@link SchemaStore} used to find schemas by fingerprint
*/
public static BinaryMessageDecoder<GreetingRecord> createDecoder(SchemaStore resolver) {
return new BinaryMessageDecoder<GreetingRecord>(MODEL$, SCHEMA$, resolver);
}
/** Serializes this GreetingRecord to a ByteBuffer. */
public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
return ENCODER.encode(this);
}
/** Deserializes a GreetingRecord from a ByteBuffer. */
public static GreetingRecord fromByteBuffer(
java.nio.ByteBuffer b) throws java.io.IOException {
return DECODER.decode(b);
}
@Deprecated public java.lang.CharSequence message;
/**
* Default constructor. Note that this does not initialize fields
* to their default values from the schema. If that is desired then
* one should use <code>newBuilder()</code>.
*/
public GreetingRecord() {}
/**
* All-args constructor.
* @param message The new value for message
*/
public GreetingRecord(java.lang.CharSequence message) {
this.message = message;
}
public org.apache.avro.Schema getSchema() { return SCHEMA$; }
// Used by DatumWriter. Applications should not call.
public java.lang.Object get(int field$) {
switch (field$) {
case 0: return message;
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
// Used by DatumReader. Applications should not call.
@SuppressWarnings(value="unchecked")
public void put(int field$, java.lang.Object value$) {
switch (field$) {
case 0: message = (java.lang.CharSequence)value$; break;
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
/**
* Gets the value of the 'message' field.
* @return The value of the 'message' field.
*/
public java.lang.CharSequence getMessage() {
return message;
}
/**
* Sets the value of the 'message' field.
* @param value the value to set.
*/
public void setMessage(java.lang.CharSequence value) {
this.message = value;
}
/**
* Creates a new GreetingRecord RecordBuilder.
* @return A new GreetingRecord RecordBuilder
*/
public static GreetingRecord.Builder newBuilder() {
return new GreetingRecord.Builder();
}
/**
* Creates a new GreetingRecord RecordBuilder by copying an existing Builder.
* @param other The existing builder to copy.
* @return A new GreetingRecord RecordBuilder
*/
public static GreetingRecord.Builder newBuilder(GreetingRecord.Builder other) {
return new GreetingRecord.Builder(other);
}
/**
* Creates a new GreetingRecord RecordBuilder by copying an existing GreetingRecord instance.
* @param other The existing instance to copy.
* @return A new GreetingRecord RecordBuilder
*/
public static GreetingRecord.Builder newBuilder(GreetingRecord other) {
return new GreetingRecord.Builder(other);
}
/**
* RecordBuilder for GreetingRecord instances.
*/
public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<GreetingRecord>
implements org.apache.avro.data.RecordBuilder<GreetingRecord> {
private java.lang.CharSequence message;
/** Creates a new Builder */
private Builder() {
super(SCHEMA$);
}
/**
* Creates a Builder by copying an existing Builder.
* @param other The existing Builder to copy.
*/
private Builder(GreetingRecord.Builder other) {
super(other);
if (isValidValue(fields()[0], other.message)) {
this.message = data().deepCopy(fields()[0].schema(), other.message);
fieldSetFlags()[0] = true;
}
}
/**
* Creates a Builder by copying an existing GreetingRecord instance
* @param other The existing instance to copy.
*/
private Builder(GreetingRecord other) {
super(SCHEMA$);
if (isValidValue(fields()[0], other.message)) {
this.message = data().deepCopy(fields()[0].schema(), other.message);
fieldSetFlags()[0] = true;
}
}
/**
* Gets the value of the 'message' field.
* @return The value.
*/
public java.lang.CharSequence getMessage() {
return message;
}
/**
* Sets the value of the 'message' field.
* @param value The value of 'message'.
* @return This builder.
*/
public GreetingRecord.Builder setMessage(java.lang.CharSequence value) {
validate(fields()[0], value);
this.message = value;
fieldSetFlags()[0] = true;
return this;
}
/**
* Checks whether the 'message' field has been set.
* @return True if the 'message' field has been set, false otherwise.
*/
public boolean hasMessage() {
return fieldSetFlags()[0];
}
/**
* Clears the value of the 'message' field.
* @return This builder.
*/
public GreetingRecord.Builder clearMessage() {
message = null;
fieldSetFlags()[0] = false;
return this;
}
@Override
@SuppressWarnings("unchecked")
public GreetingRecord build() {
try {
GreetingRecord record = new GreetingRecord();
record.message = fieldSetFlags()[0] ? this.message : (java.lang.CharSequence) defaultValue(fields()[0]);
return record;
} catch (java.lang.Exception e) {
throw new org.apache.avro.AvroRuntimeException(e);
}
}
}
@SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumWriter<GreetingRecord>
WRITER$ = (org.apache.avro.io.DatumWriter<GreetingRecord>)MODEL$.createDatumWriter(SCHEMA$);
@Override public void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException {
WRITER$.write(this, SpecificData.getEncoder(out));
}
@SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumReader<GreetingRecord>
READER$ = (org.apache.avro.io.DatumReader<GreetingRecord>)MODEL$.createDatumReader(SCHEMA$);
@Override public void readExternal(java.io.ObjectInput in)
throws java.io.IOException {
READER$.read(this, SpecificData.getDecoder(in));
}
}
How solve problem with different classloader?
I find workaround here: @AuthenticationPrincipal with Spring Boot not working
public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(MyApplication.class, args);
}
Turn off spring.devtools.restart solve issue:
I have a solution. As long as you use Spring, I guess you probably use Spring Devtools.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
Remove that dependency which causes the troubles:
org.springframework.kafka.listener.ListenerExecutionFailedException: Listener method 'public void org.company.MyKafkaConsumer.consume(org.apache.kafka.clients.consumer.ConsumerRecord<java.lang.String, org.mycompany.avro.MyAvroObject>,org.springframework.kafka.support.Acknowledgment)' threw exception; nested exception is java.lang.ClassCastException: class org.mycompany.avro.MyAvroObject cannot be cast to class org.mycompany.avro.MyAvroObject (org.mycompany.avro.MyAvroObject is in unnamed module of loader 'app'; org.mycompany.avro.MyAvroObject is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @3a9df291); nested exception is java.lang.ClassCastException: class org.mycompany.avro.MyAvroObject cannot be cast to class org.mycompany.avro.MyAvroObject (org.mycompany.avro.MyAvroObject is in unnamed module of loader 'app'; org.mycompany.avro.MyAvroObject is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @3a9df291)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.decorateException(KafkaMessageListenerContainer.java:1899) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeErrorHandler(KafkaMessageListenerContainer.java:1887) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeRecordListener(KafkaMessageListenerContainer.java:1792) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeWithRecords(KafkaMessageListenerContainer.java:1719) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeRecordListener(KafkaMessageListenerContainer.java:1617) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeListener(KafkaMessageListenerContainer.java:1348) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.pollAndInvoke(KafkaMessageListenerContainer.java:1064) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.run(KafkaMessageListenerContainer.java:972) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: java.lang.ClassCastException: class org.mycompany.avro.MyAvroObject cannot be cast to class org.mycompany.avro.MyAvroObject (org.mycompany.avro.MyAvroObject is in unnamed module of loader 'app'; org.mycompany.avro.MyAvroObject is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @3a9df291)
at org.company.MyKafkaConsumer.consume(NiaKafkaConsumer.java:33) ~[classes/:na]
at jdk.internal.reflect.GeneratedMethodAccessor81.invoke(Unknown Source) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:171) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:120) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.kafka.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:48) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:334) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:86) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:51) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeOnMessage(KafkaMessageListenerContainer.java:1854) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeOnMessage(KafkaMessageListenerContainer.java:1836) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeRecordListener(KafkaMessageListenerContainer.java:1779) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
... 8 common frames omitted
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With