On Wed, Jun 20, 2012 at 2:46 AM, Richards
<hbkri...@gmail.com> wrote:
Hi,
I would like to use Kryo Serialization for my project. I am new to kryo and I have gone through the documentation. But I am quite confused about its usage as we relied on java serialization till now.
The use case is like this:
I have an abstract class Constant which has different concrete implementations like NumberConstant, DateConstant, NumberRangeConstant, StringConstant, etc.
I would like to serialize these implementations before they are exchanged between JVM.
What I did was:
/***************************************************************************************************************************************************************/
abstract class Constant implements Serializable, KryoSerializable{
//some methods are there
}
/***************************************************************************************************************************************************************/
class NumberConstant extends Constant implements Serializable, KryoSerializable{
double value;
public NumberConstant(){
value=0.0;
}
//setters, getters, hashCode(), equals() also present
public void read(Kryo kryo, Input input) {
value = new Double(input.readDouble());
input.close();
Don't close the input during deserialization.
}
public void write(Kryo kryo, Output output) {
output.writeDouble(value.doubleValue());
output.flush();
output.close();
Don't close the output during serialization.
}
}
/***************************************************************************************************************************************************************/
class NumberRangeConstant extends Constant implements Serializable, KryoSerializable{
private NumberConstant min;
private NumberConstant max;
public NumberRangeConstant(NumberConstant min, private NumberConstant max){
this.min=min;
this.max = max;
}
//some helper methods to compare ranges are also present here.
public void read(Kryo kryo, Input input) {
//1. Is registration required at this point?
kryo.register(NumericConstant.class);
Register all classes you want to register before any serialization or deserialization. Registering is optional.
// 2. I think I should read only these object without classes. Am I correct?
start = kryo.readObject(input, NumericConstant.class);
end = kryo.readObject(input, NumericConstant.class);
input.close();
You get the idea by now.
}
@Override
public void write(Kryo kryo, Output output) {
//3. Is registration required at this point?
kryo.register(NumericConstant.class);
//4. I think I have to write the objects only and not their classes. Am I correct?
kryo.writeObject(output, start);
kryo.writeObject(output, end);
output.close();
output.flush();
}
}
/***************************************************************************************************************************************************************/
Similarly I have other constant classes also.
5. How do I create a Serilizer for these constants? Is the following code correct?
public class ConstantSerializer extends com.esotericsoftware.kryo.Serializer<Constant> {
@Override
public void write(Kryo kryo, Output output, Constant constant) {
kryo.writeClassAndObject(output, constant);
You need to write the data for the constant here. If you call Kryo to write it, it will call your serializer to write it: infinite loop.
The real problem is you should only register serializers for concrete types, not interfaces or abstract classes.
}
@Override
public Constant create(Kryo kryo, Input input, Class<Constant> constantClass) {
/* 6. What should I do if there are no default constructors? Moreover there different implementations for Constant class*/
return (Constant) kryo.readClassAndObject(input);
}
//7. Should I override the read() also? If yes how should I do it?
The latest Kryo does not have create and read methods. You should update to the latest.
}
/***************************************************************************************************************************************************************/
8. I would like to use a Map<Integer,Constant> which should be shared between JVMs. How should I use a Serializer for that? There can be different kinds of Constants in the map. I think I should use both MapSerializer and ConstantSerializer. Can you show me a sample code for that?
This isn't clear. You have the same map in both JVMs and you only want to set an int key instead of a whole Constant? Or you want to send the map?
You seem to be pretty much all over the place here. Maybe try a simpler example? Create a new simple class that only has one field and serialize it. Maybe breakpoint and step through what Kryo is doing, it isn't much code at all.
Also, Kryo can serialize most classes (yours that I've seen are very simple) without you doing anything. You
don't have to register any classes, your classes don't need to implement
anything, and you don't have to write any serializers. Use the quickstart code here:
https://code.google.com/p/kryo/#QuickstartUse writeClassAndObject/readClassAndObject if you have a Constant, since you may not be sure what the concrete type is. You can also pass it a Map<Integer, Constant> and it will just work.
-Nate