Coherence C++ client library
Coherence is a scalable, fault-tolerant, cloud-ready,
distributed platform for building grid-based applications and reliably storing data.
The product is used at scale, for both compute and raw storage, in a vast array of
industries such as critical financial trading systems, high performance telecommunication
products and eCommerce applications.
Typically these deployments do not tolerate any downtime and Coherence is chosen due to its
novel features in death detection, application data evolvability, and the robust,
battle-hardened core of the product that enables it to be seamlessly deployed and
adapted within any ecosystem.
At a high level, Coherence provides an implementation of the familiar Map
interface but rather than storing the associated data in the local process it is partitioned
(or sharded) across a number of designated remote nodes. This partitioning enables
applications to not only distribute (and therefore scale) their storage across multiple
processes, machines, racks, and data centers but also to perform grid-based processing
to truly harness the CPU resources of the machines.
The Coherence interface NamedCache
(an extension of Map
) provides methods
to query, aggregate (map/reduce style) and compute (send functions to storage nodes
for locally executed mutations) the data set. These capabilities, in addition to
numerous other features, enable Coherence to be used as a framework for writing robust,
distributed applications.
For more details on how to obtain and use Coherence, please see the Coherence CE README.
Coherence for C++ allows C++ applications to access Coherence clustered services, including data,
data events, and data processing from outside the Coherence cluster. Typical uses of Coherence
for C++ include desktop and web applications that require access to Coherence caches.
Coherence for C++ consists of a native C++ library that connects to a Coherence*Extend clustered
service instance running within the Coherence cluster using a high performance TCP/IP-based
communication layer. This library sends all client requests to the Coherence*Extend clustered
service which, in turn, responds to client requests by delegating to an actual Coherence
clustered service (for example, a partitioned or replicated cache service).
A NamedCache instance is retrieved by using the CacheFactory::getCache(...)
API call.
After it is obtained, a client accesses the NamedCache
in the same way as it would if it
were part of the Coherence cluster. The fact that NamedCache
operations are being sent to a
remote cluster node (over TCP/IP) is completely transparent to the client application.
NOTE: The C++ client follows the interface and concepts of the Java client, and
users familiar with Coherence for Java should find migrating to Coherence for C++ straightforward.
See Developing Remote Clients for Oracle Coherence
for further details.
The Coherence for C++ build system is based on Ant. To build Coherence for C++:
tools/internal/common/ant
tools/internal/common/ant-contrib/lib
tools/internal/common/ant-contrib/lib
JAVA_HOME
environment variable to point to the Oracle JDK 8 homecd bin
cfglocal.sh
from a bash shell (e.g. . cfglocal.sh
). On windows open a Visual Studio native tools command prompt and run cfgwindows.cmd
cd ../prj/coherence
ant -Dbuild.type=release clean build dist
- can take from 20 minutes to over an hour depending on platform type and CPU speedThe resulting files:
dist/14.1.2.0.0b0/include
- the public header filesdist/14.1.2.0.0b0/lib
- the Coherence for C++ shared libraryThe following example illustrates starting a storage enabled Coherence Server,
followed by a Coherence for C++ console application. Using the console, data is
inserted and retrieved. The console is then terminated and restarted and data is once again
retrieved to illustrate the permanence of the data.
Sanka is a command line tool which can be used to run the main
method on Coherence C++ classes
JAVA_HOME
environment variable to point to the Oracle JDK 8 homecd bin
cfglocal.sh
from a bash shell (e.g. . cfglocal.sh
). On windows open a Visual Studio native tools command prompt and run cfgwindows.cmd
cd ../prj/sanka
ant -Dbuild.type=release
build distUnix shell:
```shell script
$JAVA_HOME/bin/java -Dcoherence.pof.enabled=true -Dcoherence.log.level=6 -jar coherence.jar
Windows command prompt:
```shell script
"%JAVA_HOME%/bin/java" -Dcoherence.pof.enabled=true -Dcoherence.log.level=6 -jar coherence.jar
coherence:
:main()
(console) using sankaUnix shell:
```shell script
cd dist/14.1.2.0.0b0/bin
./sanka -l ../lib/libcoherence.so coherence::CacheFactory
Windows command prompt:
```shell script
.\sanka -l ..\lib\coherence.dll coherence::net::CacheFactory
Console:
```shell script
Map (?): cache welcomes
Map (welcomes): get english
NULL
Map (welcomes): put english Hello
NULL
Map (welcomes): put spanish Hola
NULL
Map (welcomes): put french Bonjour
NULL
Map (welcomes): get english
Hello
Map (welcomes): list
french = Bonjour
english = Hello
spanish = Hola
Map (welcomes): bye
Start the console again
Unix shell:
```shell script
./sanka -l ../lib/libcoherence.so coherence::net::CacheFactory
Windows command prompt:
```shell script
.\sanka -l ..\lib\coherence.dll coherence::CacheFactory
Console:
```shell script
Map (?): cache welcomes
Map (welcomes): list
french = Bonjour
english = Hello
spanish = Hola
The following example illustrates starting a storage enabled Coherence server,
followed by running the HelloCoherence
application. The HelloCoherence
application
inserts and retrieves data from the Coherence server.
HelloCoherence
Copy and paste the following source to a file named HelloCoherence.cpp
:
#include "coherence/lang.ns"
#include "coherence/net/CacheFactory.hpp"
#include "coherence/net/NamedCache.hpp"
#include "coherence/util/Iterator.hpp"
#include <iostream>
using namespace coherence::lang;
using coherence::net::CacheFactory;
using coherence::net::NamedCache;
using coherence::util::Iterator;
int main()
{
try
{
// access/create the "welcomes" cache in the Coherence cluster
NamedCache::Handle hCache = CacheFactory::getCache("welcomes");
std::cout << "Accessing cache \"" << hCache->getCacheName()
<< "\" containing " << hCache->size() << " entries"
<< std::endl;
hCache->put(String::create("english"), String::create("Hello"));
hCache->put(String::create("spanish"), String::create("Hola"));
hCache->put(String::create("french"), String::create("Bonjour"));
// list
for (Iterator::Handle hIterator = hCache->entrySet()->iterator();
hIterator->hasNext(); )
{
std::cout << hIterator->next() << std::endl;
}
// disconnect from the Coherence cluster
CacheFactory::shutdown();
}
catch (const std::exception& e)
{
std::cerr << "error: " << e.what() << std::endl;
return 1;
}
return 0;
}
Compile HelloCoherence.cpp
using the Coherence for C++ header files and shared library:
Using GCC on Linux and macOS:
```shell script
g++ -Idist/14.1.2.0.0b0/include -lcoherence -L dist/14.1.2.0.0b0/lib -o HelloCoherence HelloCoherence.cpp
From a Visual Studio native tools command prompt on Windows:
```shell script
cl -Idist\14.1.2.0.0b0\include dist\14.1.2.0.0b0\lib\coherence.lib -o HelloCoherence HelloCoherence.cpp
Unix shell:
```shell script
$JAVA_HOME/bin/java -Dcoherence.pof.enabled=true -Dcoherence.log.level=6 -jar coherence.jar
Windows command prompt:
```shell script
"%JAVA_HOME%/bin/java" -coherence.pof.enabled=true -Dcoherence.log.level=6 -jar coherence.jar
HelloCoherence
Unix shell:
```shell script
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:dist/14.1.2.0.0b0/lib
./HelloCoherence
Windows command prompt:
```shell script
set PATH=%DEV_ROOT%\dist\14.1.2.0.0b0\lib;%PATH%
HelloCoherence
Coherence for C++ has unit and functional tests to validate builds and code changes.
tools/internal/common/cxxtest
-Werror
flag in prj/build-import.xml
<!-- compilerarg if="cc.g++" value="-Werror"/ -->
) as CxxTest sourceJAVA_HOME
environment variable to point to the Oracle JDK 8 homecd bin
cfglocal.sh
from a bash shell (e.g. . cfglocal.sh
). On windows open a Visual Studio native tools command prompt and run cfgwindows.cmd
shell script
cd ../prj/tests/unit (or cd ../prj/tests/functional)
ant -Dbuild.type=release build test
Interested in contributing? See our contribution guidelines for details.
Please consult the security guide for our responsible security vulnerability disclosure process