これらのメソッドはそれぞれ、ObjectGrid のローカル・インスタンスを 1 つ 作成します。
以下のコード・スニペット は、eXtreme Scale でローカル ObjectGrid インスタンス を取得および構成する方法を示しています。
// Obtain a local ObjectGrid reference
// you can create a new ObjectGrid, or get configured ObjectGrid
// defined in ObjectGrid xml file
ObjectGridManager objectGridManager =
ObjectGridManagerFactory.getObjectGridManager();
ObjectGrid ivObjectGrid =
objectGridManager.createObjectGrid("objectgridName");
// Add a TransactionCallback into ObjectGrid
HeapTransactionCallback tcb = new HeapTransactionCallback();
ivObjectGrid.setTransactionCallback(tcb);
// Define a BackingMap
// if the BackingMap is configured in ObjectGrid xml
// file, you can just get it.
BackingMap ivBackingMap = ivObjectGrid.defineMap("myMap");
// Add a Loader into BackingMap
Loader ivLoader = new HeapCacheLoader();
ivBackingMap.setLoader(ivLoader);
// initialize ObjectGrid
ivObjectGrid.initialize();
// Obtain a session to be used by the current thread.
// Session can not be shared by multiple threads
Session ivSession = ivObjectGrid.getSession();
// Obtaining ObjectMap from ObjectGrid Session
ObjectMap objectMap = ivSession.getMap("myMap");
以下のコードは、ObjectGrid を作成して多くのユーザー間で共有する単純なケースです。
import com.ibm.websphere.objectgrid.ObjectGrid;
import com.ibm.websphere.objectgrid.ObjectGridException;
import com.ibm.websphere.objectgrid.ObjectGridManagerFactory;
import com.ibm.websphere.objectgrid.ObjectGridManager;
final ObjectGridManager oGridManager=
ObjectGridManagerFactory.getObjectGridManager();
ObjectGrid employees =
oGridManager.createObjectGrid("Employees",true);
employees.initialize();
employees.
/*sample continues..*/
前の Java コード・スニペットは、Employees ObjectGrid を 作成し、キャッシュに入れます。Employees ObjectGrid は、デフォルト構成によって初期化され、すぐに使用できる状態になっています。createObjectGrid メソッド内の 2 番目のパラメーターは、true に設定されます。 これにより、ObjectGridManager は、作成した ObjectGrid インスタンスをキャッシュに入れるよう指示されます。 このパラメーターが false に設定されている場合、インスタンスはキャッシュに入れられません。 各 ObjectGrid インスタンスには name があり、その名前に基づき、多くのクライアントまたはユーザー間でそのインスタンスを共有できます。
objectGrid インスタンスがピアツーピア共有で使用されている場合は、キャッシングを true に設定する必要があります。ピアツーピア共有について詳しくは、『ピア Java 仮想マシン間の変更の配布』を参照してください。
WebSphere® eXtreme Scale は高度な構成が可能です。 前の例では、構成を伴わない単純な ObjectGrid を作成する方法を示しました。この例では、XML 構成ファイルに基づいて事前構成された ObjectGrid インスタンスを作成する方法が示されています。ObjectGrid インスタンスは、プログラマチックに構成するか、または XML ベースの構成ファイルを使用して構成することができます。これら 2 つの方法を組み合わせて ObjectGrid を構成することもできます。 ObjectGridManager インターフェースを使用すると、 XML 構成に基づいて ObjectGrid インスタンスを作成できます。 ObjectGridManager インターフェースには、URL を引数として取るいくつかのメソッドがあります。 ObjectGridManager 内に渡される各 XML ファイルについて、 スキーマに対する妥当性検査を行う必要があります。 XML の妥当性検査は、以前にファイルの妥当性検査が行われ、最後の妥当検査以降、そのファイルに対しては変更が行われていない場合に限り、使用不可にすることができます。 妥当性検査を使用不可にすると、少量のオーバーヘッドが節約されますが、 無効な XML ファイルが使用される可能性が生じます。 IBM® Java Developer Kit (JDK) バージョン 5 は、XML 妥当性検査をサポートします。これをサポートしない JDK を使用すると、 Apache Xerces で XML を妥当性検査しなければならない場合があります。
以下の Java コード・スニペット は、ObjectGrid を作成するために XML 構成ファイルを渡す 方法を示しています。
import java.net.MalformedURLException;
import java.net.URL;
import com.ibm.websphere.objectgrid.ObjectGrid;
import com.ibm.websphere.objectgrid.ObjectGridException;
import com.ibm.websphere.objectgrid.ObjectGridManager;
import com.ibm.websphere.objectgrid.ObjectGridManagerFactory;
boolean validateXML = true; // turn XML validation on
boolean cacheInstance = true; // Cache the instance
String objectGridName="Employees"; // Name of Object Grid URL
allObjectGrids = new URL("file:test/myObjectGrid.xml");
final ObjectGridManager oGridManager=
ObjectGridManagerFactory.getObjectGridManager();
ObjectGrid employees =
oGridManager.createObjectGrid(objectGridName, allObjectGrids,
bvalidateXML, cacheInstance);
この XML ファイルには、いくつかの ObjectGrids の構成情報が含まれています。前のコード・スニペットは、具体的に ObjectGrid Employees を返します。この場合、Employees の構成は、この XML ファイルに定義されていることを想定しています。/**
* A simple factory method to return an instance of an
* Object Grid. A unique name is assigned.
* The instance of ObjectGrid is not cached.
* Users can then use {@link ObjectGrid#setName(String)} to change the
* ObjectGrid name.
*
* @return ObjectGrid an instance of ObjectGrid with a unique name assigned
* @throws ObjectGridException any error encountered during the
* ObjectGrid creation
*/
public ObjectGrid createObjectGrid() throws ObjectGridException;
/**
* A simple factory method to return an instance of an ObjectGrid with the
* specified name. The instances of ObjectGrid can be cached. If an ObjectGrid
* with the this name has already been cached, an ObjectGridException
* will be thrown.
*
* @param objectGridName the name of the ObjectGrid to be created.
* @param cacheInstance true, if the ObjectGrid instance should be cached
* @return an ObjectGrid instance
* @this name has already been cached or
* any error during the ObjectGrid creation.
*/
public ObjectGrid createObjectGrid(String objectGridName, boolean cacheInstance)
throws ObjectGridException;
/**
* Create an ObjectGrid instance with the specified ObjectGrid name. The
* ObjectGrid instance created will be cached.
* @param objectGridName the Name of the ObjectGrid instance to be created.
* @return an ObjectGrid instance
* @throws ObjectGridException if an ObjectGrid with this name has already
* been cached, or any error encountered during the ObjectGrid creation
*/
public ObjectGrid createObjectGrid(String objectGridName)
throws ObjectGridException;
/**
* Create an ObjectGrid instance based on the specified ObjectGrid name and the
* XML file. The ObjectGrid instance defined in the XML file with the specified
* ObjectGrid name will be created and returned. If such an ObjectGrid
* cannot be found in the xml file, an exception will be thrown.
*
* This ObjecGrid instance can be cached.
*
* If the URL is null, it will be simply ignored. In this case, this method behaves
* the same as {@link #createObjectGrid(String, boolean)}.
*
* @param objectGridName the Name of the ObjectGrid instance to be returned. It
* must not be null.
* @param xmlFile a URL to a wellformed xml file based on the ObjectGrid schema.
* @param enableXmlValidation if true the XML is validated
* @param cacheInstance a boolean value indicating whether the ObjectGrid
* instance(s)
* defined in the XML will be cached or not. If true, the instance(s) will
* be cached.
*
* @throws ObjectGridException if an ObjectGrid with the same name
* has been previously cached, no ObjectGrid name can be found in the xml file,
* or any other error during the ObjectGrid creation.
* @return an ObjectGrid instance
* @see ObjectGrid
*/
public ObjectGrid createObjectGrid(String objectGridName, final URL xmlFile,
final boolean enableXmlValidation, boolean cacheInstance)
throws ObjectGridException;
/**
* Process an XML file and create a List of ObjectGrid objects based
* upon the file.
* These ObjecGrid instances can be cached.
* An ObjectGridException will be thrown when attempting to cache a
* newly created ObjectGrid
* that has the same name as an ObjectGrid that has already been cached.
*
* @param xmlFile the file that defines an ObjectGrid or multiple
* ObjectGrids
* @param enableXmlValidation setting to true will validate the XML
* file against the schema
* @param cacheInstances set to true to cache all ObjectGrid instances
* created based on the file
* @return an ObjectGrid instance
* @throws ObjectGridException if attempting to create and cache an
* ObjectGrid with the same name as
* an ObjectGrid that has already been cached, or any other error
* occurred during the
* ObjectGrid creation
*/
public List createObjectGrids(final URL xmlFile, final boolean enableXmlValidation,
boolean cacheInstances) throws ObjectGridException;
/** Create all ObjectGrids that are found in the XML file. The XML file will be
* validated against the schema. Each ObjectGrid instance that is created will
* be cached. An ObjectGridException will be thrown when attempting to cache a
* newly created ObjectGrid that has the same name as an ObjectGrid that has
* already been cached.
* @param xmlFile The XML file to process. ObjectGrids will be created based
* on what is in the file.
* @return A List of ObjectGrid instances that have been created.
* @throws ObjectGridException if an ObjectGrid with the same name as any of
* those found in the XML has already been cached, or
* any other error encounterred during ObjectGrid creation.
*/
public List createObjectGrids(final URL xmlFile) throws ObjectGridException;
/**
* Process the XML file and create a single ObjectGrid instance with the
* objectGridName specified only if an ObjectGrid with that name is found in
* the file. If there is no ObjectGrid with this name defined in the XML file,
* an ObjectGridException
* will be thrown. The ObjectGrid instance created will be cached.
* @param objectGridName name of the ObjectGrid to create. This ObjectGrid
* should be defined in the XML file.
* @param xmlFile the XML file to process
* @return A newly created ObjectGrid
* @throws ObjectGridException if an ObjectGrid with the same name has been
* previously cached, no ObjectGrid name can be found in the xml file,
* or any other error during the ObjectGrid creation.
*/
public ObjectGrid createObjectGrid(String objectGridName, URL xmlFile)
throws ObjectGridException;