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:
Parent topic: Getting Started
hello-example
.To define the example cache:
Parent topic: Building Your First Coherence Application
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
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:
Parent topic: Building Your First Coherence 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(); } }
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:
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:
Parent topic: Building Your First Coherence Application
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 includes the following topics:
Parent topic: Building Your First Coherence Application
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:
Parent topic: Using JDeveloper for Coherence Development
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:
Parent topic: Using JDeveloper for Coherence Development
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:
Parent topic: Using JDeveloper for Coherence Development