Spring を使用したコンテナー・サーバーの始動

Spring 管理拡張 Bean および名前空間のサポートを使用して、コンテナー・サーバーを始動できます。

このタスクについて

Spring 用に構成されたいくつかの XML ファイルを使用して、基本的な eXtreme Scale コンテナー・サーバーを始動できます。

手順

  1. ObjectGrid XML ファイル:

    まず最初に、1 つの ObjectGrid "Grid" と 1 つのマップ "Test" が含まれているだけの、単純な ObjectGrid XML ファイル を定義します。この ObjectGrid には "partitionListener" という名前の ObjectGridEventListener プラグインがあり、 マップ "Test" には "testLRUEvictor" という名前の Evictor プラグインがあります。 ObjectGridEventListener プラグインと Evictor プラグインの両方とも、 名前に "{spring}" が含まれるため、Spring を使用して構成されることに注意してください。

    <?xml version="1.0" encoding="UTF-8"?>
    <objectGridConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://ibm.com/ws/objectgrid/config ../objectGrid.xsd"
     xmlns="http://ibm.com/ws/objectgrid/config">
        <objectGrids>
            <objectGrid name="Grid">
    	    <bean id="ObjectGridEventListener" className="{spring}partitionListener" />
                <backingMap name="Test" pluginCollectionRef="test" />
            </objectGrid>
        </objectGrids>
    
        <backingMapPluginCollections>
            <backingMapPluginCollection id="test">
                <bean id="Evictor" className="{spring}testLRUEvictor"/>
            </backingMapPluginCollection>
        </backingMapPluginCollections>
    </objectGridConfig>
  2. ObjectGrid デプロイメント XML ファイル:

    次に、 以下に示すように単純な ObjectGrid デプロイメント XML ファイルを作成します。これは ObjectGrid を 5 個の区画に 分けます。レプリカは必要ありません。

    <?xml version="1.0" encoding="UTF-8"?>
    <deploymentPolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://ibm.com/ws/objectgrid/deploymentPolicy ../deploymentPolicy.xsd"
     xmlns="http://ibm.com/ws/objectgrid/deploymentPolicy">
        <objectgridDeployment objectgridName="Grid">
            <mapSet name="mapSet" numInitialContainers="1" numberOfPartitions="5" minSyncReplicas="0" 
                maxSyncReplicas="1" maxAsyncReplicas="0">
                <map ref="Test"/>
            </mapSet>
        </objectgridDeployment>
    </deploymentPolicy>
  3. ObjectGrid Spring XML ファイル:

    次に、 ObjectGrid Spring 管理拡張 Bean および名前空間のサポート機能を両方とも使用 して、ObjectGrid Bean を構成します。spring xml ファイル の名前は Grid_spring.xml です。この XML ファイルには 2 つのスキーマ が含まれていることに注意してください。spring-beans-2.0.xsd は Spring 管理 Bean を使用 するためのもので、objectgrid.xsd は objectgrid 名前空間内に事前定義された Bean を使用する ためのものです。

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:objectgrid="http://www.ibm.com/schema/objectgrid"
           xsi:schemaLocation="
           http://www.ibm.com/schema/objectgrid 
    			http://www.ibm.com/schema/objectgrid/objectgrid.xsd
           http://www.springframework.org/schema/beans 
    			http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    
        <objectgrid:register id="ogregister" gridname="Grid"/>
      
        <objectgrid:server id="server" isCatalog="true" name="server">
            <objectgrid:catalog host="localhost" port="2809"/>
        </objectgrid:server>
      
        <objectgrid:container id="container" 
    			objectgridxml="com/ibm/ws/objectgrid/test/springshard/objectgrid.xml" 
           deploymentxml="com/ibm/ws/objectgrid/test/springshard/deployment.xml" 
    			server="server"/>
      
        <objectgrid:LRUEvictor id="testLRUEvictor" numberOfLRUQueues="31"/>
      
        <bean id="partitionListener" 
    			class="com.ibm.websphere.objectgrid.springshard.ShardListener" scope="shard"/>
    </beans>

    この spring XML ファイルには、次の 6 個の Bean が定義されました。

    1. objectgrid:register: これは、ObjectGrid "Grid" に対してデフォルトの Bean ファクトリーを 登録します。
    2. objectgrid:server: これは、"server" という名前で ObjectGrid サーバーを 定義します。objectgrid:catalog Bean がネストされているので、このサーバーはカタログ・サービスも 提供します。
    3. objectgrid:catalog: これは、"localhost:2809" に設定された ObjectGrid カタログ・サービス・エンドポイント を定義します。
    4. objectgrid:container: これは、前述したように、指定された objectgrid XML ファイルおよびデプロイメント XML ファイルと共に ObjectGrid コンテナー を定義します。server プロパティーは、このコンテナーがどのサーバーで ホストされているのかを指定します。
    5. objectgrid:LRUEvictor: これは、使用する LRU キューの数を 31 に設定して LRUEvictor を 定義します。
    6. bean partitionListener: これは ShardListener プラグインを定義します。 このプラグインの実装を指定する必要があるため、事前定義された Bean を使用することはできません。また、この Bean の有効範囲は "shard" (断片) に設定されています。これは、この ShardListener のインスタンスが ObjectGrid 断片当たり 1 つのみであることを意味します。
  4. サーバーの始動:

    以下のスニペットは、 コンテナー・サービスとカタログ・サービスの両方をホストする ObjectGrid サーバーを 開始します。これを見て分かるように、サーバーを開始するために呼び出す必要のあるメソッドは、Bean ファクトリーからの Bean "container" の get だけです。 これは、ロジックの大部分を Spring 構成に移すことになり、プログラミングの複雑さ が軽減されます。

    public class ShardServer extends TestCase
    {
        Container container;
        org.springframework.beans.factory.BeanFactory bf;
    
        public void startServer(String cep) 
        {
            try
            {
                bf = new org.springframework.context.support.ClassPathXmlApplicationContext(
                    "/com/ibm/ws/objectgrid/test/springshard/Grid_spring.xml", ShardServer.class);
                container = (Container)bf.getBean("container");
            }
            catch(Exception e)
            {
                throw new ObjectGridRuntimeException("Cannot start OG container", e);
            }
        }
    	
        public void stopServer() 
        {
            if(container != null)
                container.teardown();
        }
    }