Here are some important points to take into account when a user implements a custom serialization code.
1 To use serialization for session failover, any objects in the context should implement the java.io.Serializable or java.io.Externalizable interface. For example, the custom services and DataFields in context should implement externalizable on writeExternal and readExternal methods.
2 Externalizable has a better performance than Serializable, so UDTT uses Externalizable by default.
3 For all fields in the class, no matter public or private, they all should be handled in writeExternal and readExternal methods.
4 The order of fields in writeExternal and readExternal should be exactly the same order.
5 The static and constant fields are not required to be serialized.
6 When the class implements Externalizable, it requires a public no-arg constructor.
The following is an example of Java Serialization / Deserialization.
public class ObjectInHashTable implements java.io.Serializable {
public String refObjectId;
public ObjectInHashTable(String id){ refObjectId=id; }
@Override public String toString(){ return refObjectId; }
}
public class ExternalizableDemo implements java.io.Externalizable{ String name; String age; String gender; Hashtable ht;
For Serializable interface, all fields of java primitive type are serialized and deserialized by default.
For Externalizable interface, only the fields that are handled in writeExternal and readExternal methods are serialized and deserialized. In the following example, the gender field is not handled in writeExternal and readExternal methods, so the value of gender is not restored after Serialiation/DeSerialization. The following is the console output of the example program.