com.gemstone.gemfire
Class Instantiator

Instantiator allows classes that implement DataSerializable to be registered with the data serialization
framework. Knowledge of DataSerializable classes
allows the framework to optimize how instances of those classes are
data serialized.

However, if a DataSerializable class is registered with the data serialization framework and
assigned a unique class id, an important optimization can be
performed that avoid the expense of using reflection to instantiate
the DataSerializable class. When the object is
written using DataSerializer.writeObject(Object, java.io.DataOutput), the object's
registered class id is written to the stream. Consequently, when
the data is read from the stream, the newInstance() method
of the appropriate Instantiator instance is invoked to
create an "empty" instance of the DataSerializable
instead of using reflection to create the new instance.

Commonly, a DataSerializable class will register
itself with the Instantiator in a static initializer
as shown in the below example code.

Instantiators may be distributed to other members of
the distributed system when they are registered. Consider the
following scenario in which VM1 and VM2 are members of the same
distributed system. Both VMs define the sameRegion and VM2's
region mirrors the contents of VM1's using keys/values mirroring.
VM1 puts an instance of the above User class into the
region. The act of instantiating User will load the
User class and invoke its static initializer, thus
registering the Instantiator with the data
serialization framework. Because the region is mirrored, the
User will be data serialized and sent to VM2.
However, when VM2 attempts to data deserialize the
User, its Instantiator will not
necessarily be registered because User's static
initializer may not have been invoked yet. As a result, an
exception would be logged while deserializing the User
and the mirror would not appear to have the new value. So, in
order to ensure that the Instantiator is registered in
VM2, the data serialization framework distributes a message to each
member when an Instantiator is registered. Note that the framework does not require that an
Instantiator be Serializable, but it
does require that it provide a two-argument constructor.