プライマリ・コンテンツに移動
Oracle® Fusion Middleware Oracle Coherenceリモート・クライアントの開発
12c (12.1.3)
E56210-03
  ドキュメント・ライブラリへ移動
ライブラリ
製品リストへ移動
製品
目次へ移動
目次

前
 
次
 

24 最初のCoherence RESTアプリケーションの作成

この章では、Coherence RESTアプリケーションを構築および実行するために必要な、基本的なタスクについて説明します。この章で説明されている多くの概念は、後の章で詳細に説明します。

この章には次の項が含まれます:

24.1 Coherence RESTの例の概要

この章では、一連の手順を使用して、基本的なCoherence RESTアプリケーションを構成および実行します。各手順では、HTTPリクエストの処理、リモート・キャッシュの構成、Coherence REST APIの使用を管理するプロキシ・サーバーの構成など、基本的な概念について説明します。この章の例では、アプリケーション・サーバーが不要なスタンドアロンのアプリケーションをデプロイするために、埋込みHTTPサーバーを使用します。WebLogicやGlassFishなどのアプリケーション・サーバーに関するデプロイメント・オプションの詳細は、第26章「Coherence RESTのデプロイ」を参照してください。

この章の手順を完了するには、Coherence for Javaをインストールする必要があります。またこの例では、次のユーザー定義の変数を使用します。

  • DEV_ROOT - リストされているすべての手順を実行する場所となるルート・フォルダのパス。つまり、その後のすべてのフォルダはDEV_ROOTに対する相対フォルダになります。

  • COHERENCE_HOME - Coherence JARを含むフォルダのパス (coherence.jarおよびcoherence-rest.jar)

24.2 ステップ1: クラスタ側の構成

Coherence RESTには、キャッシュおよびプロキシ・スキームの両方が必要です。プロキシ・スキームでは、受信するHTTPリクエストを処理するHTTPアクセプタを定義する必要があります。キャッシュおよびプロキシは、クラスタ側のキャッシュ構成デプロイメント・ディスクリプタ内に構成されます。この例では、プロキシはlocalhostおよびポート8080上でクライアントHTTPリクエストを受け入れるように構成されます。dist-http-exampleという名前の分散キャッシュを定義し、これを使用してクライアント・データをクラスタに格納します。

クラスタ側を構成する手順は、次のとおりです。

  1. example-server-config.xmlという名前のXMLファイルをDEV_ROOT\configフォルダに作成します。

  2. このファイルに次のXMLをコピーします。

    <?xml version="1.0"?>
    <cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
       xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config
       coherence-cache-config.xsd">
       <caching-scheme-mapping>
          <cache-mapping>
             <cache-name>dist-http-example</cache-name>
             <scheme-name>dist-http</scheme-name>
          </cache-mapping>
       </caching-scheme-mapping>
    
       <caching-schemes>
          <distributed-scheme>
             <scheme-name>dist-http</scheme-name>
             <backing-map-scheme>
                <local-scheme/>
             </backing-map-scheme>
             <autostart>true</autostart>
          </distributed-scheme>
    
          <proxy-scheme>
             <service-name>ExtendHttpProxyService</service-name>
             <acceptor-config>
                <http-acceptor>
                   <local-address>
                      <address>localhost</address>
                      <port>8080</port>
                   </local-address>
                </http-acceptor>
             </acceptor-config>
             <autostart>true</autostart>
          </proxy-scheme>
       </caching-schemes>
    </cache-config>
    
  3. ファイルを保存して閉じます。

24.3 ステップ2: ユーザー・タイプの作成

Personユーザー・タイプを作成します。これはキャッシュに保存され、基本的なREST操作を説明するために使用します。

Personオブジェクトを作成するには:

  1. DEV_ROOT\exampleフォルダにテキスト・ファイルを作成します。

  2. このファイルに次のJavaコードをコピーします。

    package example;
    import java.io.Serializable;
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement(name="person")
    @XmlAccessorType(XmlAccessType.PROPERTY)
    public class Person implements Serializable {
    
        public Person() {}
    
        public Person(String name, int age)
            {
            m_name = name;
            m_age  = age;
            }
    
        public String getName() { return m_name; }
    
        public void setName(String name) { m_name = name; }
    
        public int getAge() { return m_age; }
    
        public void setAge(int age) {  m_age = age; }
    
        protected String m_name;
        protected int    m_age;
    }
    
  3. Person.javaという名前でファイルを保存して閉じます。

  4. 次のように入力してPerson.javaをコンパイルします。

    javac example\Person.java
    

24.4 ステップ3: RESTサービスの構成

Coherence RESTサービスには、公開されるキャッシュに関するメタデータが必要です。メタデータには、キャッシュ・エントリのキーと値のタイプ、およびキー・コンバータおよび値マーシャリング処理が含まれます。キーおよび値のタイプは、Coherenceが組込みのコンバータおよびマーシャリング処理を使用できるようにするために必要です(XMLとJSONがサポートされます)。

RESTサービスを構成するには:

  1. coherence-rest-config.xmlという名前のXMLファイルをDEV_ROOT\configフォルダに作成します。

  2. このファイルに次のXMLをコピーします。

    <?xml version="1.0"?>
    <rest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns="http://xmlns.oracle.com/coherence/coherence-rest-config"
          xsi:schemaLocation=
             "http://xmlns.oracle.com/coherence/coherence-rest-config
          coherence-rest-config.xsd">
      <resources>
        <resource>
          <cache-name>dist-http-example</cache-name>
          <key-class>java.lang.String</key-class>
          <value-class>example.Person</value-class>
        </resource>
      </resources>
    </rest>
    
  3. ファイルを保存し、閉じます。

24.5 ステップ4: キャッシュ・サーバー・プロセスの開始

RESTサービスは、キャッシュ・サービス・プロセス(DefaultCacheServer)の一環として公開されます。キャッシュ・サーバーのクラスパスは、前述の手順で作成したすべての構成ファイルおよびPerson.classを検索できるように構成する必要があります。クラスパスには、必要な依存性ライブラリも含める必要があります(「Coherence RESTの依存性」を参照)。簡潔にするために、前述のすべてのファイルはDEV_ROOT\libsフォルダに配置します。

DEV_ROOTフォルダは、次のような構成になります。

\
\config
\config\example-server-config.xml
\config\coherence-rest-config.xml
\example
\example\Person.class
\libs
\libs\jersey-server-1.18.jar
\libs\jersey-servlet-1.18.jar
\libs\jersey-core-1.18.jar
\libs\jersey-json-1.18.jar
\libs\jersey-multipart-1.18.jar
\libs\jersey-grizzly2-1.12.jar
\libs\jackson-core-asl-1.9.2.jar
\libs\jackson-jaxrs-1.9.2.jar
\libs\jackson-mapper-asl-1.9.2.jar
\libs\jackson-xc-1.9.2.jar

次のコマンド行では、キャッシュ・サーバー・プロセスが開始され、tangosol.coherence.cacheconfigシステム・プロパティを使用して、ステップ1で作成したキャッシュ構成ファイルに明示的に名前が付けられます。また、必要なライブラリおよび構成ファイルをすべてクラスパス変数に設定します。

java -cp DEV_ROOT\config;DEV_ROOT;DEV_ROOT\libs\jersey-server-1.18.jar;
DEV_ROOT\libs\jersey-servlet-1.18.jar;DEV_ROOT\libs\jersey-core-1.18.jar;
DEV_ROOT\libs\jersey-json-1.18.jar;DEV_ROOT\libs\jersey-multipart-1.18.jar;
DEV_ROOT\libs\jersey-grizzly2-1.12.jar;DEV_ROOT\libs\jackson-core-asl-1.9.2.jar;
DEV_ROOT\libs\jackson-jaxrs-1.9.2.jar;DEV_ROOT\libs\jackson-mapper-asl-1.9.2.jar;
DEV_ROOT\libs\jackson-xc-1.9.2.jar;COHERENCE_HOME\coherence.jar;
COHERENCE_HOME\coherence-rest.jar -Dtangosol.coherence.clusterport=8080 
-Dtangosol.coherence.ttl=0 
-Dtangosol.coherence.cacheconfig=DEV_ROOT\config\example-server-config.xml 
com.tangosol.net.DefaultCacheServer

UNIXベース・システム用のスクリプトの例は次のとおりです

#!/bin/bash

export CLASSPATH=${DEV_ROOT}/config:${DEV_ROOT}:
${DEVROOT}/lib/jackson-core-asl-1.9.2.jar:${DEVROOT}/lib/jackson-jaxrs-1.9.2.jar:
${DEVROOT}/lib/jackson-mapper-asl-1.9.2.jar:${DEVROOT}/lib/jackson-xc-1.9.2.jar:
${DEV_ROOT}/lib/jersey-core-1.18.jar:${DEV_ROOT}/lib/jersey-json-1.18.jar:
${DEV_ROOT}/lib/jersey-multipart-1.18.jar:${DEV_ROOT}/lib/jersey-server-1.18.jar:
${DEV_ROOT}/lib/jersey-servlet-1.18.jar:${DEV_ROOT}/lib/jersey-grizzly2-1.12.jar:
${COHERENCE_HOME}/lib/coherence.jar:${COHERENCE_HOME}/lib/coherence-rest.jar

java -cp ${CLASSPATH} -Dtangosol.coherence.clusterport=8080 
-Dtangosol.coherence.ttl=0 -Dtangosol.coherence.cacheconfig=
${DEV_ROOT}/config/example-server-config.xml com.tangosol.net.DefaultCacheServer

コンソール出力をチェックして、プロキシ・サービスが開始されていることを確認します。出力メッセージは次のようになります。

(thread=Proxy:ExtendHttpProxyService:HttpAcceptor, member=1): Started: HttpAcceptor{Name=Proxy:ExtendHttpProxyService:HttpAcceptor, State=(SERVICE_STARTED), HttpServer=com.tangosol.coherence.rest.server.DefaultHttpServer, LocalAddress=localhost, LocalPort=8080, ResourceConfig=com.tangosol.coherence.rest.server.DefaultResourceConfig, RootResource=com.tangosol.coherence.rest.DefaultRootResource}

24.6 ステップ5: クライアントからRESTサービスへのアクセス

クライアント・アプリケーションでは、キャッシュの操作を行うためにCoherence RESTサービスを使用します。HTTPベース・クライアントを構築するためのクライアント・ライブラリを提供するアプリケーション・プラットフォームは数多くあります。たとえば、Jersey Projectでは、HTTPベースのREST Webサービスによるクライアント側通信にJavaがサポートされます。クライアントでdist-http-exampleキャッシュにアクセスするために使用するPUTGETおよびPost操作のセマンティクスを次に示します。Jerseyを使用して構築されたJavaクライアントの例は、Jersey-client-1.18.jarライブラリに従っており、このライブラリが必要です。Coherence REST APIの詳細は、第25章「RESTを使用したグリッド操作の実行」を参照してください。

Put操作

PUT http://localhost:8080/dist-http-example/1 
Content-Type=application/json 
Request Body: {"name":"chris","age":30}
PUT http://localhost:8080/dist-http-example/2 
Content-Type=application/json
Request Body: {"name":"adam","age":26}

GET操作

GET http://localhost:8080/dist-http-example/1.json

GET http://localhost:8080/dist-http-example/1.xml

GET http://localhost:8080/dist-http-example?q=name is 'adam'

GET http://localhost:8080/dist-http-example;p=name

GET http://localhost:8080/dist-http-example/count()

GET http://localhost:8080/dist-http-example/double-average(age)

Post操作

POST http://localhost:8080/dist-http-example/increment(age,1)

サンプルJersey RESTクライアント

package example;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class RestExample {
   public static void PUT(String url, MediaType mediaType,String data) {
      process(url,"put",mediaType,data);
   }
   public static void GET(String url, MediaType mediaType) {
      process(url,"get",mediaType,null);
   }
   public static void POST(String url, MediaType mediaType,String data) {
      process(url,"post",mediaType,data);
   }
   public static void DELETE(String url, MediaType mediaType) {
      process(url,"delete",mediaType,null);
   }

   public static void process(String sUrl, String action, MediaType mediaType,
      String data) {
      Client client = Client.create();
      ClientResponse response = null;
      WebResource webResource = client.resource(sUrl);      if (action.equalsIgnoreCase("get")) {
         response = webResource.type(mediaType).get(ClientResponse.class);
      } else if (action.equalsIgnoreCase("post")) {
         response = webResource.type(mediaType).post(ClientResponse.class, data);
      } else if (action.equalsIgnoreCase("put")) {
         response = webResource.type(mediaType).put(ClientResponse.class, data);
      } else if (action.equalsIgnoreCase("delete")) {
         response = webResource.type(mediaType).delete(ClientResponse.class,data);
      }
      int status = response.getStatus();
      System.out.println("status:" + status + "Result: "
         + response.getEntity(String.class));
   }

   public static void main(String[] args) throws URISyntaxException,
      MalformedURLException, IOException {
      PUT("http://localhost:8080/dist-http-example/1",
         MediaType.APPLICATION_JSON_TYPE,"{\"name\":\"chris\",\"age\":32}");
      PUT("http://localhost:8080/dist-http-example/2",
         MediaType.APPLICATION_JSON_TYPE,
         "{\"name\":\"\ufeff\u30b8\u30e7\u30f3A\",\"age\":66}");
      PUT("http://localhost:8080/dist-http-example/3",
         MediaType.APPLICATION_JSON_TYPE,"{\"name\":\"adm\",\"age\":88}");
      POST("http://localhost:8080/dist-http-example/increment(age,1)",
         MediaType.APPLICATION_JSON_TYPE,null);
      GET("http://localhost:8080/dist-http-example/3",
         MediaType.APPLICATION_JSON_TYPE);
      GET("http://localhost:8080/dist-http-example/count()",
         MediaType.APPLICATION_XML_TYPE);
   }
}