The instructions and command line examples in this chapter assume that you have extracted the Java Coherence archive and the C++ Coherence archive onto your file system:
the Java Coherence archive was extracted into the top-level of your file system. For example, it would appear as C:\coherence on Windows.
the C++ Coherence archive was extracted into the Java Coherence root directory. The root directory for the C++ version is coherence-cpp. Thus, on Windows it would appear in the file system as C:\coherence\coherence-cpp.
See "Installing the C++ Client Distribution" for more information on installing Coherence for C++.
|
Note: Coherence C++ does not have any local dependencies on the Java installation. While this section assumes that you have installed both the Java and C++ versions of Coherence on the computer that is used to run the examples, installation of the Java version is optional. If the Java version is not installed, the Cache Server must be running on a remote computer and the Java console example is not available. |
Coherence for C++ provides the following sample applications in the coherence-cpp/examples directory of the installed product:
hellogrid—An example of basic cache access.
console—A command line application that enables you to interact with the cache using simple commands.
contacts—An example of how to store pre-existing (that is, non-Coherence) C++ classes in the grid.
The following sections are included in this chapter:
The requirements for running a sample include:
The Coherence C++ shared library, found under the platform specific coherence-cpp/lib directory of the installation. See "Setting the run-time Library and Search Path" for details.
A Coherence extend cache configuration file, found under the coherence-cpp/examples/config directory.
A running Coherence Proxy Service and Cache Server; these are Java components. See "Configuring the Cluster Side" for details.
Coherence for C++ applications communicate with the Coherence cluster using a proxy server. To run the examples against a cluster, the proxy must first be started.
A sample command to start the proxy service and cache server is listed below. You must be sure to point the proxy at the server cache configuration file, such as extend-server-config.xml provided in the config directory. For example, on Windows execute:
Example 16-1 Sample Command to Start the Proxy Service and the Cache Server
c:\coherence\lib> java -Dtangosol.coherence.cacheconfig=c:\coherence\coherence-cpp\examples\config\extend-server-config.xml -cp coherence.jar "com.tangosol.net.DefaultCacheServer"
|
Note: For the contacts example, you must also use the additional POF configuration and custom classes included in theexamples/java/ContactCache directory. |
The Coherence for C++ distribution includes platform specific build scripts. Each script takes a single command line parameter, which is the name of the sample to build. For example, to build the console example on Windows, open a new command prompt window and execute:
c:\coherence\coherence-cpp\examples> build hellogrid
The sample executable are created within the particular examples subdirectory, that is:
c:\coherence\coherence-cpp\examples\hellogrid\hellogrid.exe
To use this scripts with your own simple applications, just create a new directory under the examples directory and place your source files there. Then run build your_dir_name to compile your application.
After the configuration has been specified and the proxy/cache server has been started, you can start the client. The examples directory contains a run script which runs the examples. This script performs the basic work of setting environment variables and library search paths. To use the script, execute the run script and supply as the first parameter the name of the example you want to run.
For example, to run the hellogrid example on Windows, run the following command from the examples directory:
c:\coherence\coherence-cpp\examples> run hellogrid
The Coherence logging for the application is directed to hellogrid.log in the examples directory.
The hellogrid example exercises the cache by entering various types of data into the cache and reading them out, printing cache contents, querying the cache, and so on. Follow these steps to build and run the hellogrid example:
C:\coherence\coherence-cpp\examples>run hellogrid
retrieved cache "dist-hello" containing 0 entries
put: hello = grid
get: hello = grid
get: dummy = NULL
entire cache contents:
34567 = 8.9
23456 = 7.8
12345 = 6.7
hello = grid
updated cache contents:
34567 = 8.9
23456 = 7.8
12345 = 6.7
45678 = 9.1
filtered cache contents by coherence::util::filter::GreaterFilter: (IdentityExtr
actor, 7)
34567 = 8.9
23456 = 7.8
45678 = 9.1
minimum: 6.7
increment results by 6.7
34567 = 15.6
23456 = 14.5
12345 = 13.4
45678 = 15.8
C:\coherence\coherence-cpp\examples>
Now that you've run the example, you are encouraged to have a look at the code. Each sample has a corresponding directory under examples which contains its sample specific source. There is also a common directory which contains source used in all samples.
The console example enables you to enter data into the cache through a C++ console, then read it out through a Java console. After you start the console example (by running run console), you are provided with the familiar Map(?): prompt from the console. The C++ console supports a subset of the commands available from Java. Enter the help command to get a list of available commands. The caches are defined within the extend-cache-config.xml configuration file Ensure that local-* caches are local only and dist-* caches are remote and use PIF/POF. Using near-* pulls remote data into an in-process coherent near cache.
Enter cache dist-hello to connect to the cache. Enter the commands illustrated in the following example to enter data into the cache and display it.
Map(?): cache dist-hello Map(dist-hello): put hello world NULL Map(dist-hello): get hello world Map(dist-hello): size 1 Map(dist-hello): put from C++ NULL Map(dist-hello): list from = C++ hello = world Map(dist-hello):
Launch a Java console to interact with the C++ console. Note that in the startup command, the Java client application must point to the same cache configuration as the C++ client. For example, on Windows, open a new command prompt window and execute the following command. (Note, the command is broken into two lines for formatting purposes).
c:\coherence\lib> java -Dtangosol.coherence.cacheconfig= c:\coherence\coherence-cpp\examples\config\extend-cache-config.xml -jar coherence.jar
Use the same console syntax that you used in the C++ console to access the cache. For example, on Windows, open a new command prompt window and execute the commands illustrated in the following figure:
Map(?): cache dist-hello
2008-04-25 09:01:02.207 Oracle Coherence GE 3.4/396 Alpha <D5> (thread=DistributedCache, member=3): Service
DistributedCache joined the cluster with senior service member 1
2008-04-25 09:01:02.239 Oracle Coherence GE 3.4/396 Alpha <D5> (thread=DistributedCache, member=3): Service
DistributedCache: received ServiceConfigSync containing 259 entries
<distributed-scheme>
<scheme-name>example-distributed</scheme-name>
<service-name>DistributedCache</service-name>
<lease-granularity>member</lease-granularity>
<backing-map-scheme>
<local-scheme//>
</backing-map-scheme>
<autostart>true</autostart></distributed-scheme>
2008-04-25 09:01:02.264 Oracle Coherence GE 3.4/396 Alpha <D4>
(thread=DistributedCache, member=3): Asking member 1 for 128 out of 128 primary
partitions
Map(dist-hello): list
from = C++
hello = world
Map(dist-hello):
Now that you've run the example, you are encouraged to have a look at the code. Each sample has a corresponding directory under examples which contains its sample specific source. There is also a common directory which contains source used in all samples.
The contact example enables you to enter names and addresses into the cache, then query to display the entries. The following commands can be run from the example:
help—returns a list of commands that the example can run
bye—stops the example and returns you to the command prompt
create—responds with prompts for a person's contact information: name, street address, city, state, zip code
find—prompts you for a name. The example returns the contact information associated with the name.
Follow these steps to build and run the contacts example:
C:\coherence\coherence-cpp\examples>build contacts building contacts\contacts.exe ... contacts.cpp ContactInfo.cpp ContactInfoSerializer.cpp Generating Code... C:\coherence\coherence-cpp\examples>
Run the contacts example. The window displays output similar to the following:
C:\coherence\coherence-cpp\examples>run contacts contacts> help commands are: bye create find <street | city | state | zip | all> contacts>
Exercise the example by entering the commands help, create, find, and bye.
contacts> help commands are: bye create find <street | city | state | zip | all> contacts> create Name: Tom Street: Oracle Parkway City: Redwood Shores State: California Zip: 94065 storing: ContactInfo(Name=Tom, Street=Oracle Parkway, City=Redwood Shores, State =California, Zip=94065) contacts> find Name: Tom ContactInfo(Name=Tom, Street=Oracle Parkway, City=Redwood Shores, State=California, Zip=94065) contacts> bye C:\coherence\coherence-cpp\examples>
Now that you've run the example, you are encouraged to have a look at the code. Each sample has a corresponding directory under examples which contains its sample specific source. There is also a common directory which contains source used in all samples.