GridGain™ 4.3.1e
Enterprise "Data Grid" Edition

org.gridgain.grid.marshaller.optimized
Class GridOptimizedMarshaller

java.lang.Object
  extended by org.gridgain.grid.marshaller.GridAbstractMarshaller
      extended by org.gridgain.grid.marshaller.optimized.GridOptimizedMarshaller
All Implemented Interfaces:
GridMarshaller

public class GridOptimizedMarshaller
extends GridAbstractMarshaller

Optimized implementation of GridMarshaller. Unlike GridJdkMarshaller, 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.

GridOptimizedMarshaller 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

 GridOptimizedMarshaller marshaller = new GridOptimizedMarshaller();

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

 GridConfigurationAdapter cfg = new GridConfigurationAdapter();

 // 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.gridgain.grid.GridConfigurationAdapter" singleton="true">
     ...
     <property name="marshaller">
         <bean class="org.gridgain.grid.marshaller.optimized.GridOptimizedMarshaller">
             <property name="requireSerializable">true</property>
         </bean>
     </property>
     ...
 </bean>
 


For information about Spring framework visit www.springframework.org

Injection Example

GridOptimizedMarshaller can be injected in users task, job or SPI as following:
 public class MyGridJob implements GridJob {
     ...
     @GridMarshallerResource
     private GridMarshaller marshaller;
     ...
 }
 
or
 public class MyGridJob implements GridJob {
     ...
     private GridMarshaller marshaller;
     ...
     @GridMarshallerResource
     public void setMarshaller(GridMarshaller marshaller) {
         this.marshaller = marshaller;
     }
     ...
 }
 

 

Field Summary
 
Fields inherited from class org.gridgain.grid.marshaller.GridAbstractMarshaller
DFLT_BUFFER_SIZE
 
Constructor Summary
GridOptimizedMarshaller()
          Initializes marshaller not to enforce Serializable interface.
GridOptimizedMarshaller(boolean requireSer, List<String> clsNames, String clsNamesPath)
          Initializes marshaller with given serialization flag.
 
Method Summary
static boolean available()
          Checks whether GridOptimizedMarshaller is able to work on the current JVM.
 boolean isRequireSerializable()
           
 byte[] marshal(Object obj)
          Marshals object to byte array.
 void marshal(Object obj, OutputStream out)
          Marshals object to the output stream.
 void setClassNames(List<String> clsNames)
          Adds provided class names for marshalling optimization.
 void setClassNamesPath(String path)
          Specifies a name of the file which lists all class names to be optimized.
 void setRequireSerializable(boolean requireSer)
          Sets flag to enforce Serializable interface or not.
<T> T
unmarshal(byte[] arr, ClassLoader clsLdr)
          Unmarshals object from byte array using given class loader.
<T> T
unmarshal(InputStream in, ClassLoader clsLdr)
          Unmarshals object from the output stream using given class loader.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GridOptimizedMarshaller

public GridOptimizedMarshaller()
Initializes marshaller not to enforce Serializable interface.

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

GridOptimizedMarshaller

public GridOptimizedMarshaller(boolean requireSer,
                               @Nullable
                               List<String> clsNames,
                               @Nullable
                               String clsNamesPath)
                        throws GridException
Initializes marshaller with given serialization flag. If true, then objects will be required to implement Serializable in order to be serialize.

Throws:
GridException - If an I/O error occurs while writing stream header.
GridRuntimeException - If this marshaller is not supported on the current JVM.
Parameters:
requireSer - Flag to enforce Serializable interface or not. If true, then objects will be required to implement Serializable in order to be marshalled, if false, then such requirement will be relaxed.
clsNames - User preregistered class names.
clsNamesPath - Path to a file with user preregistered class names.
Method Detail

setClassNames

public void setClassNames(@Nullable
                          List<String> clsNames)
Adds provided class names for marshalling optimization.

NOTE: these collections of classes must be identical on all nodes and in the same order.

Parameters:
clsNames - User preregistered class names to add.

setClassNamesPath

public void setClassNamesPath(@Nullable
                              String path)
                       throws GridException
Specifies a name of the file which lists all class names to be optimized. The file path can either be absolute path, relative to GRIDGAIN_HOME, or specify a resource file on the class path.

The format of the file is class name per line, like this:

 ...
 com.example.Class1
 com.example.Class2
 ...
 

NOTE: this class list must be identical on all nodes and in the same order.

Throws:
GridException - If an error occurs while writing stream header.
Parameters:
path - Path to a file with user preregistered class names.

isRequireSerializable

public boolean isRequireSerializable()
Returns:
Whether to enforce Serializable interface.

setRequireSerializable

public void setRequireSerializable(boolean requireSer)
Sets flag to enforce Serializable interface or not.

Parameters:
requireSer - Flag to enforce Serializable interface or not. If true, then objects will be required to implement Serializable in order to be marshalled, if false, then such requirement will be relaxed.

marshal

public void marshal(@Nullable
                    Object obj,
                    OutputStream out)
             throws GridException
Marshals object to the output stream. This method should not close given output stream.

Throws:
GridException - If marshalling failed.
Parameters:
obj - Object to marshal.
out - Output stream to marshal into.

marshal

public byte[] marshal(@Nullable
                      Object obj)
               throws GridException
Marshals object to byte array.

Specified by:
marshal in interface GridMarshaller
Overrides:
marshal in class GridAbstractMarshaller
Throws:
GridException - If marshalling failed.
Parameters:
obj - Object to marshal.
Returns:
Byte array.

unmarshal

public <T> T unmarshal(InputStream in,
                       @Nullable
                       ClassLoader clsLdr)
            throws GridException
Unmarshals object from the output stream using given class loader. This method should not close given input stream.

Throws:
GridException - If unmarshalling failed.
Type Parameters:
T - Type of unmarshalled object.
Parameters:
in - Input stream.
clsLdr - Class loader to use.
Returns:
Unmarshalled object.

unmarshal

public <T> T unmarshal(byte[] arr,
                       @Nullable
                       ClassLoader clsLdr)
            throws GridException
Unmarshals object from byte array using given class loader.

Specified by:
unmarshal in interface GridMarshaller
Overrides:
unmarshal in class GridAbstractMarshaller
Throws:
GridException - If unmarshalling failed.
Type Parameters:
T - Type of unmarshalled object.
Parameters:
arr - Byte array.
clsLdr - Class loader to use.
Returns:
Unmarshalled object.

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.

GridGain™ 4.3.1e
Enterprise "Data Grid" Edition

GridGain - In-Memory Big Data
Enterprise "Data Grid" Edition, ver. 4.3.1e.10112012
2012 Copyright © GridGain Systems
Follow us:   Follow GridGain on Github Join GridGain User Group Follow GridGain on Twitter Follow GridGain on Vimeo