組み込みサーバー API を使用したサーバーの開始と停止

WebSphere® eXtreme Scale では、組み込みサーバーおよびコンテナーのライフサイクルの管理にプログラマチック API を使用できます。コマンド行オプションやファイル・ベースのサーバー・プロパティーでも構成可能な任意のオプションを使用して、プログラムでサーバーを構成できます。コンテナー・サーバー、カタログ・サービス、またはその両方として、組み込みサーバーの構成が可能です。

始める前に

既存の Java 仮想マシン内からコードを実行するためのメソッドが必要です。 eXtreme Scale クラスが、クラス・ローダー・ツリーから利用可能でなければなりません。

このタスクについて

管理 API を使用して多くの管理タスクを実行できます。API の一般的な使用法の 1 つとして、Web アプリケーションの状態を保管する内部サーバーとしての使用があります。 Web サーバーは、組み込み WebSphere eXtreme Scale サーバーを始動し、コンテナー・サーバーをカタログ・サービスに報告することができ、サーバーは、より幅広い分散グリッドのメンバーとして追加されます。この使用法では、本来は揮発性のデータ・ストアにスケーラビリティーと高可用性が提供されます。

組み込み eXtreme Scale サーバーの全ライフサイクルをプログラムで制御できます。例は、できる限り汎用的にして、概要を説明したステップの直接的なコードの例のみを示しています。

手順

  1. ServerFactory クラスから ServerProperties オブジェクトを取得し、必要なオプションを構成します。
    ServerProperties インターフェースについて詳しくは、 ServerProperties インターフェースを参照してください。
    すべての eXtreme Scale サーバーに、一連の構成可能なプロパティーがあります。 コマンド行からサーバーが始動されると、それらのプロパティーはデフォルトに設定されますが、外部ソースまたはファイルを指定することによって、複数のプロパティーをオーバーライドすることができます。 組み込み有効範囲では、ServerProperties オブジェクトでプロパティーを直接設定できます。 これらのプロパティーは、ServerFactory クラスからサーバー・インスタンスを取得する前に設定する必要があります。 以下の例のスニペットでは、ServerProperties オブジェクトを取得して CatalogServiceBootStrap フィールドを設定し、 複数のオプション・サーバー設定を初期化します。 構成可能な設定のリストについては、API 資料を参照してください。
    ServerProperties props = ServerFactory.getServerProperties();
    props.setCatalogServiceBootstrap("host:port"); // required to connect to specific catalog service
    props.setServerName("ServerOne"); // name server
    props.setTraceSpecification("com.ibm.ws.objectgrid=all=enabled"); // Sets trace spec
  2. サーバーをカタログ・サービスにする場合には、CatalogServerProperties オブジェクトを取得します。

    CatalogServerProperties インターフェースについて詳しくは 、CatalogServerProperties インターフェースを 参照してください。

    すべての組み込みサーバーが、カタログ・サービスまたはコンテナー・サーバー、あるいはその両方になることができます。 以下の例では、CatalogServerProperties オブジェクトを取得し、カタログ・サービス・オプションを使用可能にし、 さまざまなカタログ・サービス設定を構成します。
    CatalogServerProperties catalogProps = ServerFactory.getCatalogProperties();
    catalogProps.setCatalogServer(true); // false by default, it is required to set as a catalog service
    catalogProps.setQuorum(true); // enables / disables quorum
  3. ServerFactory クラスから Server インスタンスを取得します。 Server インスタンスは、グリッド内のメンバーシップの管理に関与するプロセス・スコープの singleton です。 このインスタンスが初期化された後、このプロセスが接続され、グリッド内の他のサーバーで高度に利用可能になります。

    Server インターフェースについて詳しくは、 Server インターフェースを参照してください。ServerFactory クラスについて詳しくは、ServerFactory クラスを参照してください。

    以下の例は、Server インスタンスの作成方法を示しています。
     Server server = ServerFactory.getInstance();
    上記の例において、ServerFactory クラスは、 Server インスタンスを返す静的メソッドを提供します。 ServerFactory クラスは、Server インスタンスを取得するための唯一のインターフェースとして意図されています。 そのため、このクラスは必ず、インスタンスが singleton であるか、または各 JVM または独立したクラス・ローダーの 1 つインスタンスであるようにします。 getInstance メソッドで Server インスタンスを初期化します。 インスタンスを初期化する前にすべてのサーバー・プロパティーの構成が必要です。 Server クラスは、新規の Container インスタンスの作成に関与します。 ServerFactory クラスと Server クラスの両方を使用して、組み込み Server インスタンスのライフサイクルを管理できます。

    Container インターフェースについて詳しくは、 Container インターフェースを参照してください。

  4. Server インスタンスを使用して Container インスタンスを開始します。
    断片を組み込みサーバーに配置するには、その前に、サーバーにコンテナーを作成する必要があります。 Server インターフェースの createContainer メソッドは、DeploymentPolicy 引数を使用します。 以下の例では、取得したサーバー・インスタンスを使用して、作成した DeploymentPolicy ファイルでコンテナーを作成します。 Container は、シリアライゼーションのためにアプリケーション・バイナリーが使用可能になっているクラス・ローダーを必要とします。 これらのバイナリーは、使用するクラス・ローダーを Thread コンテキスト・クラス・ローダーに設定して createContainer メソッドを呼び出すことによって、使用可能にできます。
    DeploymentPolicy policy = DeploymentPolicyFactory.createDeploymentPolicy(new 
    	  URL("file://urltodeployment.xml"), 
    	new URL("file://urltoobjectgrid.xml"));
    Container container = server.createContainer(policy);
  5. コンテナーを除去してクリーンアップします。

    コンテナー・サーバーを除去してクリーンアップするには、取得した Container インスタンスで teardown メソッドを実行します。コンテナーで teardown メソッドを実行すると、コンテナーを適切にクリーンアップし、組み込みサーバーからコンテナーを除去します。 

    コンテナーのクリーンアップ処理には、そのコンテナー内のすべての断片の移動と終了処理が含まれます。各サーバーには、多くのコンテナーと断片が含まれます。コンテナーをクリーンアップしても、親の Server インスタンスのライフサイクルには影響しません。 以下の例は、サーバーで teardown メソッドを実行する方法を示しています。teardown メソッドは、ContainerMBean インターフェースを通して使用可能になります。ContainerMBean インターフェースを使用することによって、このコンテナーに対するプログラムによるアクセスがもうなくても、その MBean でコンテナーを除去してクリーンアップすることができます。 また、terminate メソッドが Container インターフェースに存在しますが、どうしても必要でない限り、このメソッドは使用しないでください。 このメソッドは強制力が強く、断片の適切な移動とクリーンアップの調整は行いません。
    container.teardown();
  6. 組み込みサーバーを停止します。
    組み込みサーバーを停止するときには、そのサーバーで実行されているコンテナーと断片も停止します。 組み込みサーバーの停止時には、開いているすべての接続をクリーンアップして、すべての断片を移動または終了処理する必要があります。 以下の例では、サーバーの停止方法と、Server インターフェースで waitFor メソッドを使用して Server インスタンスが確実に完全にシャットダウンするようにする方法を示しています。 コンテナーの例と同様に、stopServer メソッドは、ServerMBean インターフェースを通して使用可能になります。 このインターフェースでは、該当の Managed Bean (MBean) によりサーバーを停止できます。
    ServerFactory.stopServer();  // Uses the factory to kill the Server singleton
     // or
    server.stopServer(); // Uses the Server instance directly
    server.waitFor(); // Returns when the server has properly completed its shutdown procedures
    全コードの例:
     import java.net.MalformedURLException;
    import java.net.URL;
    
    import com.ibm.websphere.objectgrid.ObjectGridException;
    import com.ibm.websphere.objectgrid.deployment.DeploymentPolicy;
    import com.ibm.websphere.objectgrid.deployment.DeploymentPolicyFactory;
    import com.ibm.websphere.objectgrid.server.Container;
    import com.ibm.websphere.objectgrid.server.Server;
    import com.ibm.websphere.objectgrid.server.ServerFactory;
    import com.ibm.websphere.objectgrid.server.ServerProperties;
    
    public class ServerFactoryTest {
    
        public static void main(String[] args) {
    
            try {
    
                ServerProperties props = ServerFactory.getServerProperties();
                props.setCatalogServiceBootstrap("catalogservice-hostname:catalogservice-port");
                props.setServerName("ServerOne"); // name server
                props.setTraceSpecification("com.ibm.ws.objectgrid=all=enabled"); // TraceSpec
    
                /*
                 * In most cases, the server will serve as a container server only
                 * and will connect to an external catalog service. This is a more
                 * highly available way of doing things. The commented code excerpt
                 * below will enable this Server to be a catalog service.
                 *
                 *
                 * CatalogServerProperties catalogProps =
                 * ServerFactory.getCatalogProperties();
                 * catalogProps.setCatalogServer(true); // enable catalog service
                 * catalogProps.setQuorum(true); // enable quorum
                 */
    
                Server server = ServerFactory.getInstance();
    
                DeploymentPolicy policy = DeploymentPolicyFactory.createDeploymentPolicy
    					(new URL("url to deployment xml"),  new URL("url to objectgrid xml file"));
                Container container = server.createContainer(policy);
                
                /*
                 * Shard will now be placed on this container if the deployment requirements are met.
                 * This encompasses embedded server and container creation.
                 *
                 * The lines below will simply demonstrate calling the cleanup methods
                 */
                
                container.teardown();
                server.stopServer();
                int success = server.waitFor();
    
            } catch (ObjectGridException e) {
                // Container failed to initialize
            } catch (MalformedURLException e2) {
                // invalid url to xml file(s)
            }
    
        }
    
    }