Class KryoCodecFactory<T>
- java.lang.Object
-
- io.hekate.codec.kryo.KryoCodecFactory<T>
-
- Type Parameters:
T
- Base type of data that should be supported by this factory.
- All Implemented Interfaces:
CodecFactory<T>
public class KryoCodecFactory<T> extends Object implements CodecFactory<T>
« start hereKryo-based implementation ofCodecFactory
interface.Module dependency
Kryo integration is provided by the 'hekate-codec-kryo' module and can be imported into the project dependency management system as in the example below:
<dependency> <groupId>io.hekate</groupId> <artifactId>hekate-codec-kryo</artifactId> <version>4.0.0</version> </dependency>
compile group: 'io.hekate', name: 'hekate-codec-kryo', version: '4.0.0'
<dependency org="io.hekate" name="hekate-codec-kryo" rev="4.0.0"/>
Configuration
Each call of
createCodec()
method will produces a new independentKryo
instance and will configure it according to properties of this factory (see description of property setters).// Prepare Kryo codec factory. KryoCodecFactory<Object> kryoCodec = new KryoCodecFactory<>() .withReferences(true) .withUnsafeIo(true) // Register known types. .withKnownType(MyClass1.class) .withKnownType(MyClass2.class); // Register Kryo codec factory and start a new node. Hekate node = new HekateBootstrap() .withDefaultCodec(kryoCodec) // ...other options... .join();
-
-
Constructor Summary
Constructors Constructor Description KryoCodecFactory()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Codec<T>
createCodec()
Creates codec.Map<Class<?>,com.esotericsoftware.kryo.Serializer<?>>
getDefaultSerializers()
Returns the map of default serializers (seesetSerializers(Map)
).org.objenesis.strategy.InstantiatorStrategy
getInstantiatorStrategy()
Returns the instantiator strategy (seesetInstantiatorStrategy(InstantiatorStrategy)
).List<Class<?>>
getKnownTypes()
Returns the list of known java types (seesetKnownTypes(List)
).Boolean
getReferences()
Returns the value that should overrideKryo.setReferences(boolean)
flag (seesetReferences(Boolean)
).Map<Class<?>,com.esotericsoftware.kryo.Serializer<?>>
getSerializers()
Returns the map of custom serializer (seesetSerializers(Map)
).boolean
isCacheUnknownTypes()
Returns the flag that controls whether class-to-identifiers mapping should be cached (seesetCacheUnknownTypes(boolean)
).boolean
isRegistrationRequired()
Returns the flag indicating if registration is required for all serializable classes (seesetRegistrationRequired(boolean)
).boolean
isUnsafeIo()
void
setCacheUnknownTypes(boolean cacheUnknownTypes)
Sets the flag that controls the behavior of unmapped classes caching.void
setDefaultSerializers(Map<Class<?>,com.esotericsoftware.kryo.Serializer<?>> defaultSerializers)
Sets the map of default serializer.void
setInstantiatorStrategy(org.objenesis.strategy.InstantiatorStrategy instantiatorStrategy)
Sets instantiator strategy.void
setKnownTypes(List<Class<?>> knownTypes)
Sets the list of known Java types.void
setReferences(Boolean references)
Sets the value that should override theKryo.setReferences(boolean)
flag.void
setRegistrationRequired(boolean registrationRequired)
Sets the flag indicating that all serializable classes must be registered viasetKnownTypes(List)
.void
setSerializers(Map<Class<?>,com.esotericsoftware.kryo.Serializer<?>> serializers)
Sets the map of custom serializers.void
setUnsafeIo(boolean unsafeIo)
Set totrue
in order to useUnsafeInput
/UnsafeOutput
with Kryo.String
toString()
KryoCodecFactory<T>
withCacheUnknownTypes(boolean cacheUnknownTypes)
Fluent-style version ofsetCacheUnknownTypes(boolean)
.<V> KryoCodecFactory<T>
withDefaultSerializer(Class<V> type, com.esotericsoftware.kryo.Serializer<V> serializer)
Fluent style version ofsetDefaultSerializers(Map)
.KryoCodecFactory<T>
withInstantiatorStrategy(org.objenesis.strategy.InstantiatorStrategy instantiatorStrategy)
Fluent-style version ofsetInstantiatorStrategy(InstantiatorStrategy)
.KryoCodecFactory<T>
withKnownType(Class<?> type)
Fluent-style version ofsetKnownTypes(List)
.KryoCodecFactory<T>
withKnownTypes(List<Class<?>> types)
Fluent-style version ofsetKnownTypes(List)
.KryoCodecFactory<T>
withReferences(Boolean references)
Fluent-style version ofsetReferences(Boolean)
.KryoCodecFactory<T>
withRegistrationRequired(boolean registrationRequired)
Fluent-style version ofsetRegistrationRequired(boolean)
.KryoCodecFactory<T>
withSerializer(Class<?> type, com.esotericsoftware.kryo.Serializer<?> serializer)
Fluent-style version ofsetSerializers(Map)
.KryoCodecFactory<T>
withUnsafeIo(boolean unsafeIo)
Fluent-style version ofsetUnsafeIo(boolean)
.
-
-
-
Method Detail
-
createCodec
public Codec<T> createCodec()
Description copied from interface:CodecFactory
Creates codec.- Specified by:
createCodec
in interfaceCodecFactory<T>
- Returns:
- Codec.
-
getKnownTypes
public List<Class<?>> getKnownTypes()
Returns the list of known java types (seesetKnownTypes(List)
).- Returns:
- List of known types.
-
setKnownTypes
public void setKnownTypes(List<Class<?>> knownTypes)
Sets the list of known Java types. Such types will be registered viaKryo.register(Class)
.Notice: Exactly the same list of types should be registered on all cluster nodes. Otherwise Kryo will not be able deserialize data. Fore more details please see
Kryo.register(Class)
.- Parameters:
knownTypes
- List of known Java types.
-
withKnownType
public KryoCodecFactory<T> withKnownType(Class<?> type)
Fluent-style version ofsetKnownTypes(List)
.- Parameters:
type
- Type.- Returns:
- This instance.
-
withKnownTypes
public KryoCodecFactory<T> withKnownTypes(List<Class<?>> types)
Fluent-style version ofsetKnownTypes(List)
.- Parameters:
types
- List of known Java types.- Returns:
- This instance.
-
isRegistrationRequired
public boolean isRegistrationRequired()
Returns the flag indicating if registration is required for all serializable classes (seesetRegistrationRequired(boolean)
).- Returns:
- Flag indicating if registration is required for all serializable classes.
-
setRegistrationRequired
public void setRegistrationRequired(boolean registrationRequired)
Sets the flag indicating that all serializable classes must be registered viasetKnownTypes(List)
.In order to get the best performance of data serialization it is recommended to register all serializable classes in advance. If this flag is set to
true
then Kryo will throw an error when trying to serialize a class that is notregistered
. However sometimes it is not feasible to do so, in such case this flag should be setfalse
.Default value of this parameter is
false
(i.e. registration is not required).- Parameters:
registrationRequired
-true
if all serializable classes should beregistered
in advance.
-
withRegistrationRequired
public KryoCodecFactory<T> withRegistrationRequired(boolean registrationRequired)
Fluent-style version ofsetRegistrationRequired(boolean)
.- Parameters:
registrationRequired
-true
if all serializable classes should beregistered
in advance.- Returns:
- This instance.
-
getSerializers
public Map<Class<?>,com.esotericsoftware.kryo.Serializer<?>> getSerializers()
Returns the map of custom serializer (seesetSerializers(Map)
).- Returns:
- Map of custom serializer.
-
setSerializers
public void setSerializers(Map<Class<?>,com.esotericsoftware.kryo.Serializer<?>> serializers)
Sets the map of custom serializers. Such serializers will be registered viaKryo.register(Class, Serializer)
.- Parameters:
serializers
- Map of serializers.
-
withSerializer
public KryoCodecFactory<T> withSerializer(Class<?> type, com.esotericsoftware.kryo.Serializer<?> serializer)
Fluent-style version ofsetSerializers(Map)
.- Parameters:
type
- Type.serializer
- Type serializer.- Returns:
- This instance.
-
getDefaultSerializers
public Map<Class<?>,com.esotericsoftware.kryo.Serializer<?>> getDefaultSerializers()
Returns the map of default serializers (seesetSerializers(Map)
).- Returns:
- Map of default serializer.
-
setDefaultSerializers
public void setDefaultSerializers(Map<Class<?>,com.esotericsoftware.kryo.Serializer<?>> defaultSerializers)
Sets the map of default serializer. Such serializers will be registered viaKryo.addDefaultSerializer(Class, Serializer)
.- Parameters:
defaultSerializers
- Map of default serializer.
-
withDefaultSerializer
public <V> KryoCodecFactory<T> withDefaultSerializer(Class<V> type, com.esotericsoftware.kryo.Serializer<V> serializer)
Fluent style version ofsetDefaultSerializers(Map)
.- Type Parameters:
V
- Type to serialize.- Parameters:
type
- Type to serialize.serializer
- Serializer.- Returns:
- This instance.
-
getInstantiatorStrategy
public org.objenesis.strategy.InstantiatorStrategy getInstantiatorStrategy()
Returns the instantiator strategy (seesetInstantiatorStrategy(InstantiatorStrategy)
).- Returns:
- Instantiator strategy.
-
setInstantiatorStrategy
public void setInstantiatorStrategy(org.objenesis.strategy.InstantiatorStrategy instantiatorStrategy)
Sets instantiator strategy. Such strategy will be registered viaKryo.setInstantiatorStrategy(InstantiatorStrategy)
.By default this parameter is set to
Kryo.DefaultInstantiatorStrategy
with a fallback toStdInstantiatorStrategy
.- Parameters:
instantiatorStrategy
- Instantiator strategy.
-
withInstantiatorStrategy
public KryoCodecFactory<T> withInstantiatorStrategy(org.objenesis.strategy.InstantiatorStrategy instantiatorStrategy)
Fluent-style version ofsetInstantiatorStrategy(InstantiatorStrategy)
.- Parameters:
instantiatorStrategy
- Instantiator strategy.- Returns:
- This instance.
-
isUnsafeIo
public boolean isUnsafeIo()
- Returns:
true
ifUnsafe
-based IO should be used by Kryo.
-
setUnsafeIo
public void setUnsafeIo(boolean unsafeIo)
Set totrue
in order to useUnsafeInput
/UnsafeOutput
with Kryo.Default value of this parameter is
true
.- Parameters:
unsafeIo
-true
to enableUnsafe
-based IO.
-
withUnsafeIo
public KryoCodecFactory<T> withUnsafeIo(boolean unsafeIo)
Fluent-style version ofsetUnsafeIo(boolean)
.- Parameters:
unsafeIo
-true
to enableUnsafe
-based IO.- Returns:
- This instance.
-
getReferences
public Boolean getReferences()
Returns the value that should overrideKryo.setReferences(boolean)
flag (seesetReferences(Boolean)
).- Returns:
- Value that should override
Kryo.setReferences(boolean)
flag.
-
setReferences
public void setReferences(Boolean references)
Sets the value that should override theKryo.setReferences(boolean)
flag. If value of this parameter isnull
(default) thenKryo.setReferences(boolean)
will not be called during Kryo instance construction.- Parameters:
references
- Value that should override theKryo.setReferences(boolean)
flag.
-
withReferences
public KryoCodecFactory<T> withReferences(Boolean references)
Fluent-style version ofsetReferences(Boolean)
.- Parameters:
references
- Value that should override theKryo.setReferences(boolean)
flag.- Returns:
- This instance.
-
isCacheUnknownTypes
public boolean isCacheUnknownTypes()
Returns the flag that controls whether class-to-identifiers mapping should be cached (seesetCacheUnknownTypes(boolean)
).- Returns:
- Flag that controls whether class-to-identifiers mapping should be cached.
-
setCacheUnknownTypes
public void setCacheUnknownTypes(boolean cacheUnknownTypes)
Sets the flag that controls the behavior of unmapped classes caching.If set to
true
then mapping of unknown classes (that were not explicitly registered viawithKnownType(Class)
) to their generated identifiers will be preserved across multiple calls ofencode
/decode
methods when called on the sameCodec
instance. If set tofalse
then such mapping will be reset (seeClassResolver.reset()
) after eachencode
/decode
operation.Note that codec factories with classes caching enabled can not be used as the
default codec
ofHekate
node, as the default codec must be stateless.Default value of this parameter is
false
.- Parameters:
cacheUnknownTypes
- Flag that controls whether class-to-identifiers mapping should be cached.
-
withCacheUnknownTypes
public KryoCodecFactory<T> withCacheUnknownTypes(boolean cacheUnknownTypes)
Fluent-style version ofsetCacheUnknownTypes(boolean)
.- Parameters:
cacheUnknownTypes
- Flag that controls whether class-to-identifiers mapping should be cached.- Returns:
- This instance.
-
-