2 Building Your First Coherence Application
Note:
The example in this chapter is basic and is only intended to teach general concepts. For more advanced examples, refer to the examples included with the installation. The examples are also distributed as part of the Coherence supplemental installation. See Running the Coherence Examples in Installing Oracle Coherence.
This chapter includes the following sections:
- Task 1: Define the Example Cache
Caches are defined in a cache configuration deployment descriptor and are referred to by name within an application. - Task 2: Configure and Start the Example Cluster
Caches are hosted on a Coherence cluster. At run time, any JVM process that is running Coherence automatically joins the cluster and can access the caches and other services provided by the cluster. - Task 3: Create and Run a Basic Coherence Standalone Application
Task 3 is a multi-part step that includes a sample Hello World application and instructions for running and verifying the example. - Task 4: Create and Run a Basic Coherence JavaEE Web Application
Task 4 is a multi-part step that re-implements the Hello World application as a JSP page.
Parent topic: Getting Started
Task 1: Define the Example Cache
hello-example
.
To define the example cache:
Parent topic: Building Your First Coherence Application
Task 2: Configure and Start the Example Cluster
The example cluster uses an operational override file to modify the out-of-box default cluster configuration. In particular, the default configuration is modified to create a private cluster which ensures that the two processes do not attempt to join an existing Coherence cluster that may be running on the network. The default configuration is also modified to load the example-config.xml
cache configuration file instead of the default cache configuration file.
To configure and start the example cluster:
Parent topic: Building Your First Coherence Application
Task 3: Create and Run a Basic Coherence Standalone Application
k1
with the value Hello World!
into the hello-example
cache and then gets and prints out the value of the key before exiting. Lastly, an additional cluster node is started to verify that the key is in the cache.
This section includes the following topics:
- Create the Sample Standalone Application
- Run the Sample Standalone Application
- Verify the Example Cache
Parent topic: Building Your First Coherence Application
Create the Sample Standalone Application
Applications use the Coherence API to access and interact with a cache. The CoherenceSession
class creates a Session
instance using a default session provider then gets a reference to a NamedCache
instance using the getCache
method. The NamedCache
instance is then used to retrieve and store objects in the cache. The Hello World application is very basic, but it does demonstrate using the Session
and NamedCache
APIs.
Example 2-1 The Sample HelloWorld Standalone Application
package com.examples;
import com.tangosol.net.CoherenceSession;
import com.tangosol.net.NamedCache;
import com.tangosol.net.Session;
public class HelloWorld {
public static void main(String[] args) throws Exception {
String key = "k1";
String value = "Hello World!";
Session session = new CoherenceSession();
NamedCache<Object, Object> cache = session.getCache("hello-example");
cache.put(key, value);
System.out.println(cache.get(key));
session.close();
}
}
Verify the Example Cache
The cache server in this example is configured, by default, to store the cache's data. The data is available to all members of the cluster and persists even after members leave the cluster. For example, the Hello World application exits after it loads and displays a key in the cache. However, the cache and key are still available for all cluster members.
This step uses the cache factory command-line tool to connect to the hello-example
cache and list all items in the cache. It demonstrates both the persistent and distributed nature of Coherence caches.
To verify the cache:
Task 4: Create and Run a Basic Coherence JavaEE Web Application
k2
with the value Hello World!
into the hello-example
cache and then gets and prints out the value of the key before exiting. Lastly, an additional cluster node is started to verify that the key is in the cache.
Note:
WebLogic server includes a Coherence integration that standardizes the packaging and deployment of Coherence applications. See Deploying Coherence Applications to WebLogic Server in Administering Oracle Coherence. The instructions in this section are not specific to, or recommended for, WebLogic Server.
This section includes the following topics:
- Create the Sample Web Application
- Deploy and Run the Sample Web Application
- Verify the Example Cache
Parent topic: Building Your First Coherence Application
Verify the Example Cache
The cache server in this example is configured, by default, to store the cache's data. The data is available to all members of the cluster and persists even after members leave the cluster. For example, the Hello World application exits after it loads and displays a key in the cache. However, the cache and key are still available for all cluster members.
This step uses the cache factory command-line tool to connect to the hello-example
cache and list all items in the cache. It demonstrates both the persistent and distributed nature of Coherence caches.
To verify the cache: