spring integration - Generic type handling in Avro encoder/decoder -
i using spring-intergration-kafka
send , receive messages kafka. message object based on generic type. base class looks this
public abstract class abstractmessage<t> implements serializable { ... }
i able send , receive different implementations of class. using avro reflection based encoders , decoders
<bean id="kafkamessageencoder" class="org.springframework.integration.kafka.serializer.avro.avroreflectdatumbackedkafkaencoder"> <constructor-arg value="com.....model.abstractmessage" /> </bean> <bean id="kafkamessagedecoder" class="org.springframework.integration.kafka.serializer.avro.avroreflectdatumbackedkafkadecoder"> <constructor-arg value="com.....model.abstractmessage" /> </bean>
this fails error
avrotypeexception: unknown type: t
this makes sense because avro cant figure out generic type specified in abstractmessage class decided use custom encoder , decoder.
public class messageencoder implements encoder<object> { @override public byte[] tobytes(object object) { try { bytearrayoutputstream baos = new bytearrayoutputstream(); objectoutputstream oos = new objectoutputstream(baos); oos.writeobject(object); oos.flush(); oos.close(); return baos.tobytearray(); } catch (exception e) { e.printstacktrace(); } return "".getbytes(); } } public class messagedecoder implements decoder<object> { @override public object frombytes(byte[] bs) { try { objectinputstream bais = new objectinputstream(new bytearrayinputstream(bs)); abstractmessage<?> message = (abstractmessage<?>) bais.readobject(); return message; } catch (exception e) { e.printstacktrace(); } return null; } }
and works fine.
i guess question not doing special here. why cant avro same thing , there different avro d/encoder can serialize/deserialize generic type objects.
thanks
Comments
Post a Comment