Composite serialization
From JPPF 6.2 Documentation
Main Page > Customizing JPPF > Composite serialization |
It is also possible to to use a special type of serialization scheme that will be applied on top of the serialization scheme defined in the previous section. This allows you to perform any kind of transformation to the data provided by the serialization, such as compression / decompression or encryption / decryption.
To implement such a transformation, you will need to create a class which extends the abstract class JPPFCompositeSerialization, defined as follows:
public abstract class JPPFCompositeSerialization implements JPPFSerialization { // Get the concrete serialization to delegate to public final JPPFSerialization getDelegate() // Get the case-insensitive unique name given to this composite serialization public abstract String getName(); }
This design makes it is possible to define a chain of transformations on top of a concrete JPPFSerialization. To this effect, the getDelegate() method provides a reference to the next transformation or serialization in the chain.
The getName() method assigns a unique, case-insensitive name to the defined transformation. If two transformations have the same name, the last one will override the first, which will then be unavailable.
For instance, the predefined ZLIBSerialization is implemented like this:
public class ZLIBSerialization extends JPPFCompositeSerialization { @Override public void serialize(Object o, OutputStream os) throws Exception { Deflater deflater = new Deflater(); DeflaterOutputStream zlibos = new DeflaterOutputStream(os, deflater); try { // delegate to the next (composite) serialization in the chain getDelegate().serialize(o, zlibos); } finally { zlibos.finish(); deflater.end(); // required to clear the native/JNI buffers } } @Override public Object deserialize(InputStream is) throws Exception { Inflater inflater = new Inflater(); InflaterInputStream zlibis = new InflaterInputStream(is, inflater); try { // delegate to the next (composite) deserialization in the chain return getDelegate().deserialize(zlibis); } finally { inflater.end(); // required to clear the native/JNI buffers } } @Override public String getName() { return "ZLIB"; } }
Composite serializations are made available to JPPF via the service provider interface (SPI):
- in your source folder create the file META-INF/services/org.jppf.serialization.JPPFCompositeSerialization
- in this file add, for each of your composite serialization implementations, its fully qualified class name, for instance:
com.my.company.MyCompositeSerialization
- ensure that the code of your implementations is added to the classpath of the JPPF nodes, drivers and clients
Note 1: Each concrete subclass of JPPFCompositeSerialization must have an implicit or explicit no-args constructor
Note 2: All JPPF built-in serializations are defined with the very same mechanism
To use one or more composite serializations, you need to specify them as a space-separated list in the value of the "jppf.object.serialization.class" property, in this format:
jppf.object.serialization.class = name1 ... nameN serialization_class_name
where:
- each nameX is the name assigned to a composite serialization
- serialization_class_name is the fully qualified class name of a serialization scheme as seen in the previous section.
For instance, to use ZLIB compression on top of the predefined DefaultJPPFSerialization, we would configure it as:
# ZLIB compression on top of JPPF serialization jppf.object.serialization.class = ZLIB org.jppf.serialization.DefaultJPPFSerialization
Built-in composite serializations:
- ZLIBSerialization (name "ZLIB"): provides zlib compression and decompression based on the java.util.zip.* APIs
- LZ4Serialization (name "LZ4"): also provides compression and decompression, using the LZ4 library. It is much faster than ZLIB but has a smaller compression rate.
Related sample:
The DataEncryption sample uses a composite serialization implementation to encrypt, via symetric encryption, the objects transported over the network by JPPF.
Main Page > Customizing JPPF > Composite serialization |