我正在使用 Kryo 序列化器来序列化我的 Java 对象
我的 Java 对象是这样的
Class A {
private Class B;
private Class C;
private Class D;
int x ;
int y;
}
B、C 和 D 是第三方库,不可序列化。我也不能编辑他们的代码。
我正在使用这样的 Kryo 来序列化 A a = new A (); //填充 A Kryo 的属性 kryo = new Kryo(); 输出输出 = new Output(new FileOutputStream("file.bin")); kryo.writeClassAndObject(输出, A ); 输出.close();
并反序列化
input = new com.esotericsoftware.kryo.io.Input(new FileInputStream("file.bin"));
InputQueueManagerImpl inputQueue = (A) kryo.readClassAndObject(input);//, A.class);
input.close();
执行时,我得到 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)
当我使 B、C、D 瞬态时,它可以正常工作。但我也想序列化 B、C、D。
kryo 可以帮忙吗?如果是,那么正确的方法是什么?