Reputation: 41
i have a problem and the documentations and github examples doesn't provide a clear example about how to do it....
i have this class
public class KeySerializable implements IdentifiedDataSerializable{
private String claveReq;
private int id_interno_pe;
private String cod_nrbe_en;
private int num_sec_ac;
public KeySerializable(String claveReq, int id_interno_pe, String cod_nrbe_en, int num_sec_ac) {
this.claveReq = claveReq;
this.id_interno_pe = id_interno_pe;
this.cod_nrbe_en = cod_nrbe_en;
this.num_sec_ac = num_sec_ac;
}
public KeySerializable() {
}
public void writeData(ObjectDataOutput out) throws IOException {
out.writeString(claveReq);
out.writeInt(id_interno_pe);
out.writeString(cod_nrbe_en);
out.writeInt(num_sec_ac);
}
public void readData(ObjectDataInput in) throws IOException {
this.claveReq = in.readString();
this.id_interno_pe = in.readInt();
this.cod_nrbe_en = in.readString();
this.num_sec_ac = in.readInt();
}
public int getFactoryId() {
return KeySerializableFactory.FACTORY_ID;
}
public int getClassId() {
return KeySerializableFactory.KEY_SERIALIZABLE_TYPE;
}
@Override
public String toString() {
return "KeySerializable [claveReq=" + claveReq + ", id_interno_pe=" + id_interno_pe + ", cod_nrbe_en="
+ cod_nrbe_en + ", num_sec_ac=" + num_sec_ac + "]";
}
}
and this class
public class ResponseSerializablePlus implements IdentifiedDataSerializable{
private int id_interno_pe;
private String cod_nrbe_en;
private int num_sec_ac;
private int statusCode;
private HashMap<String,List<String>> headers;
private byte[] content;
public ResponseSerializablePlus(int id_interno_pe, String cod_nrbe_en, int num_sec_ac, int statusCode,
HashMap<String, List<String>> headers, byte[] content) {
this.id_interno_pe = id_interno_pe;
this.cod_nrbe_en = cod_nrbe_en;
this.num_sec_ac = num_sec_ac;
this.statusCode = statusCode;
this.headers = headers;
this.content = content;
}
public ResponseSerializablePlus() {
}
public void writeData(ObjectDataOutput out) throws IOException {
out.writeInt(id_interno_pe);
out.writeString(cod_nrbe_en);
out.writeInt(num_sec_ac);
out.write(statusCode);
out.writeObject(headers);
out.writeByteArray(content);
}
public void readData(ObjectDataInput in) throws IOException {
this.id_interno_pe = in.readInt();
this.cod_nrbe_en = in.readString();
this.num_sec_ac = in.readInt();
this.statusCode = in.readInt();
this.headers = in.readObject();
this.content = in.readByteArray();
}
public int getFactoryId() {
return ResponseSerializablePlusFactory.FACTORY_ID;
}
public int getClassId() {
return ResponseSerializablePlusFactory.RESPONSE_SERIALIZABLE_PLUS_CLASS;
}
@Override
public String toString() {
return "ResponseSerializablePlus [id_interno_pe=" + id_interno_pe + ", cod_nrbe_en=" + cod_nrbe_en
+ ", num_sec_ac=" + num_sec_ac + ", statusCode=" + statusCode + ", headers=" + headers + ", content="
+ Arrays.toString(content) + "]";
}
and this other class
public class ResponseSerializable implements IdentifiedDataSerializable{
private int statusCode;
private HashMap<String,List<String>> headers;
private byte[] content;
public ResponseSerializable(int statusCode, HashMap<String, List<String>> headers, byte[] content) {
this.statusCode = statusCode;
this.headers = headers;
this.content = content;
}
public ResponseSerializable() {
}
public void writeData(ObjectDataOutput out) throws IOException {
out.write(statusCode);
out.writeObject(headers);
out.writeByteArray(content);
}
public void readData(ObjectDataInput in) throws IOException {
this.statusCode = in.readInt();
this.headers = in.readObject();
this.content = in.readByteArray();
}
public int getFactoryId() {
return ResponseSerializableFactory.FACTORY_ID;
}
public int getClassId() {
return ResponseSerializableFactory.RESPONSE_TYPE;
}
@Override
public String toString() {
return "ResponseSerializable [statusCode=" + statusCode + ", headers=" + headers + ", content="
+ Arrays.toString(content) + "]";
}
}
and the factory it's always the same but with different classes
public class KeySerializableFactory implements DataSerializableFactory{
public static final int FACTORY_ID = 1;
public static final int KEY_SERIALIZABLE_TYPE = 1;
public IdentifiedDataSerializable create(int typeId) {
if ( typeId == KEY_SERIALIZABLE_TYPE ) {
return new KeySerializable();
} else {
return null;
}
}
}
and im always having this bunch errors
the documentation and the github examples from hazelcast doesn't provide a good example about how to use the getters and setters and i don't understand what to do here to write or read an object
any hint? can you help me?
Upvotes: 0
Views: 376
Reputation: 266
You shouldn't be calling readData
or writeData
yourself. These methods are called by Hazelcast internally when the data is about to be serialized/deserialized.
On your side, you should only register the data serializable factories you have created to the Hazelcast.
Then, upon receiving an instance of a class that implements IdentifiedDataSerializable
and has a factory registered for it, Hazelcast will call the methods I mentioned above in appropriate places and return you the object read.
From the code sample you shared, you need to drop the line starting with newResponse.writeData(...
, and everything should be working, assuming you did the factory registration.
Also, please fix your ResponseSerializablePlus
and ResponseSerializable
classes: You need to use writeInt
method to write the statusCode
field. The readData
and writeData
methods should be consistent with each other.
Upvotes: 0