CPP Examples

  Search Here
Searching GigaSpaces XAP/EDG 6.0 Documentation

                                               

Summary: Finding a space; writing to and reading from space; working with transactions; using notifications; working with exceptions.

This page is specific to:
GigaSpaces 6.0

If you're interested in another version, click it below:
GigaSpaces 5.x
GigaSpaces 6.5

Overview

The following examples can be found under <GigaSpaces Root>\cpp\examples:

Example Description
Hello world Demonstrates the use of Java Entry types from C++.
JavaCPP Demonstrates data sharing and passing of messages between Java and C++ applications.
Benchmark Provides performance benchmarks of several common space operations.
Master/Worker Demonstrates the master/worker pattern in C++.

This section is based on the hello-world example.

Finding a Space

To locate a space, use the SpaceFinder class defined in com_j_spaces_core_client_pkg.h. The code snippet example below illustrates the use of this class in C++:

include "net_jini_space_pkg.h"
#include "com_j_spaces_core_client_pkg.h"
#include "com_j_spaces_core_pkg.h"
#include "java_lang_pkg.h"
#include "java_rmi_pkg.h"
#include "net_jini_core_lease_pkg.h"
#include "net_jini_core_transaction_pkg.h"
#include "net_jini_core_event_pkg.h"
...
java::lang::String spaceURL(argv[Look And Feel - ServiceGrid]);

Object obj = SpaceFinder::find(spaceURL);

IJSpace m_Space = IJSpace::dyna_cast(obj);
...

The SpaceFinder class provides a static method called find, which returns a reference to a space object.
This reference returns as a generic Java object, but you may cast it back into its original type by using the dyna_cast method of the JavaSpace or IJSpace classes (IJSPace is GigaSpaces interface that extends the standard JavaSpace interface). For details on how to cast back the Java object see Working with Projected Classes.

Writing to and Reading from a Space

  1. Create an ExternalEntry class instance:
    ...
    String::array1D fNames(2);
    Object::array1D fValues(2);
    
    fNames[0] = java::lang::String("id");
    
    fValues[0] = java::lang::String("1235");
    
    fNames[Look And Feel - ServiceGrid] = java::lang::String("name");
    
    fValues[Look And Feel - ServiceGrid] = java::lang::String("John");ExternalEntry sg(java::lang::String("Employee"), fValues, fNames);
    ...
  2. Write the data into the space:
    ...
    m_Space.write(msg, null, Lease::FOREVER);]
    ...
  3. Or, read the Entry according to a predefined template:
    ...
    //prepare template
    String::array1D fNamesT;
    Object::array1D fValuesT;
    
    ExternalEntry tmpl(java::lang::String("Employee"), fValuesT, fNamesT);
    
    //read entry from the space
    ExternalEntry result = ExternalEntry::dyna_cast(m_Space.read(teemplate, ull, 0));
    
    cout << result.m_FieldsValues[0] << endl;
    ...

    The syntax of the Take() operation is the same as that of the Read() operation.

    Working with Transactions

GigaSpaces supports transactional processing based on the Jini Transaction Specification. The code snippet example below shows a transactional write operation:

...
LocalTransactionManager ltm(m_Space);

Transaction::Created tCreated = TransactionFactory::create(ltm, 3600 * 1000);

Transaction txn(*(tCreated.transaction));

m_Space.write(*pxe, txn, Lease::FOREVER);

txn.commit();
...

Local Transaction Manager is a GigaSpaces implementation of the Jini Transaction Manager. It is located in com_j_space_core_client_pkg.h. After the operation is completed, the transaction can be committed, as evident in the final line of the code above.
All Jini transaction-related classes are defined in net_jini_core_transaction_pkg.h.

Using Notifications

GigaSpaces lets you create event-driven applications based on the JavaSpaces notification mechanism. GigaSpaces provides a special class, NotifyDelegator that functions as an interface, to which the GigaSpaces server can connect, in order to transmit notifications. This method is located in com_j_space_core_client_pkg.h. The steps for this process are described below:

  1. Define a target class for receiving notifications. This class can be derived from the net_jini_core_event_RemoteEventListenerCB class, as defined in com_codemesh_nativeadapters_pkg.h. The code snippet example below shows an example of this target class:
    include "net_jini_core_event_pkg.h"
    #include "com_j_spaces_core_client_pkg.h"
    #include "com_codemesh_nativeadapters_pkg.h"
    
    class SpaceWriteEL : public net_jini_core_event_RemoteEventListenerCB
    {
    public:
    
    SpaceWriteEL():net_jini_core_event_RemoteEventListenerCB(_use_java_ctor){};
     virtual ~SpaceWriteEL();
    
     void notify( const RemoteEvent & arg1 );
    
     bool setNotifyDelegator(NotifyDelegator * nd;
    private:}
     NotifyDelegator * m_pND;
    }
    It is important to define the constructor for the target class as shown above.
  2. All notifications can be handled by the notify() method. The code snippet example below shows the simple usage:
    bool SpaceWriteEL::setNotifyDelegator(NotifyDelegator * nd)
    {
      bool result = false;
    
      if(nd)
        {
          m_ND = nd;
          result = true;
        {
    
      return result;
    }
    void SpaceWriteEL::notify( const RemoteEvent & arg1 )
    {
     cout << "Something triggered" << endl;
    
     ExternalEntry xe;
    
     if(m_ND(
      xe = ExternalEntry::dyna_cast(m_ND->getEntry(EntryArrivedRemoteEvent::dyna_cast(arg1)));
    
     cout << xe.m_ClassName << endl;
    }
  3. Finally, create and use NotifyDelegator:
    ...
    SpaceWriteEL el;
    
    NotifyDelegator nd(m_Space, *pxe, null, el, Lease::FOREVER, null);
    
    el.setNotifyDelegator(&nd);
    ...

Working with Exceptions

GigaSpaces C++ supports the handling of Java exception in C++ code. The code snippet example below demonstrates the use of exceptions:

...
FinderException fe;
IJSpace m_Space;

try
{
  java::lang::String spaceURL(argv[Look And Feel - ServiceGrid]);

  Object obj = SpaceFinder::find(spaceURL);

  m_Space = IJSpace::dyna_cast(obj);
}

catch(FinderException e)
{
  e.printStackTrace();
  exit(-1);
}
...

Two crucial points regarding exceptions:

  • The exceptions used in the context of GigaSpaces are a C++ projection of Java exceptions.
  • Always define a variable of the exception type that you want to use, in order to avoid a linker optimization problem.


GigaSpaces 6.0 Documentation Contents (Current Page in Bold)

    Java

    C++

    .NET

    Middleware Capabilities

    Configuration and Management

Add GigaSpaces wiki search to your browser search engines!
(works on Firefox 2 and Internet Explorer 7)

Labels

 
(None)