Class MultiplexingSerializer

  • All Implemented Interfaces:
    ClassLoaderAware, Serializer

    @Named("multi")
    public class MultiplexingSerializer
    extends Object
    implements Serializer, ClassLoaderAware
    A Serializer implementation that multiplexes serialization/deserialization requests across multiple Serializer implementations.

    More specifically, for serialization operations, the list of available Serializers are iterated over. The first serializer that is able to serialize the object without error, will be the Serializer used for that type going forward. All serialized payloads will be prefixed with the following header to enable deserialization.

         +--------------+-------------------------+
         |  Length (4)  | Serializer Name (1...)  |
         +--------------+-------------------------+
         |  Serialized Payload (0...)             |
         +----------------------------------------+
     
    Upon deserialization, the header will be read and the specified Serializer will be used to deserialize the payload. If the header is no present, an IOException will be thrown.
    Since:
    20.06
    • Constructor Detail

      • MultiplexingSerializer

        public MultiplexingSerializer​(Serializer... serializers)
        Construct a new Serializer that will multiplex serialization operations across the provided list of Serializers. It's important to keep in mind that the iteration order when performing serialization/deserialization operation is based on the order they are provided. This may have bearing on ordering decisions.
        Throws:
        IllegalArgumentException - if no Serializers are provided, or if the any of the provided Serializers return null from Serializer.getName()
    • Method Detail

      • serialize

        public void serialize​(WriteBuffer.BufferOutput out,
                              Object o)
                       throws IOException
        Description copied from interface: Serializer
        Serialize an object to a WriteBuffer by writing its state using the specified BufferOutput object.

        Note: Starting with Coherence 12.2.1 classes that need to designate an alternative object to be used by the Serializer when writing the object to the buffer should implement the SerializationSupport.writeReplace() method.

        Specified by:
        serialize in interface Serializer
        Parameters:
        out - the BufferOutput with which to write the object's state
        o - the object to serialize
        Throws:
        IOException - if an I/O error occurs
      • deserialize

        public <T> T deserialize​(ReadBuffer.BufferInput in,
                                 Class<? extends T> clazz)
                          throws IOException
        Description copied from interface: Serializer
        Deserialize an object as an instance of the specified class by reading its state using the specified BufferInput object.

        Note: Starting with Coherence 12.2.1 classes that need to designate an alternative object to be returned by the Serializer after an object is deserialized from the buffer should implement the SerializationSupport.readResolve() method.

        Specified by:
        deserialize in interface Serializer
        Type Parameters:
        T - the class to deserialize to
        Parameters:
        in - the BufferInput with which to read the object's state
        clazz - the type of the object to deserialize
        Returns:
        the deserialized user type instance
        Throws:
        IOException - if an I/O error occurs
      • getName

        public String getName()
        Description copied from interface: Serializer
        Return the name of this serializer.
        Specified by:
        getName in interface Serializer
        Returns:
        the name of this serializer
      • setContextClassLoader

        public void setContextClassLoader​(ClassLoader loader)
        Description copied from interface: ClassLoaderAware
        Specify the context ClassLoader for this object. The context ClassLoader can be set when the object is created, and allows the creator to provide the appropriate class loader to be used by the object when when loading classes and resources.
        Specified by:
        setContextClassLoader in interface ClassLoaderAware
        Parameters:
        loader - the context ClassLoader for this object
      • logSerializationErrors

        protected void logSerializationErrors​(Map<Serializer,​Exception> serializerExceptionMap,
                                              String typeName)
        Given the provided mappings between a Serializer and the Exception it threw, log each mapping for diagnostic purposes.
        Parameters:
        serializerExceptionMap - a mapping between a Serializer instance and the Exception it threw for any given serialization operation
        typeName - The name of the type that produced the error.
      • writeSerializationHeader

        protected int writeSerializationHeader​(WriteBuffer.BufferOutput out,
                                               String serializerName)
                                        throws IOException
        Write the multiplex serialization header.
             +--------------+-------------------------+
             |  Length (4)  | Serializer Name (1...)  |
             +--------------+-------------------------+
             |  Serialized Payload (0...)             |
             +----------------------------------------+
         
        Parameters:
        out - the stream to write to
        serializerName - the name of the Serializer
        Throws:
        IOException - if an error occurs writing the header or payload
        See Also:
        Serializer.getName()