Skip Headers
Oracle® Coherence Client Guide
Release 3.7.1

Part Number E22839-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

16 Sample C++ Application

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:

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:

The following sections are included in this chapter:

16.1 Prerequisites for Building and Running the Sample Applications

The requirements for running a sample include:

16.2 Starting a Coherence Proxy Service and Cache Server

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 the examples/java/ContactCache directory.

16.3 Building the Sample Applications

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.

16.4 Starting a Sample 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.

16.5 Running the hellogrid Example

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.

16.6 Running the console Example

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.

  1. 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):
    
    
  2. 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
    
    
  3. 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):
    
  4. 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.

16.7 Running the contacts Example

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:

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>
  1. 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>
    
  2. 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>
    
  3. 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.