LogSequence オブジェクトおよび LogElement オブジェクトはピア JVM 間で変更を配布し、ObjectGridEventListener プラグインを使用して、eXtreme Scale トランザクションで発生した変更を通信します。
Java Message Service (JMS) を使用してトランザクションの変更を配布する方法について詳しくは、トランザクションの配布を参照してください。
ObjectGrid インスタンスが ObjectGridManager によりキャッシュされていることが前提条件です。詳しくは、createObjectGrid メソッドを参照してください。cacheInstance ブール値は、true に設定する必要があります。
このメカニズムを実装する必要はありません。この機能を使用するためのピアツーピア・レプリカ生成メカニズムが組み込まれています。JMS を使用したピアツーピア・レプリカ生成の構成を参照してください。
オブジェクトは、アプリケーションがメッセージ・トランスポートを使用して、ObjectGrid で発生した変更を リモート Java 仮想マシン 内のピア ObjectGrids に簡単にパブリッシュし、それらの変更をその JVM 上で適用するための手段を提供します。LogSequenceTransformer クラスは、このサポートを使用可能にするために重要です。ここでは、メッセージを伝搬するために Java Message Service (JMS) メッセージング・システムを使用するリスナーをどのように作成すればいいのかを 詳しく見ていきます。eXtreme Scale トランザクション のコミットが WebSphere® Application Server クラスターの 複数メンバーにわたって実行された結果として生じる LogSequence の伝送を、eXtreme Scale は IBM 提供のプラグインによってサポートします。この機能はデフォルトでは使用不可ですが、作動可能に 構成することができます。ただし、コンシューマーまたはプロデューサーのいずれかが WebSphere Application Server ではない 場合、外部 JMS メッセージング・システムが必要となる可能性があります。
LogSequenceTransformer クラス、ObjectGridEventListener、 LogSequence および LogElement API により、信頼性のあるパブリッシュおよびサブスクライブを使用して、変更内容を配布し、配布するマップをフィルターに掛けることができます。このトピックのスニペットは、これらの API を JMS とともに使用して、ピアツーピア ObjectGrid をビルドする方法を示します。これは、共通メッセージ・トランスポートを共有するさまざまなプラットフォームのセットでホストされるアプリケーションによって共有されます。
プラグインの初期化
ObjectGrid が開始するときに、ObjectGrid は、 ObjectGridEventListener インターフェース契約の一部である、プラグインの initialize メソッドを呼び出します。 initialize メソッドは、接続、セッション、およびパブリッシャーを含む JMS リソースを取得し、JMS リスナーであるスレッドを開始する必要があります。
以下の例は初期化メソッドを示しています。
initialize method example
public void initialize(Session session) {
mySession = session;
myGrid = session.getObjectGrid();
try {
if (mode == null) {
throw new ObjectGridRuntimeException("No mode specified");
}
if (userid != null) {
connection = topicConnectionFactory.createTopicConnection(userid,
password);
} else
connection = topicConnectionFactory.createTopicConnection();
// need to start the connection to receive messages.
connection.start();
// the jms session is not transactional (false).
jmsSession = connection.createTopicSession(false,
javax.jms.Session.AUTO_ACKNOWLEDGE);
if (topic == null)
if (topicName == null) {
throw new ObjectGridRuntimeException("Topic not specified");
} else {
topic = jmsSession.createTopic(topicName);
}
publisher = jmsSession.createPublisher(topic);
// start the listener thread.
listenerRunning = true;
listenerThread = new Thread(this);
listenerThread.start();
} catch (Throwable e) {
throw new ObjectGridRuntimeException("Cannot initialize", e);
}
}
スレッドを開始するためのコードは、Java 2 Platform, Standard Edition (Java SE) スレッドを使用します。WebSphere Application Server バージョン 6.x または WebSphere Application Server バージョン 5.x エンタープライズ・サーバーを実行している場合、非同期 Bean アプリケーション・プログラミング・インターフェース (API) を 使用して、このデーモン・スレッドを開始します。共通 API を使用することもできます。以下に、 作業マネージャーを使用する同じアクションを示す置換の断片の例を示します。
// start the listener thread.
listenerRunning = true;
workManager.startWork(this, true);
また、プラグインは、実行可能なインターフェースの代わりに、 作業インターフェースを実装する必要があります。また、 リリース・メソッドを追加して、listenerRunning 変数を false に設定する必要があります。プラグインは、 コンストラクター、または Inversion of Control (IoC) コンテナーを使用している場合は注入によって、 WorkManager インスタンスに提供されている必要があります。
変更の送信
以下は、ObjectGrid 上で行われるローカル変更をパブリッシュするための サンプル transactionEnd メソッドです。このサンプルでは JMS を使用していますが、信頼できるパブリッシュおよび サブスクライブ・メッセージングの機能を持つ任意のメッセージ・トランスポートを使用することもできます。
transactionEnd method example
// This method is synchronized to make sure the
// messages are published in the order the transaction
// were committed. If we started publishing the messages
// in parallel then the receivers could corrupt the Map
// as deletes may arrive before inserts etc.
public synchronized void transactionEnd(String txid, boolean isWriteThroughEnabled,
boolean committed,
Collection changes) {
try {
// must be write through and commited.
if (isWriteThroughEnabled && committed) {
// write the sequences to a byte []
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
if (publishMaps.isEmpty()) {
// serialize the whole collection
LogSequenceTransformer.serialize(changes, oos, this, mode);
} else {
// filter LogSequences based on publishMaps contents
Collection publishChanges = new ArrayList();
Iterator iter = changes.iterator();
while (iter.hasNext()) {
LogSequence ls = (LogSequence) iter.next();
if (publishMaps.contains(ls.getMapName())) {
publishChanges.add(ls);
}
}
LogSequenceTransformer.serialize(publishChanges, oos, this, mode);
}
// make an object message for the changes
oos.flush();
ObjectMessage om = jmsSession.createObjectMessage(bos.toByteArray());
// set properties
om.setStringProperty(PROP_TX, txid);
om.setStringProperty(PROP_GRIDNAME, myGrid.getName());
// transmit it.
publisher.publish(om);
}
} catch (Throwable e) {
throw new ObjectGridRuntimeException("Cannot push changes", e);
}
}
更新メッセージの受信および適用
以下は実行メソッドです。 このメソッドは、アプリケーションがループを停止するまで、ループ内で実行します。 各ループの反復は、JMS メッセージの受信、およびメッセージの ObjectGrid への適用を試行します。
JMS message run method example
private synchronized boolean isListenerRunning() {
return listenerRunning;
}
public void run() {
try {
System.out.println("Listener starting");
// get a jms session for receiving the messages.
// Non transactional.
TopicSession myTopicSession;
myTopicSession = connection.createTopicSession(false, javax.jms.
Session.AUTO_ACKNOWLEDGE);
// get a subscriber for the topic, true indicates don't receive
// messages transmitted using publishers
// on this connection. Otherwise, we'd receive our own updates.
TopicSubscriber subscriber = myTopicSession.createSubscriber(topic,
null, true);
System.out.println("Listener started");
while (isListenerRunning()) {
ObjectMessage om = (ObjectMessage) subscriber.receive(2000);
if (om != null) {
// Use Session that was passed in on the initialize...
// very important to use no write through here
mySession.beginNoWriteThrough();
byte[] raw = (byte[]) om.getObject();
ByteArrayInputStream bis = new ByteArrayInputStream(raw);
ObjectInputStream ois = new ObjectInputStream(bis);
// inflate the LogSequences
Collection collection = LogSequenceTransformer.inflate(ois,
myGrid);
Iterator iter = collection.iterator();
while (iter.hasNext()) {
// process each Maps changes according to the mode when
// the LogSequence was serialized
LogSequence seq = (LogSequence) iter.next();
mySession.processLogSequence(seq);
}
mySession.commit();
} // if there was a message
} // while loop
// stop the connection
connection.close();
} catch (IOException e) {
System.out.println("IO Exception: " + e);
} catch (JMSException e) {
System.out.println("JMS Exception: " + e);
} catch (ObjectGridException e) {
System.out.println("ObjectGrid exception: " + e);
System.out.println("Caused by: " + e.getCause());
} catch (Throwable e) {
System.out.println("Exception : " + e);
}
System.out.println("Listener stopped");
}