シリアライズとは、オブジェクトをバイナリ形式にエンコードするプロセスです。これは、ネットワークを介してデータを移動する必要がある場合のCoherenceの操作に不可欠な機能です。Portable Object Format (POF)は言語に依存しないバイナリ形式です。POFは領域と時間の両面で効率を向上させるように設計されており、Coherenceの使用時に不可欠なものとなっています。POFの使用には、パフォーマンス上のメリットから言語の非依存性まで、様々なメリットがあります。Coherenceで作業する際には、シリアライズ・ソリューションとしてPOFを詳細に検討することをお薦めします。
この章では、POFシリアライズの対象とするためにTopLinkアプリケーション・ファイルに加える必要がある変更および追加についてのみ説明します。POFの使用方法および構成の詳細は、Oracle Coherence開発者ガイドのPortable Object Formatの使用方法に関する項を参照してください。
この章の内容は次のとおりです。
エンティティのシリアライズおよびデシリアライズの方法を指定するシリアライズ・ルーチンを実装する必要があります。これは、PortableObjectインタフェースを実装するか、com.tangosol.io.pof.PofSerializerインタフェースを使用してシリアライザを作成することで実行できます。
エンティティ・クラス・ファイルでのPortableObjectインタフェースの実装
com.tangosol.io.pof.PortableObjectインタフェースは、POFデータ・ストリームとの間で自身の状態を自己シリアライズおよびデシリアライズする機能を持つクラスを提供します。このインタフェースを使用するには、必須メソッドreadExternalおよびwriteExternalも実装する必要があります。
例4-1は、PortableObjectインタフェースを実装するエンティティ・クラス・ファイルの例を示しています。必須メソッドのreadExternalおよびwriteExternalも実装されていることに注目してください。
このクラスには、TradeオブジェクトとSecurityオブジェクトとの関連マッピングを定義する@OneToOne注釈が含まれていることにも注意してください。TopLinkでは、JPA仕様によって定義されている関連マッピング(1対1、1対多、多対多および多対多)がすべてサポートされています。これらの関連は、注釈として表すことができます。関連マッピングはシリアライズまたはデシリアライズしないでください。これらの操作はTopLink Gridによって自動的に実行されます。
例4-1 PortableObjectを実装するエンティティ・クラスの例
package oracle.toplinkgrid.codesample.pof.models.trader; import java.io.IOException; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id;import javax.persistence.OneToOne;import com.tangosol.io.pof.PofReader;import com.tangosol.io.pof.PofWriter;import com.tangosol.io.pof.PortableObject;/** * This class will not be stored within Coherence as Trades are not high * throughput objects in this model. * */ @Entitypublic class Trade implements Serializable, PortableObject{ /** * */ private static final long serialVersionUID = -244532585419336780L; @Id @GeneratedValue protected long id;@OneToOne(fetch=FetchType.EAGER)protected Security security; protected int quantity; protected double amount; public long getId() { return id; } public void setId(long id) { this.id = id; } public Security getSecurity() { return security; } public void setSecurity(Security security) { this.security = security; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; }public void readExternal(PofReader pofreader) throws IOException{ id = pofreader.readLong(0); quantity = pofreader.readInt(2); amount = pofreader.readDouble(3); }public void writeExternal(PofWriter pofwriter) throws IOException{ pofwriter.writeLong(0, id); pofwriter.writeInt(2, quantity); pofwriter.writeDouble(3, amount); } }
エンティティに対するPOFSerializerの作成
PortableObjectインタフェースを実装するかわりに、com.tangosol.io.pof.PofSerializerインタフェースを実装して独自のシリアライザおよびデシリアライザを作成できます。このインタフェースには、シリアライズ・ロジックを、シリアライズするエンティティから外部化する手段が用意されています。これは、CoherenceでPOFを使用する場合に、クラスの構造を変更したくないときには特に役立ちます。POFSerializerインタフェースは、次のメソッドを提供します。
public Object deserialize(PofReader in)
public void serialize(PofWriter out, Object o)
キャッシュ構成ファイルで、操作するエンティティに対応するキャッシュ・マッピングを作成します。シリアライザ(com.tangosol.io.pof.ConfigurablePofContextなど)およびPOF構成ファイルpof-config.xmlを指定します。EclipseLinkキャッシュ・ストア(oracle.eclipselink.coherence.integrated.EclipseLinkJPACacheStoreなど)を<cachestore-scheme>属性内に指定します。
例4-2 キャッシュ構成ファイルの例
<?xml version="1.0"?>
<cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config http://xmlns.oracle.com/coherence/coherence-cache-config/1.0/coherence-cache-config.xsd">
<caching-scheme-mapping>
<cache-mapping>
<cache-name>ATTORNEY_JPA_CACHE</cache-name>
<scheme-name>eclipselink-jpa-distributed</scheme-name>
</cache-mapping>
<cache-mapping>
<cache-name>CONTACT_JPA_CACHE</cache-name>
<scheme-name>eclipselink-jpa-distributed-load</scheme-name>
</cache-mapping>
...
additional cache mappings
...
<caching-schemes>
<distributed-scheme>
<scheme-name>eclipselink-jpa-distributed-load</scheme-name>
<service-name>EclipseLinkJPA</service-name>
<serializer>
<instance>
<class-name>com.tangosol.io.pof.ConfigurablePofContext</class-name>
<init-params>
<init-param>
<param-type>String</param-type>
<param-value>trader-pof-config.xml</param-value>
</init-param>
</init-params>
</instance>
</serializer>
<backing-map-scheme>
<read-write-backing-map-scheme>
<internal-cache-scheme>
<local-scheme/>
</internal-cache-scheme>
</read-write-backing-map-scheme>
</backing-map-scheme>
<autostart>true</autostart>
</distributed-scheme>
<distributed-scheme>
<scheme-name>eclipselink-jpa-distributed</scheme-name>
<service-name>EclipseLinkJPA</service-name>
<serializer>
<instance>
<class-name>com.tangosol.io.pof.ConfigurablePofContext</class-name>
<init-params>
<init-param>
<param-type>String</param-type>
<param-value>trader-pof-config.xml</param-value>
</init-param>
</init-params>
</instance>
</serializer>
<backing-map-scheme>
<read-write-backing-map-scheme>
<internal-cache-scheme>
<local-scheme/>
</internal-cache-scheme>
<!-- Define the cache scheme -->
<cachestore-scheme>
<class-scheme>
<class-name>oracle.eclipselink.coherence.integrated.EclipseLinkJPACacheStore</class-name>
<init-params>
<init-param>
<param-type>java.lang.String</param-type>
<param-value>{cache-name}</param-value>
</init-param>
<init-param>
<param-type>java.lang.String</param-type>
<param-value>coherence-pu</param-value>
</init-param>
</init-params>
</class-scheme>
</cachestore-scheme>
</read-write-backing-map-scheme>
</backing-map-scheme>
<autostart>true</autostart>
</distributed-scheme>
</caching-schemes>
</cache-config>
POFシリアライズの対象とするエンティティ・クラスを指定するファイルを定義します。Coherenceにはデフォルトで、pof-config.xmlという名前のPOF構成ファイルが用意されています。このファイルを使用して、エンティティ・クラスにtype-idを割り当てます。
POF構成ファイルには、次のクラスに対するtype-idエントリも含める必要があります。
oracle.eclipselink.coherence.integrated.internal.cache.WrapperInternal: このインタフェースは、エンティティ・ラッパーの内部属性にアクセスするために使用されます。
oracle.eclipselink.coherence.integrated.cache.WrapperPofSerializer: 関連付けられたシリアライザ。このクラスは、Coherenceキャッシュに直接アクセスする場合に、Coherence内でエンティティ・ラッパーに対するシリアライズ・サポートを提供するために使用されます。これには、カスタム値エクストラクタを持つユーザーが含まれます。
oracle.eclipselink.coherence.integrated.internal.querying.EclipseLinkExtractor: このインタフェースはCoherence POFシリアライズで、EclipseLinkエクストラクタをシリアライズ用にマークするために使用されます。フィルタに対するTopLink Gridエンティティから値を抽出します。
oracle.eclipselink.coherence.integrated.cache.ExtractorSerializer: 関連付けられたシリアライザ。このクラスは、Coherenceキャッシュに直接アクセスする場合に、Coherence内でエンティティ・ラッパーに対するシリアライズ・サポートを提供するために使用されます。これには、カスタム値エクストラクタを持つユーザーが含まれます。
oracle.eclipselink.coherence.integrated.internal.cache.VersionPutProcessor: グリッドに対するオプティミスティック・ロック対応の更新に使用される内部ファイル。
oracle.eclipselink.coherence.integrated.internal.cache.VersionRemoveProcessor: グリッドからのオプティミスティック・ロック対応の削除に使用される内部ファイル。
oracle.eclipselink.coherence.integrated.internal.cache.RelationshipUpdateProcessor: グリッドに遅延ロードされた関連データを更新するために使用される内部ファイル。
oracle.eclipselink.coherence.integrated.internal.querying.EclipseLinkFilterFactory$SubClassOf: 内部ファイル。これは、エンティティ・タイプに基づいてフィルタするフィルタ拡張です。多相問合せからスーパークラスを取り除きます。
例4-3は、TopLink Gridサポート・ファイルを含むPOF構成ファイルの例を示しています。
例4-3 POF構成ファイルの例
<?xml version="1.0"?>
<pof-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-pof-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-pof-config http://xmlns.oracle.com/coherence/coherence-pof-config/1.0/coherence-pof-config.xsd">
<user-type-list>
<!-- include all "standard" Coherence POF user types -->
<include>coherence-pof-config.xml</include>
<user-type>
<type-id>1163</type-id>
<class-name>oracle.toplinkgrid.codesample.pof.models.trader.Attorney</class-name>
</user-type>
...
additional type IDs for Entity classes
...
<user-type>
<type-id>1144</type-id>
<class-name>oracle.eclipselink.coherence.integrated.internal.cache.WrapperInternal</class-name>
<serializer>
<class-name>oracle.eclipselink.coherence.integrated.cache.WrapperPofSerializer</class-name>
</serializer>
</user-type>
<user-type>
<type-id>1142</type-id>
<class-name>oracle.eclipselink.coherence.integrated.internal.querying.EclipseLinkExtractor</class-name>
<serializer>
<class-name>oracle.eclipselink.coherence.integrated.cache.ExtractorSerializer</class-name>
</serializer>
</user-type>
<user-type>
<type-id>1141</type-id>
<class-name>oracle.eclipselink.coherence.integrated.internal.cache.VersionPutProcessor</class-name>
</user-type>
<user-type>
<type-id>1140</type-id>
<class-name>oracle.eclipselink.coherence.integrated.internal.cache.VersionRemoveProcessor</class-name>
</user-type>
<user-type>
<type-id>1139</type-id>
<class-name>oracle.eclipselink.coherence.integrated.internal.cache.RelationshipUpdateProcessor</class-name>
</user-type>
<user-type>
<type-id>1138</type-id>
<class-name>oracle.eclipselink.coherence.integrated.internal.querying.EclipseLinkFilterFactory$SubClassOf</class-name>
</user-type>
</user-type-list>
<allow-interfaces>true</allow-interfaces>
<allow-subclasses>true</allow-subclasses>
</pof-config>