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 ofMarshaller. UnlikeJdkMarshaller, which is based on standardObjectOutputStream, this marshaller does not enforce that all serialized objects implementSerializableinterface. It is also about 20 times faster as it removes lots of serialization overhead that exists in default JDK implementation.OptimizedMarshalleris 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 Modifier and Type Field Description static booleanUSE_DFLT_SUIDUse defaultserialVersionUIDforSerializableclasses.-
Fields inherited from class org.apache.ignite.marshaller.AbstractMarshaller
ctx, DFLT_BUFFER_SIZE
-
-
Constructor Summary
Constructors Constructor Description OptimizedMarshaller()Creates new marshaller will all defaults.OptimizedMarshaller(boolean requireSer)Creates new marshaller providing whether it should requireSerializableinterface or not.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static booleanavailable()Checks whetherGridOptimizedMarshalleris able to work on the current JVM.voidclearClassDescriptorsCache()Clears the optimized class descriptors cache.protected byte[]marshal0(@Nullable Object obj)Marshals object to byte array.protected voidmarshal0(@Nullable Object obj, OutputStream out)Marshals object to the output stream.voidonUndeploy(ClassLoader ldr)Undeployment callback invoked when class loader is being undeployed.OptimizedMarshallersetIdMapper(OptimizedMarshallerIdMapper mapper)Sets ID mapper.OptimizedMarshallersetPoolSize(int poolSize)Specifies size of cached object streams used by marshaller.OptimizedMarshallersetRequireSerializable(boolean requireSer)Sets whether marshaller should requireSerializableinterface or not.StringtoString()protected <T> Tunmarshal0(byte[] arr, @Nullable ClassLoader clsLdr)Unmarshals object from byte array using given class loader.protected <T> Tunmarshal0(InputStream in, @Nullable ClassLoader clsLdr)Unmarshals object from the input stream using given class loader.protected <T> Tunmarshal0(InputStream in, @Nullable ClassLoader clsLdr, boolean useCache)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
-
-
-
-
Field Detail
-
USE_DFLT_SUID
public static final boolean USE_DFLT_SUID
Use defaultserialVersionUIDforSerializableclasses.
-
-
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 requireSerializableinterface or not.- Parameters:
requireSer- Whether to requireSerializable.
-
-
Method Detail
-
setRequireSerializable
public OptimizedMarshaller setRequireSerializable(boolean requireSer)
Sets whether marshaller should requireSerializableinterface or not.- Parameters:
requireSer- Whether to requireSerializable.- Returns:
thisfor chaining.
-
setIdMapper
public OptimizedMarshaller setIdMapper(OptimizedMarshallerIdMapper mapper)
Sets ID mapper.- Parameters:
mapper- ID mapper.- Returns:
thisfor 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. If0(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. If0, pool is not used.- Returns:
thisfor chaining.
-
marshal0
protected void marshal0(@Nullable @Nullable Object obj, OutputStream out) throws IgniteCheckedExceptionMarshals object to the output stream. This method should not close given output stream.- Specified by:
marshal0in classAbstractNodeNameAwareMarshaller- Parameters:
obj- Object to marshal.nullobject will be marshaled to binarynullrepresentation.out- Output stream to marshal into.- Throws:
IgniteCheckedException- If marshalling failed.
-
marshal0
protected byte[] marshal0(@Nullable @Nullable Object obj) throws IgniteCheckedExceptionMarshals object to byte array.- Specified by:
marshal0in classAbstractNodeNameAwareMarshaller- Parameters:
obj- Object to marshal.nullobject will be marshaled to binarynullrepresentation.- Returns:
- Byte array.
- Throws:
IgniteCheckedException- If marshalling failed.
-
unmarshal0
protected <T> T unmarshal0(InputStream in, @Nullable @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:
unmarshal0in classAbstractNodeNameAwareMarshaller- Type Parameters:
T- Type of unmarshalled object.- Parameters:
in- Input stream.clsLdr- If notnullthen given class loader will be used for unmarshal object.- Returns:
- Unmarshalled object.
- Throws:
IgniteCheckedException- If unmarshalling failed.
-
unmarshal0
protected <T> T unmarshal0(InputStream in, @Nullable @Nullable ClassLoader clsLdr, boolean useCache) throws IgniteCheckedException
Unmarshals object from the input stream using given class loader. This method should not close given input stream.- Type Parameters:
T- Type of unmarshalled object.- Parameters:
in- Input stream.clsLdr- Class loader to use.useCache- True if class loader cache will be used, false otherwise.- Returns:
- Unmarshalled object.
- Throws:
IgniteCheckedException- If unmarshalling failed.
-
unmarshal0
protected <T> T unmarshal0(byte[] arr, @Nullable @Nullable ClassLoader clsLdr) throws IgniteCheckedExceptionUnmarshals object from byte array using given class loader.- Specified by:
unmarshal0in classAbstractNodeNameAwareMarshaller- Type Parameters:
T- Type of unmarshalled object.- Parameters:
arr- Byte array.clsLdr- If notnullthen given class loader will be used for unmarshal object.- Returns:
- Unmarshalled object.
- Throws:
IgniteCheckedException- If unmarshalling failed.
-
available
public static boolean available()
Checks whetherGridOptimizedMarshalleris able to work on the current JVM.As long as
GridOptimizedMarshalleruses 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:
trueifGridOptimizedMarshallercan work on the current JVM orfalseif it can't.
-
onUndeploy
public void onUndeploy(ClassLoader ldr)
Undeployment callback invoked when class loader is being undeployed.- Specified by:
onUndeployin classAbstractMarshaller- Parameters:
ldr- Class loader being undeployed.
-
clearClassDescriptorsCache
public void clearClassDescriptorsCache()
Clears the optimized class descriptors cache. This is essential for the clients on disconnect in order to make them register their user types again (server nodes may lose previously registered types).
-
-