IT技术及科技前沿

中文IT博客,为IT专业技术人员提供最全面的信息传播和服务

首页 新随笔 订阅 管理

 

Overview

Serialization is the process of encoding an object into a binary format. It is a critical component to working with Coherence as data needs to be moved around the network. The Portable Object Format (also referred to as POF) is a language agnostic binary format. POF was designed to be incredibly efficient in both space and time and has become a cornerstone element in working with Coherence.

Why should you use POF

There are several options available with respect to serialization including standard Java serialization, POF, and your own custom serialization routines. Each has their own tradeoffs. Standard Java serialization is easy to implement, supports cyclic object graphs and preserves object identity. Unfortunately, it's also comparatively slow, has a verbose binary format, and restricted to only Java objects.

The Portable Object Format on the other hand has the following advantages:

  • It's language independent with current support for Java, .NET, and C++.
  • It's very efficient, in a simple test class with a String, a long, and three ints, (de)serialization was seven times faster, and the binary produced was one sixth the size compared with standard Java serialization.
  • It's versionable, objects can evolve and have forward and backward compatibility.
  • It supports the ability to externalize your serialization logic.
  • It's indexed which allows for extracting values without deserializing the whole object (see PofExtractors and PofUpdaters).

Note that if your object model requires cyclic object graphs, POF does not currently support this.

Working with POF

POF requires you to implement serialization routines that know how to serialize and deserialize your objects. There are two ways to do this:

Implementing the PortableObject interface

The PortableObject interface is a simple interface made up of two methods:

  • public void readExternal(PofReader reader)
  • public void writeExternal(PofWriter writer)

As mentioned above, POF elements are indexed. This is accomplished by providing a numerical index for each element that you write or read from the POF stream. It's important to keep in mind that the indexes must be unique to each element written and read from the POF stream, especially when you have derived types involved because the indexes must be unique between the super class and the derived class.

Below is a simple example of implementing the PortableObject interface:

public void readExternal(PofReader in)         throws IOException     {    m_symbol    = (Symbol) in.readObject(0);    m_ldtPlaced = in.readLong(1);    m_fClosed   = in.readBoolean(2);    }public void writeExternal(PofWriter out)         throws IOException     {    out.writeObject(0, m_symbol);    out.writeLong(1, m_ldtPlaced);    out.writeBoolean(2, m_fClosed);    }

Implementing the PofSerializer interface:

PofSerializers provide you with a way to externalize your serialization logic from the classes you want to serialize. This is particularly useful when you don't want to change the structure of your classes to work with POF and Coherence. The PofSerializer interface is also made up of two methods:

  • public Object deserializer(PofReader in)
  • public void writeObject(PofWriter out, Object o)

As with the PortableObject interface, all elements written to or read from the POF stream must be uniquely indexed. Below is an example implementation of the PofSerializer interface:

public Object deserialize(PofReader in)         throws IOException     {    Symbol symbol    = (Symbol)in.readObject(0);    long   ldtPlaced = in.readLong(1);    bool   fClosed   = in.readBoolean(2);        // mark that we're done reading the object    in.readRemainder(null);    return new Trade(symbol, ldtPlaced, fClosed);    }public void serialize(PofWriter out, Object o)         throws IOException     {    Trade trade = (Trade) o;    out.writeObject(0, trade.getSymbol());    out.writeLong(1, trade.getTimePlaced());    out.writeBoolean(2, trade.isClosed());        // mark that we're done writing the object    out.writeRemainder(null);    }

Assigning POF indexes

When assigning POF indexes to your object's attributes, it's important to keep a few things in mind:

  • Order your reads and writes - You should start with the lowest index value in your serialization routine and finish with the highest. When deserializing a value, you want to read in the same order you've written.
  • It's ok to have non-contiguous indexes but you must read/write sequentially.
  • When Subclassing reserve index ranges - Index's are cumulative across derived types. As such, each derived type must be aware of the POF index range reserved by its super class.
  • Don't re-purpose your indexes - If you ever want Evolvable support, it's imperative that you don't re-purpose the indexes of your attributes across class revisions.
  • Label your indexes - If you label your indexes with a public static final int, it will be much easier to work with the object, especially when using PofExtractors and PofUpdaters. Note that once you've labeled your indexes, you want to still make sure they're read and written out in order as mentioned above.

The ConfigurablePofContext

Coherence provides a ConfigurablePofContext class which is responsible for mapping a POF serialized object to an appropriate serialization routine (either a PofSerializer or by calling through the PortableObject interface). Each class is given a unique type-id in POF that can be mapped to an optional PofSerializer. Once your classes have serialization routines, they must be registered with the ConfigurablePofContext. Custom user types are registered with the ConfigurablePofContext using a POF configuration file. This is an XML file which has a <user-type-list> element that contains a list of classes that implement PortableObject or have a PofSerializer associated with them. The type-id for each class must be unique, and must match across all cluster instances (including extend clients).

The following is an example of what a pof-config.xml file would look like:

<pof-config>  <user-type-list>    <include>coherence-pof-config.xml</include>    ...    <!-- User types must be above 1000 -->    <user-type>      <type-id>1001</type-id>      <class-name>com.examples.MyTrade</class-name>      <serializer>        <class-name>com.examples.MyTradeSerializer</class-name>      </serializer>    </user-type>    <user-type>      <type-id>1002</type-id>      <class-name>com.examples.MyPortableTrade</class-name>    </user-type>  ...</pof-config>
NOTE
Coherence reserves the first 1000 type-id's for internal use. If you look closely you'll see that the user-type-list includes the coherence-pof-config.xml file. This is where the Coherence specific user types are defined and should be included in all of your POF configuration files.

Configuring Coherence to use the ConfigurablePofContext

In order to start using POF, you must also configure each service to use the ConfigurablePofContext. This is accomplished using the <serializer> element of your cache scheme in your cache configuration file. The ConfigurablePofContext takes a string based <init-param> that points to your pof-configuration file.

Below is an example of a distributed cache scheme configured to use POF:

 <distributed-scheme>    <scheme-name>example-distributed</scheme-name>    <service-name>DistributedCache</service-name>    <serializer>      <class-name>com.tangosol.io.pof.ConfigurablePofContext</class-name>      <init-params>        <init-param>          <param-value>my-pof-config.xml</param-value>          <param-type>String</param-type>        </init-param>      </init-params>    </serializer>    ... </distributed-scheme>

Alternatively you can configure an entire JVM instance to use POF using the following system properties:

  • tangosol.pof.enabled=true - This will turn on POF for the entire JVM instance.
  • tangosol.pof.config="CONFIG_FILE_PATH" - The path to the POF configuration file you want to use. Note that if this is not in the class path it must be presented as a file resource (for example file:///opt/home/coherence/mycustom-pof-config.xml).

Summary

Using POF has many advantages ranging from performance benefits to language independence. It's recommended that you look closely at POF as your serialization solution when working with Coherence. For information on how to work with POF in .NET see the Configuration and Usage for .NET Clients section of the .NET documentation. For information on how to work with POF in C++ see the Integrating user data types of the C++ documentation.

posted on 2010-12-27 11:14  孟和2012  阅读(286)  评论(0编辑  收藏  举报