Gemfire JavaDocs_test
Class Instantiator
- java.lang.Object
-
- org.apache.geode.Instantiator
-
- Direct Known Subclasses:
CanonicalInstantiator
public abstract class Instantiator extends java.lang.Object
Instantiator
allows classes that implementDataSerializable
to be registered with the data serialization framework. Knowledge ofDataSerializable
classes allows the framework to optimize how instances of those classes are data serialized.Ordinarily, when a
DataSerializable
object is written usingDataSerializer.writeObject(Object, java.io.DataOutput)
, a special marker class id is written to the stream followed by the class name of theDataSerializable
object. After the marker class id is read byDataSerializer.readObject(java.io.DataInput)
it performs the following operations,- The class name is read
- The class is loaded using
Class.forName(java.lang.String)
- An instance of the class is created using reflection
DataSerializable.fromData(java.io.DataInput)
is invoked on the newly-created object
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 theDataSerializable
class. When the object is written usingDataSerializer.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, thenewInstance()
method of the appropriateInstantiator
instance is invoked to create an "empty" instance of theDataSerializable
instead of using reflection to create the new instance.Commonly, a
DataSerializable
class will register itself with theInstantiator
in a static initializer as shown in the below example code.public class User implements DataSerializable { private String name; private int userId; static { Instantiator.register(new Instantiator(User.class, 45) { public DataSerializable newInstance() { return new User(); } }); } public User(String name, int userId) { this.name = name; this.userId = userId; } /** Creates an "empty" User whose contents are filled in by invoking its toData() method / private User() { } public void toData(DataOutput out) throws IOException { out.writeUTF(this.name); out.writeInt(this.userId); } public void fromData(DataInput in) throws IOException, ClassNotFoundException { this.name = in.readUTF(); this.userId = in.readInt(); } }
Instantiator
s 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 replicates the contents of VM1's using replication. VM1 puts an instance of the aboveUser
class into the region. The act of instantiatingUser
will load theUser
class and invoke its static initializer, thus registering theInstantiator
with the data serialization framework. Because the region is a replicate, theUser
will be data serialized and sent to VM2. However, when VM2 attempts to data deserialize theUser
, itsInstantiator
will not necessarily be registered becauseUser
's static initializer may not have been invoked yet. As a result, an exception would be logged while deserializing theUser
and the replicate would not appear to have the new value. So, in order to ensure that theInstantiator
is registered in VM2, the data serialization framework distributes a message to each member when anInstantiator
is registered.Note that the framework does not require that an
Instantiator
beSerializable
, but it does require that it provide a two-argument constructor.- Since:
- GemFire 3.5
- See Also:
register(Instantiator)
,newInstance()
-
-
Constructor Summary
Constructors Constructor Description Instantiator(java.lang.Class<? extends DataSerializable> c, int classId)
Creates a newInstantiator
that instantiates a given class.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.lang.Object
getContext()
Returns the context of thisInstantiator
.java.lang.Object
getEventId()
Returns the uniqueeventId
of thisInstantiator
.int
getId()
Returns the uniqueid
of thisInstantiator
.java.lang.Class<? extends DataSerializable>
getInstantiatedClass()
Returns theDataSerializable
class that is instantiated by thisInstantiator
.abstract DataSerializable
newInstance()
Creates a new "empty" instance of aDataSerializable
class whose state will be filled in by invoking itsfromData
method.static void
register(Instantiator instantiator)
Registers aDataSerializable
class with the data serialization framework.static void
register(Instantiator instantiator, boolean distribute)
Deprecated.as of 9.0 useregister(Instantiator)
insteadvoid
setContext(java.lang.Object context)
sets the context of thisInstantiator
.void
setEventId(java.lang.Object eventId)
sets the uniqueeventId
of thisInstantiator
.
-
-
-
Constructor Detail
-
Instantiator
public Instantiator(java.lang.Class<? extends DataSerializable> c, int classId)
Creates a newInstantiator
that instantiates a given class.- Parameters:
c
- TheDataSerializable
class to register. This class must have a static initializer that registers thisInstantiator
.classId
- A unique id for classc
. TheclassId
must not be zero. This has been anint
since dsPhase1.- Throws:
java.lang.IllegalArgumentException
- Ifc
does not implementDataSerializable
,classId
is less than or equal to zero.java.lang.NullPointerException
- Ifc
isnull
-
-
Method Detail
-
register
public static void register(Instantiator instantiator)
Registers aDataSerializable
class with the data serialization framework. This method is usually invoked from the static initializer of a class that implementsDataSerializable
.- Parameters:
instantiator
- AnInstantiator
whosenewInstance()
method is invoked when an object is data deserialized.- Throws:
java.lang.IllegalStateException
- If classc
is already registered with a different class id, or another class has already been registered with idclassId
java.lang.NullPointerException
- Ifinstantiator
isnull
.
-
register
@Deprecated public static void register(Instantiator instantiator, boolean distribute)
Deprecated.as of 9.0 useregister(Instantiator)
insteadRegisters aDataSerializable
class with the data serialization framework. This method is usually invoked from the static initializer of a class that implementsDataSerializable
.- Parameters:
instantiator
- AnInstantiator
whosenewInstance()
method is invoked when an object is data deserialized.distribute
- True if the registeredInstantiator
has to be distributed to other members of the distributed system. Note that if distribute is set to false it may still be distributed in some cases.- Throws:
java.lang.IllegalArgumentException
- If classc
is already registered with a different class id, or another class has already been registered with idclassId
java.lang.NullPointerException
- Ifinstantiator
isnull
.
-
newInstance
public abstract DataSerializable newInstance()
Creates a new "empty" instance of aDataSerializable
class whose state will be filled in by invoking itsfromData
method.- Returns:
- a new "empty" instance of a
DataSerializable
class - See Also:
DataSerializer.readObject(java.io.DataInput)
-
getInstantiatedClass
public java.lang.Class<? extends DataSerializable> getInstantiatedClass()
Returns theDataSerializable
class that is instantiated by thisInstantiator
.- Returns:
- the
DataSerializable
class that is instantiated by thisInstantiator
-
getId
public int getId()
Returns the uniqueid
of thisInstantiator
.- Returns:
- the unique
id
of thisInstantiator
-
setEventId
public void setEventId(java.lang.Object eventId)
sets the uniqueeventId
of thisInstantiator
. For internal use only.- Parameters:
eventId
- the uniqueeventId
of thisInstantiator
-
getEventId
public java.lang.Object getEventId()
Returns the uniqueeventId
of thisInstantiator
. For internal use only.- Returns:
- the unique
eventId
of thisInstantiator
-
setContext
public void setContext(java.lang.Object context)
sets the context of thisInstantiator
. For internal use only.- Parameters:
context
- the context of thisInstantiator
-
getContext
public java.lang.Object getContext()
Returns the context of thisInstantiator
. For internal use only.- Returns:
- the context of this
Instantiator
-
-