Runtime tools : Channels components : Session context failover : Java Serialization / Deserialization
  
Java Serialization / Deserialization
Here are some document links about java objects serialization for reference:
https://www.geeksforgeeks.org/serialization-in-java/
https://www.geeksforgeeks.org/externalizable-interface-java/
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;
public ExternalizableDemo(){
}
public ExternalizableDemo (String name,String age, String gender){
this.name=name;
this.age=age;
this. gender = gender;
ht=new com.ibm.btt.base.Hashtable ();
ht.put ("key1", "Value1");
ht.put ("key2", "Value2");
ht.put ("refObject", new ObjectInHashTable("reFFFOBJS"));
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(name);
out.writeObject(age);
out.writeObject(ht);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
this.name=(String)in.readObject();
this.age=(String)in.readObject();
this.ht= (com.ibm.btt.base.Hashtable) in.readObject();
}
public String toString() {
  return "name="+name+" age="+age+ " gender="+gender+"\n"+ht;
}
public static void main(String[] args) {
ExternalizableDemopeaple=new ExternalizableDemo("mayou", "18", "man");
System.out.println(peaple.toString());
try {
ObjectOutputStreamobjOutput=new ObjectOutputStream(new
FileOutputStream("c://cache.txt"));
objOutput.writeObject(peaple);
objOutput.close();
ObjectInputStreamois=new ObjectInputStream(new
FileInputStream("c://cache.txt"));
peaple = (ExternalizableDemo)ois.readObject();
System.out.println("##### after Serialiation/DeSerialization :");
System.out.println(peaple.toString());
} catch (Throwable t) {
t.printStackTrace();
}
}
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.
name=mayou age=18 gender=man
<key=refObject value=reFFFOBJS>
<key=key1 value=Value1>
<key=key2 value=Value2>
##### after Serialiation/DeSerialization :
name=mayou age=18 gender=null
<key=key1 value=Value1>
<key=refObject value=reFFFOBJS>
<key=key2 value=Value2>
Go up to
Session context failover