ディストリビューションに付属のCoherenceの例には、RESTアプリケーションのエンドツーエンドの例も含まれます。Coherenceの例の実行の詳細は、『Oracle Coherenceのインストール』を参照してください。
この章の内容は次のとおりです。
この章では、一連の手順を使用して、基本的なCoherence RESTアプリケーションを構成および実行します。各手順では、HTTPリクエストの処理、リモート・キャッシュの構成、Coherence REST APIの使用を管理するプロキシ・サーバーの構成など、基本的な概念について説明します。
この章の例では、アプリケーション・サーバーが不要なスタンドアロンのアプリケーションをデプロイするために、埋込みHTTPサーバーを使用します。WebLogicなどのアプリケーション・サーバーに関するデプロイメント・オプションの詳細は、「Coherence RESTのデプロイ」を参照してください。
この章の手順を完了するには、Coherence for Javaをインストールする必要があります。またこの例では、次のユーザー定義の変数を使用します。
DEV_ROOT
- リストされているすべての手順を実行する場所となるルート・フォルダのパス。つまり、その後のすべてのフォルダはDEV_ROOT
に対する相対フォルダになります。
COHERENCE_HOME
- Coherence JARを含むフォルダのパス (coherence.jar
およびcoherence-rest.jar
)
Coherence RESTには、キャッシュおよびプロキシ・スキームの両方が必要です。プロキシ・スキームでは、受信するHTTPリクエストを処理するHTTPアクセプタを定義する必要があります。キャッシュおよびプロキシは、クラスタ側のキャッシュ構成デプロイメント・ディスクリプタ内に構成されます。この例では、プロキシはlocalhost
およびポート8080
上でクライアントHTTPリクエストを受け入れるように構成されます。dist-http-example
という名前の分散キャッシュを定義し、これを使用してクライアント・データをクラスタに格納します。
クラスタ側を構成する手順は、次のとおりです。
Coherence RESTサービスには、公開されるキャッシュに関するメタデータが必要です。メタデータには、キャッシュ・エントリのキーと値のタイプ、およびキー・コンバータおよび値マーシャリング処理が含まれます。キーおよび値のタイプは、Coherenceが組込みのコンバータおよびマーシャリング処理を使用できるようにするために必要です(XMLとJSONがサポートされます)。
RESTサービスを構成するには:
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\*
次のコマンド行では、キャッシュ・サーバー・プロセスが開始され、coherence.cacheconfig
システム・プロパティを使用して、ステップ1で作成したキャッシュ構成ファイルに明示的に名前が付けられます。また、必要なすべてのライブラリおよび構成ファイルを設定します(必要なすべてのライブラリ依存性でdependencies
を置き換えます)。
java -cp DEV_ROOT\config;DEV_ROOT;DEV_ROOT\libs\dependencies;
COHERENCE_HOME\coherence-rest.jar -Dcoherence.clusterport=8090
-Dcoherence.ttl=0
-Dcoherence.cacheconfig=DEV_ROOT\config\example-server-config.xml
com.tangosol.net.DefaultCacheServer
UNIXベース・システム用のスクリプトの例は次のとおりです
#!/bin/bash
export CLASSPATH=${DEV_ROOT}/config:${DEV_ROOT}:
${DEVROOT}/lib/dependencies:${COHERENCE_HOME}/lib/coherence.jar:
${COHERENCE_HOME}/lib/coherence-rest.jar
java -cp ${CLASSPATH} -Dcoherence.clusterport=8090
-Dcoherence.ttl=0 -Dcoherence.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}
クライアント・アプリケーションでは、キャッシュの操作を行うためにCoherence RESTサービスを使用します。HTTPベース・クライアントを構築するためのクライアント・ライブラリを提供するアプリケーション・プラットフォームは数多くあります。たとえば、Jersey Projectでは、HTTPベースのREST Webサービスによるクライアント側通信にJavaがサポートされます。クライアントでdist-http-example
キャッシュにアクセスするために使用するPUT
、GET
およびPost
操作のセマンティクスを次に示します。Jerseyを使用して構築されたJavaクライアントの例は、Jersey-client-2.12.jar
ライブラリに従っており、このライブラリが必要です。Coherence REST APIの詳細は、「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.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; 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 = ClientBuilder.newClient(); Response response = null; WebTarget webTarget = client.target(sUrl); String responseType = MediaType.APPLICATION_XML; if (mediaType == MediaType.APPLICATION_JSON_TYPE) { responseType = MediaType.APPLICATION_JSON; } if (action.equalsIgnoreCase("get")) { response = webTarget.request(responseType).get(); } else if (action.equalsIgnoreCase("post")) { Entity<String> person = Entity.entity(data, responseType); response = webTarget.request(responseType).post(person); } else if (action.equalsIgnoreCase("put")) { Entity<String> person = Entity.entity(data, responseType); response = webTarget.request(responseType).put(person); } else if (action.equalsIgnoreCase("delete")) { Entity<String> person = Entity.entity(data, responseType); response = webTarget.request(responseType).delete(); } System.out.println(response.readEntity(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_XML_TYPE, null); GET("http://localhost:8080/dist-http-example/1", MediaType.APPLICATION_JSON_TYPE); GET("http://localhost:8080/dist-http-example/1", MediaType.APPLICATION_XML_TYPE); GET("http://localhost:8080/dist-http-example/count()", MediaType.APPLICATION_XML_TYPE); } }