Skip Headers
Oracle® Coherence Getting Started Guide
Release 3.5

Part Number E14510-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

24 The Portable Object Format

The following sections are included in this chapter:

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. For more information on the POF binary stream, see "The PIF-POF Binary Format" in Oracle Coherence Developer's Guide.

Why Should I 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 trade-offs. 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:

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.

Example 24-1 is a simple example of implementing the PortableObject interface:

Example 24-1 Implementation of 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:

The PofSerializer interface 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:

Example 24-2 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. See Chapter 25, "PofExtractors and PofUpdaters." Note that once you've labeled your indexes, you want to still make sure that they are 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 "Configuration and Usage for .NET Clients" in Oracle Coherence Client Guide. For information on how to work with POF in C++, see "Configuration and Usage for C++ Clients" in Oracle Coherence Client Guide.