org.apache.ignite.internal.marshaller.optimized

## Class OptimizedMarshaller

- java.lang.Object
- - org.apache.ignite.marshaller.AbstractMarshaller
  - - org.apache.ignite.marshaller.AbstractNodeNameAwareMarshaller
    - - org.apache.ignite.internal.marshaller.optimized.OptimizedMarshaller

All Implemented Interfaces:
  Marshaller

    public class OptimizedMarshaller
    extends AbstractNodeNameAwareMarshaller

Optimized implementation of `Marshaller`. Unlike `JdkMarshaller`, which is based on standard `ObjectOutputStream`, this marshaller does not enforce that all serialized objects implement `Serializable` interface. It is also about 20 times faster as it removes lots of serialization overhead that exists in default JDK implementation.

`OptimizedMarshaller` is tested only on Java HotSpot VM on other VMs it could yield unexpected results. It is the default marshaller on Java HotSpot VMs and will be used if no other marshaller was explicitly configured.

# Configuration

## Mandatory

This marshaller has no mandatory configuration parameters.

## Java Example

     OptimizedMarshaller marshaller = new OptimizedMarshaller();

     // Enforce Serializable interface.
     marshaller.setRequireSerializable(true);

     IgniteConfiguration cfg = new IgniteConfiguration();

     // Override marshaller.
     cfg.setMarshaller(marshaller);

     // Starts grid.
     G.start(cfg);

## Spring Example

GridOptimizedMarshaller can be configured from Spring XML configuration file:

     <bean id="grid.custom.cfg" class="org.apache.ignite.configuration.IgniteConfiguration" singleton="true">
         ...
         <property name="marshaller">
             <bean class="OptimizedMarshaller">
                 <property name="requireSerializable">true</property>
             </bean>
         </property>
         ...
     </bean>

For information about Spring framework visit www.springframework.org

### Field Summary

Fields
`static boolean` `USE_DFLT_SUID` Use default `serialVersionUID` for `Serializable` classes.

### Fields inherited from class org.apache.ignite.marshaller.AbstractMarshaller

`ctx, DFLT_BUFFER_SIZE`

### Constructor Summary

Constructors
Constructor and Description
`OptimizedMarshaller()` Creates new marshaller will all defaults.
`OptimizedMarshaller(boolean requireSer)` Creates new marshaller providing whether it should require `Serializable` interface or not.

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`static boolean` `available()` Checks whether `GridOptimizedMarshaller` is able to work on the current JVM.
`protected byte[]` `marshal0(Object obj)` Marshals object to byte array.
`protected void` `marshal0(Object obj, OutputStream out)` Marshals object to the output stream.
`void` `onUndeploy(ClassLoader ldr)` Undeployment callback invoked when class loader is being undeployed.
`OptimizedMarshaller` `setIdMapper(OptimizedMarshallerIdMapper mapper)` Sets ID mapper.
`OptimizedMarshaller` `setPoolSize(int poolSize)` Specifies size of cached object streams used by marshaller.
`OptimizedMarshaller` `setRequireSerializable(boolean requireSer)` Sets whether marshaller should require `Serializable` interface or not.
`String` `toString()`
`protected <T> T` `unmarshal0(byte[] arr, ClassLoader clsLdr)` Unmarshals object from byte array using given class loader.
`protected <T> T` `unmarshal0(InputStream in, ClassLoader clsLdr)` Unmarshals object from the input stream using given class loader.

### Methods inherited from class org.apache.ignite.marshaller.AbstractNodeNameAwareMarshaller

`marshal, marshal, nodeName, unmarshal, unmarshal`

### Methods inherited from class org.apache.ignite.marshaller.AbstractMarshaller

`getContext, setContext`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Field Detail

#### USE_DFLT_SUID

    public static final boolean USE_DFLT_SUID

Use default `serialVersionUID` for `Serializable` classes.

### Constructor Detail

#### OptimizedMarshaller

    public OptimizedMarshaller()

Creates new marshaller will all defaults.

Throws:
  `IgniteException` - If this marshaller is not supported on the current JVM.

#### OptimizedMarshaller

    public OptimizedMarshaller(boolean requireSer)

Creates new marshaller providing whether it should require `Serializable` interface or not.

Parameters:
  `requireSer` - Whether to require `Serializable`.

### Method Detail

#### setRequireSerializable

    public OptimizedMarshaller setRequireSerializable(boolean requireSer)

Sets whether marshaller should require `Serializable` interface or not.

Parameters:
  `requireSer` - Whether to require `Serializable`.
Returns:
  `this` for chaining.

#### setIdMapper

    public OptimizedMarshaller setIdMapper(OptimizedMarshallerIdMapper mapper)

Sets ID mapper.

Parameters:
  `mapper` - ID mapper.
Returns:
  `this` for chaining.

#### setPoolSize

    public OptimizedMarshaller setPoolSize(int poolSize)

Specifies size of cached object streams used by marshaller. Object streams are cached for performance reason to avoid costly recreation for every serialization routine. If `0` (default), pool is not used and each thread has its own cached object stream which it keeps reusing.

Since each stream has an internal buffer, creating a stream for each thread can lead to high memory consumption if many large messages are marshalled or unmarshalled concurrently. Consider using pool in this case. This will limit number of streams that can be created and, therefore, decrease memory consumption.

NOTE: Using streams pool can decrease performance since streams will be shared between different threads which will lead to more frequent context switching.

Parameters:
  `poolSize` - Streams pool size. If `0`, pool is not used.
Returns:
  `this` for chaining.

#### marshal0

    protected void marshal0(@Nullable
                            Object obj,
                            OutputStream out)
                     throws IgniteCheckedException

Marshals object to the output stream. This method should not close given output stream.

Specified by:
  `marshal0` in class `AbstractNodeNameAwareMarshaller`
Parameters:
  `obj` - Object to marshal.
  `out` - Output stream to marshal into.
Throws:
  `IgniteCheckedException` - If marshalling failed.

#### marshal0

    protected byte[] marshal0(@Nullable
                              Object obj)
                       throws IgniteCheckedException

Marshals object to byte array.

Specified by:
  `marshal0` in class `AbstractNodeNameAwareMarshaller`
Parameters:
  `obj` - Object to marshal.
Returns:
  Byte array.
Throws:
  `IgniteCheckedException` - If marshalling failed.

#### unmarshal0

    protected <T> T unmarshal0(InputStream in,
                               @Nullable
                               ClassLoader clsLdr)
                        throws IgniteCheckedException

Unmarshals object from the input stream using given class loader. This method should not close given input stream.

Specified by:
  `unmarshal0` in class `AbstractNodeNameAwareMarshaller`
Type Parameters:
  `T` - Type of unmarshalled object.
Parameters:
  `in` - Input stream.
  `clsLdr` - Class loader to use.
Returns:
  Unmarshalled object.
Throws:
  `IgniteCheckedException` - If unmarshalling failed.

#### unmarshal0

    protected <T> T unmarshal0(byte[] arr,
                               @Nullable
                               ClassLoader clsLdr)
                        throws IgniteCheckedException

Unmarshals object from byte array using given class loader.

Specified by:
  `unmarshal0` in class `AbstractNodeNameAwareMarshaller`
Type Parameters:
  `T` - Type of unmarshalled object.
Parameters:
  `arr` - Byte array.
  `clsLdr` - Class loader to use.
Returns:
  Unmarshalled object.
Throws:
  `IgniteCheckedException` - If unmarshalling failed.

#### available

    public static boolean available()

Checks whether `GridOptimizedMarshaller` is able to work on the current JVM.

As long as `GridOptimizedMarshaller` uses JVM-private API, which is not guaranteed to be available on all JVM, this method should be called to ensure marshaller could work properly.

Result of this method is automatically checked in constructor.

Returns:
  `true` if `GridOptimizedMarshaller` can work on the current JVM or `false` if it can't.

#### onUndeploy

    public void onUndeploy(ClassLoader ldr)

Undeployment callback invoked when class loader is being undeployed.

Specified by:
  `onUndeploy` in class `AbstractMarshaller`
Parameters:
  `ldr` - Class loader being undeployed.

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`

${current.year} Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Database and Caching Platform | : ver. 2.7.6
Release Date | : September 11 2019