Class 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 of CodecFactory 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 independent Kryo 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();
    

    See Also:
    HekateBootstrap.setDefaultCodec(CodecFactory)
    • Constructor Detail

      • KryoCodecFactory

        public KryoCodecFactory()
    • Method Detail

      • getKnownTypes

        public List<Class<?>> getKnownTypes()
        Returns the list of known java types (see setKnownTypes(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 via Kryo.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.
      • isRegistrationRequired

        public boolean isRegistrationRequired()
        Returns the flag indicating if registration is required for all serializable classes (see setRegistrationRequired(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 via setKnownTypes(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 not registered. However sometimes it is not feasible to do so, in such case this flag should be set false.

        Default value of this parameter is false (i.e. registration is not required).

        Parameters:
        registrationRequired - true if all serializable classes should be registered in advance.
      • withRegistrationRequired

        public KryoCodecFactory<T> withRegistrationRequired​(boolean registrationRequired)
        Fluent-style version of setRegistrationRequired(boolean).
        Parameters:
        registrationRequired - true if all serializable classes should be registered in advance.
        Returns:
        This instance.
      • getSerializers

        public Map<Class<?>,​com.esotericsoftware.kryo.Serializer<?>> getSerializers()
        Returns the map of custom serializer (see setSerializers(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 via Kryo.register(Class, Serializer).
        Parameters:
        serializers - Map of serializers.
      • withSerializer

        public KryoCodecFactory<T> withSerializer​(Class<?> type,
                                                  com.esotericsoftware.kryo.Serializer<?> serializer)
        Fluent-style version of setSerializers(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 (see setSerializers(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 via Kryo.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 of setDefaultSerializers(Map).
        Type Parameters:
        V - Type to serialize.
        Parameters:
        type - Type to serialize.
        serializer - Serializer.
        Returns:
        This instance.
      • setInstantiatorStrategy

        public void setInstantiatorStrategy​(org.objenesis.strategy.InstantiatorStrategy instantiatorStrategy)
        Sets instantiator strategy. Such strategy will be registered via Kryo.setInstantiatorStrategy(InstantiatorStrategy).

        By default this parameter is set to Kryo.DefaultInstantiatorStrategy with a fallback to StdInstantiatorStrategy.

        Parameters:
        instantiatorStrategy - Instantiator strategy.
      • isUnsafeIo

        public boolean isUnsafeIo()
        Returns true if Unsafe-based IO should be used by Kryo (see setUnsafeIo(boolean)).
        Returns:
        true if Unsafe-based IO should be used by Kryo.
      • setUnsafeIo

        public void setUnsafeIo​(boolean unsafeIo)
        Set to true in order to use UnsafeInput/UnsafeOutput with Kryo.

        Default value of this parameter is true.

        Parameters:
        unsafeIo - true to enable Unsafe-based IO.
      • withUnsafeIo

        public KryoCodecFactory<T> withUnsafeIo​(boolean unsafeIo)
        Fluent-style version of setUnsafeIo(boolean).
        Parameters:
        unsafeIo - true to enable Unsafe-based IO.
        Returns:
        This instance.
      • getReferences

        public Boolean getReferences()
        Returns the value that should override Kryo.setReferences(boolean) flag (see setReferences(Boolean)).
        Returns:
        Value that should override Kryo.setReferences(boolean) flag.
      • setReferences

        public void setReferences​(Boolean references)
        Sets the value that should override the Kryo.setReferences(boolean) flag. If value of this parameter is null (default) then Kryo.setReferences(boolean) will not be called during Kryo instance construction.
        Parameters:
        references - Value that should override the Kryo.setReferences(boolean) flag.
      • withReferences

        public KryoCodecFactory<T> withReferences​(Boolean references)
        Fluent-style version of setReferences(Boolean).
        Parameters:
        references - Value that should override the Kryo.setReferences(boolean) flag.
        Returns:
        This instance.
      • isCacheUnknownTypes

        public boolean isCacheUnknownTypes()
        Returns the flag that controls whether class-to-identifiers mapping should be cached (see setCacheUnknownTypes(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 via withKnownType(Class)) to their generated identifiers will be preserved across multiple calls of encode/decode methods when called on the same Codec instance. If set to false then such mapping will be reset (see ClassResolver.reset()) after each encode/decode operation.

        Note that codec factories with classes caching enabled can not be used as the default codec of Hekate 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 of setCacheUnknownTypes(boolean).
        Parameters:
        cacheUnknownTypes - Flag that controls whether class-to-identifiers mapping should be cached.
        Returns:
        This instance.