0

I am using Kryo Serializer for Serializing my Java Object

My Java Object is like this

Class A {

private Class B;
private Class C;
private Class D;
int x ;
int y;
}

B, C and D are third party libraries and not serializable . Also I cant edit their code .

I am using Kryo like this to Serialize A a = new A (); //populate properties of A Kryo kryo = new Kryo(); Output output = new Output(new FileOutputStream("file.bin")); kryo.writeClassAndObject(output, A ); output.close();

and to deserialize

    input = new com.esotericsoftware.kryo.io.Input(new FileInputStream("file.bin"));
        InputQueueManagerImpl inputQueue = (A) kryo.readClassAndObject(input);//, A.class);
        input.close();

While executing , I get ConcurrentModificationException

    at apache.Application.main(Application.java:43)
Caused by: com.esotericsoftware.kryo.KryoException: java.util.ConcurrentModificationException
Serialization trace:
classes (sun.misc.Launcher$AppClassLoader)
contextClassLoader (java.lang.Thread)
threads (java.lang.ThreadGroup)
parent (java.lang.ThreadGroup)
group (java.util.concurrent.Executors$DefaultThreadFactory)
threadFactory (java.util.concurrent.ThreadPoolExecutor)
executor (com.rabbitmq.client.impl.ConsumerWorkService)
workService (com.rabbitmq.client.impl.ConsumerDispatcher)
dispatcher (com.rabbitmq.client.impl.ChannelN)
_channelMap (com.rabbitmq.client.impl.ChannelManager)
_channelManager (com.rabbitmq.client.impl.AMQConnection)
delegate (org.springframework.amqp.rabbit.connection.SimpleConnection)
target     (org.springframework.amqp.rabbit.connection.CachingConnectionFactory$ChannelCachingConnectionProxy)
checkoutPermits (org.springframework.amqp.rabbit.connection.CachingConnectionFactory)
connectionFactory (org.springframework.amqp.rabbit.core.RabbitAdmin)
amqpAdmin (mqclient.rabbitmq.manager.impl.InputQueueManagerImpl)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.write(FieldSerializer.java:585)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:213)

When I make B,C,D transient it works properly . But I want to Serialize B,C,D too.

Can kryo help here? If Yes , what is the correct way to do it?

Ankur Garg
  • 2,553
  • 8
  • 30
  • 41

1 Answers1

0

It means that while kryo is serializing your class and all of its member class (B, C, ...) something got modified in whole hierarchy from another thread. For example let say that class B contains a List list. And something is getting changed in this list while you are serializing your class A -> then you will get above exception. That's how kryo is designed.

Jags
  • 799
  • 7
  • 19