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. For detailed instructions on building and running the examples, see .
This chapter includes the following sections:
Caches are defined in a cache configuration deployment descriptor and are referred to by name within an application. This allows configuration changes to be made to a cache without having to change an application's code. The following cache configuration defines a basic distributed cache which is mapped to the cache name hello-example
.
To define the example cache:
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. When a JVM joins the cluster, it is called a cluster node, or alternatively, a cluster member. For the sample applications in this chapter, two separate Java processes form the cluster: a cache server process and the Hello World application process. For simplicity, the two processes are collocated on a single computer. The cache server, by default, is configured to store cache data.
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:
Step 3 is a multi-part step that includes a sample Hello World application and instructions for running and verifying the example. The application is run from the command line and starts a cache node that joins with a cache server. The application puts a key named 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.
Applications use the Coherence API to access and interact with a cache. The CacheFactory
class is used to get an instance of a cache and the NamedCache
interface is used to retrieve and store objects in the cache. The Hello World application is very basic, but it does demonstrate using the CacheFactory
class and the NamedCache
interface.
Example 2-1 The Sample HelloWorld Standalone Application
package com.examples; import com.tangosol.net.CacheFactory; import com.tangosol.net.NamedCache; public class HelloWorld { public static void main(String[] args) { String key = "k1"; String value = "Hello World!"; CacheFactory.ensureCluster(); NamedCache<Object, Object> cache = CacheFactory.getCache("hello-example"); cache.put(key, value); System.out.println((String)cache.get(key)); CacheFactory.shutdown(); } }
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:
Step 4 is a multi-part step that includes the Hello World application re-implemented as a JSP page. Instructions are included for packaging the sample as a Web application to be deployed to a JavaEE server. The application runs on the application server and starts a cache node that joins with a cache server. The application puts a key named 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. For Weblogic server instructions about packaging and deploying Coherence applications, see Administering Oracle Coherence. The instructions in this section are not specific to, or recommended for, WebLogic Server.
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:
This section provides basic instructions on how to setup JDeveloper for Coherence development. The instructions are for running Coherence within the IDE which is a common approach during development. While the instructions are specific to JDeveloper, the same approach should be possible with any IDE. See your IDE's documentation for specific instructions.
JDeveloper can run cache server (DefaultCacheServer
) and cache (CacheFactory
) instances. Each instance is started as a separate Java process and emits standard output to the process' log. Input (such as cache commands) can be entered directly in the process as if it were started from the command line. This configuration facilitates development and testing Coherence solutions.
To run Coherence in JDeveloper:
Java can dump a list of threads and all their held locks to standard out. This is achieved in Linux environments using the kill
command and in Windows environments using ctrl+break
. Thread dumps are very useful for troubleshooting during development (for example, finding deadlocks).
When developing Coherence solutions in JDeveloper, you can view thread dumps directly in a process's log tab. This is achieved, by sending the above signals to the Java process running in JDeveloper.
To view thread dumps in JDeveloper:
JDeveloper can create Coherence configuration files. JDeveloper loads the appropriate XSD files and lists all the elements in the Component Palette. In addition, JDeveloper validates configuration files against the XSD and provides XML code completion. The following procedure creates both a cache configuration file and an operational override file. The same procedure can be used for any of the Coherence configuration files.
To create a cache configuration and operation override file in JDeveloper: