ObjectGridManager インターフェースを使用した、ObjectGrid のライフサイクルの制御

ObjectGridManager インターフェースで、スタートアップ Bean またはサーブレットのいずれかを使用すると ObjectGrid インスタンスのライフサイクルを制御できます。

スタートアップ Bean でのライフサイクルの管理

スタートアップ Bean は、ObjectGrid インスタンスのライフサイクルの制御に使用できます。スタートアップ Bean はアプリケーションの開始時にロードします。スタートアップ Bean では、アプリケーションが予想通りに開始または停止するときにはいつでもコードを実行できます。スタートアップ Bean を作成するために、ホームの com.ibm.websphere.startupservice.AppStartUpHome インターフェースを使用し、また、リモート側の com.ibm.websphere.startupservice.AppStartUp インターフェースを使用します。 Bean で start メソッドおよび stop メソッドを実行します。 start メソッドは、アプリケーションの始動時に必ず起動されます。 stop メソッドは、アプリケーションのシャットダウン時に必ず起動されます。 start メソッドは、ObjectGrid インスタンスの作成に使用されます。 stop メソッドは、ObjectGrid インスタンスの除去に使用されます。 以下は、スタートアップ Bean でのこの ObjectGrid のライフサイクル管理を示すコード・スニペットです。
public class MyStartupBean implements javax.ejb.SessionBean {
    private ObjectGridManager objectGridManager;

    /* The methods on the SessionBean interface have been
     * left out of this example for the sake of brevity */

    public boolean start(){
        // Starting the startup bean
        // This method is called when the application starts
        objectGridManager = ObjectGridManagerFactory.getObjectGridManager();
        try {
            // create 2 ObjectGrids and cache these instances
            ObjectGrid bookstoreGrid = objectGridManager.createObjectGrid("bookstore", true);
            bookstoreGrid.defineMap("book");
            ObjectGrid videostoreGrid = objectGridManager.createObjectGrid("videostore", true);
            // within the JVM,
            // these ObjectGrids can now be retrieved from the
            //ObjectGridManager using the getObjectGrid(String) method
        } catch (ObjectGridException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public void stop(){
        // Stopping the startup bean
        // This method is called when the application is stopped
        try {
            // remove the cached ObjectGrids and destroy them
            objectGridManager.removeObjectGrid("bookstore", true);
            objectGridManager.removeObjectGrid("videostore", true);
        } catch (ObjectGridException e) {
            e.printStackTrace();
        }
    }
}
start メソッドが呼び出された後、新規に作成された ObjectGrid インスタンスが ObjectGridManager インターフェースから取得されます。 例えば、サーブレットがアプリケーションに含まれる場合、サーブレットは以下のコード・スニペットを使用して eXtreme Scale にアクセスします。
ObjectGridManager objectGridManager = 
	ObjectGridManagerFactory.getObjectGridManager();
ObjectGrid bookstoreGrid = objectGridManager.getObjectGrid("bookstore");
ObjectGrid videostoreGrid = objectGridManager.getObjectGrid("videostore");

サーブレットでのライフサイクルの管理

サーブレットで ObjectGrid のライフサイクルを管理するためには、 init メソッドを使用して ObjectGrid インスタンスを作成したり、destroy メソッドを使用して ObjectGrid インスタンスを除去することができます。 ObjectGrid インスタンスがキャッシュされた場合、サーブレット・コードで検索および操作を行います。 以下は、サーブレット内での ObjectGrid の作成、操作、および破棄を示すサンプル・コードです。
public class MyObjectGridServlet extends HttpServlet implements Servlet {
    private ObjectGridManager objectGridManager;

    public MyObjectGridServlet() {
        super();
    }

    public void init(ServletConfig arg0) throws ServletException {
        super.init();
        objectGridManager = ObjectGridManagerFactory.getObjectGridManager();
        try {
            // create and cache an ObjectGrid named bookstore
            ObjectGrid bookstoreGrid = 
						objectGridManager.createObjectGrid("bookstore", true);
            bookstoreGrid.defineMap("book");
        } catch (ObjectGridException e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
        ObjectGrid bookstoreGrid = objectGridManager.getObjectGrid("bookstore");
        Session session = bookstoreGrid.getSession();
        ObjectMap bookMap = session.getMap("book");
        // perform operations on the cached ObjectGrid
        // ...
	// Close the session (optional in Version 7.1.1 and later) for improved performance
	session.close();
	 }
    public void destroy() {
        super.destroy();
        try {
            // remove and destroy the cached bookstore ObjectGrid
            objectGridManager.removeObjectGrid("bookstore", true);
        } catch (ObjectGridException e) {
            e.printStackTrace();
        }
    }
}