The Coherence examples that ship with distribution also include and end-to-end example of a REST application. For detailed instructions on running the Coherence Examples, see Installing Oracle Coherence.
This chapter includes the following sections:
This chapter is organized into a set of steps that are used to configure and run a basic Coherence REST application. The steps demonstrate fundamental concepts, such as: configuring a proxy server responsible for handling HTTP request, configuring a remote cache, and using the Coherence REST API.
The example in this chapter uses an embedded HTTP server in order to deploy a standalone application that does not require an application server. For details about deployment options with application servers, such as WebLogic, see Deploying Coherence REST.
Coherence for Java must be installed to complete the steps in this chapter. In addition, the following user-defined variables are used in this example:
DEV_ROOT
- The path to root folder where user is performing all of the listed steps, or in other words all of the following folders are relative to DEV_ROOT.
COHERENCE_HOME
- The path to folder containing Coherence JARs (coherence.jar
and coherence-rest.jar
)
Coherence REST requires both a cache and a proxy scheme. The proxy scheme must define an HTTP acceptor to handle an incoming HTTP request. The cache and proxy are configured in the cluster-side cache configuration deployment descriptor. For this example, the proxy is configured to accept client HTTP requests on localhost
and port 8080
. A distributed cache named dist-http-example
is defined and is used to store client data in the cluster.
To configure the cluster side:
Create the Person
user type, which is stored in the cache and used to demonstrate basic REST operations.
To create the Person object:
The Coherence REST services require metadata about the cache that it exposes. The metadata includes the cache entry's key and value types as well as key converters and value marshallers. The key and value types are required in order for Coherence to be able to use built-in converters and marshallers (XML and JSON supported).
To configure the REST services:
REST services are exposed as part of a cache server process (DefaultCacheServer
). The cache server's classpath must be configured to find all the configuration files that were created in the previous steps as well as the Person.class
. The classpath must also contain the required dependency libraries (see "Dependencies for Coherence REST"). For the sake of brevity, all of the dependencies are placed in DEV_ROOT
\libs
folder and are not individually listed.
The DEV_ROOT
folder should appear as follows:
\ \config \config\example-server-config.xml \config\coherence-rest-config.xml \example \example\Person.class \libs \libs\*
The following command line starts a cache server process and explicitly names the cache configuration file created in Step 1 by using the coherence.cacheconfig
system property. In addition it sets all the needed libraries and configuration files (replace dependencies
with all the required library 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
An example script for UNIX-based system follows:
#!/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
Check the console output to verify that the proxy service has started. The output message should include the following:
(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}
Client applications use Coherence REST services to perform cache operations. There are many application platforms that provide client libraries to build HTTP-based clients. For example, the Jersey project provides Java support for client-side communication with HTTP-based REST Web services. The following sections demonstrate the semantics for PUT
, GET
, and Post
operations that a client would use to access the dist-http-example
cache. An example Java client built using Jersey follows and requires the Jersey-client-2.12.jar
library. See Performing Grid Operations with REST, for complete details on the Coherence REST API.
Put Operations
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 Operations
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 Operation
POST http://localhost:8080/dist-http-example/increment(age,1)
Sample Jersey REST Client
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); } }