Serialization Configuration Wrap-Up
This section summarizes the configuration of serialization options, explained in the above sections, into all-in-one examples. The following are example serialization configurations.
Declarative Configuration:
<hazelcast>
...
<serialization>
<portable-version>2</portable-version>
<use-native-byte-order>true</use-native-byte-order>
<byte-order>BIG_ENDIAN</byte-order>
<enable-compression>true</enable-compression>
<enable-shared-object>false</enable-shared-object>
<allow-unsafe>true</allow-unsafe>
<data-serializable-factories>
<data-serializable-factory factory-id="1001">
abc.xyz.Class
</data-serializable-factory>
</data-serializable-factories>
<portable-factories>
<portable-factory factory-id="9001">
xyz.abc.Class
</portable-factory>
</portable-factories>
<serializers>
<global-serializer>abc.Class</global-serializer>
<serializer type-class="Employee" class-name="com.EmployeeSerializer">
</serializer>
</serializers>
<check-class-def-errors>true</check-class-def-errors>
</serialization>
...
</hazelcast>
Programmatic Configuration:
Config config = new Config();
SerializationConfig srzConfig = config.getSerializationConfig();
srzConfig.setPortableVersion( "2" ).setUseNativeByteOrder( true );
srzConfig.setAllowUnsafe( true ).setEnableCompression( true );
srzConfig.setCheckClassDefErrors( true );
GlobalSerializerConfig globSrzConfig = srzConfig.getGlobalSerializerConfig();
globSrzConfig.setClassName( "abc.Class" );
SerializerConfig serializerConfig = srzConfig.getSerializerConfig();
serializerConfig.setTypeClass( "Employee" )
.setClassName( "com.EmployeeSerializer" );
Serialization configuration has the following elements.
-
portable-version
: Defines versioning of the portable serialization. Portable version differentiates two of the same classes that have changes, such as adding/removing field or changing a type of a field. -
use-native-byte-order
: Set totrue
to use native byte order for the underlying platform. Its default value isfalse
. -
byte-order
: Defines the byte order that the serialization uses:BIG_ENDIAN
orLITTLE_ENDIAN
. Its default value isBIG_ENDIAN
. -
enable-compression
: Enables compression if default Java serialization is used. Its default value isfalse
. -
enable-shared-object
: Enables shared object if default Java serialization is used. Its default value isfalse
. -
allow-unsafe
: Set totrue
to allowunsafe
to be used. Its default value isfalse
. -
data-serializable-factory
: Custom classes implementingcom.hazelcast.nio.serialization.DataSerializableFactory
to be registered. These can be used to speed up serialization/deserialization of objects. -
portable-factory
: ThePortableFactory
class to be registered. -
global-serializer
: The global serializer class to be registered if no other serializer is applicable. This element has the optional boolean attributeoverride-java-serialization
. If set totrue
, the Java serialization step is assumed to be handled by the global serializer. Java Serializable and Externalizable is prior to global serializer by default (false
). -
serializer
: The class name of the serializer implementation. -
check-class-def-errors
: When set totrue
, the serialization system checks for class definitions error at start and throws a Serialization Exception with an error definition.