Chapter 4. Sample Applications

The Spring GemFire project includes one sample application. Named "Hello World", the sample demonstrates how to configure and use GemFire inside a Spring application. At runtime, the sample offers a shell to the user for running various commands against the grid. It provides an excellent starting point for users unfamiliar with the essential components or the Spring and GemFire concepts.

The sample is bundled with the distribution and is included in the main solution file. You can also run it from the command line once it is built in visual studio.

4.1. Prerequisites

1. You will need to download, install, and obtain a license for

  • GemFire Enterprise 6.5

  • GemFire Enterprise Native Client 3.5.0

The GemFire Enterprise licence file, gemfireLicense.zip should reside in the root of your GemFire Enterprise install directory. The GemFire Enterprise Native Client licence file, gfCpplicense.zip, should reside in the 'bin' sub-directory of the Native Client install directory.

The .NET clients run in a client-server architecture. This is shown below from the perspective of a single 'native (C++ or .NET) client and a single cache server process.

Client-Server Configuration for native (C++ or .NET) clients
Figure 4.1. Client-Server Configuration for native (C++ or .NET) clients

While not shown in this picture, there can be multiple .NET/C++ client applications and also multiple other processes that act in a peer-to-peer like manner as part of the GemFire Distributed System.

When running in a client-server architecture there needs to be configuration of the client side, the .NET/C++ Application, and the data grid side, the Java Cache Server. In this example the configuration of the client side is done entirely by the FactoryObject's declared in the Spring XML file. The data grid side is configured with a standalone configuration file.

2. The Java Cache Server requires a configuration file in order to run. This file is provided in the Spring GemFire distribution and in named cache.xml. It is located in the 'example\Spring.Data.Gemfire.HelloWorld' directory. It needs to be into the GemFire Enterprise 6.0 'bin' directory where the cacheserver.bat file is located.

3. Run the cacheserver.bat file located in the 'bin' of the GemFire Enterprise 6.0 product. By default it will load a configuration file named 'cache.xml'.

The Spring GemFire project ships with the essential GemFire client side libraries to run the sample application. Note that because the .NET Client API is a wrapper around the C++ API, it needs to be referenced in either the App.config file or installed into the GAC. The HelloWorld example program is configured to load the libraries from the 'lib\GemFire\net\2.0' directory.

Despite providing the client However, it is recommended that you download the GemFire Enterprise Native Client 3.0.0.9 from the download site to have access to reference documentation. You may also find the GemFire .NET API Tour of interest to read.

4.2. Hello World

The Hello World sample demonstrates the basic functionality of the Spring GemFire project and is also useful to understand how GemFire clients work. The application bootstraps GemFire, configures it, allows for the execution of several commands against the data grid, and gracefully shuts down when the application exits. Multiple instances can be started at the same time as they will work with each other sharing data without any user intervention.

4.2.1. Compiling, starting and stopping the sample

Hello World is designed as a stand-alone application. The main class is in the file Program.cs and generates an executable named HelloWorld.exe. To start the example follow the steps

To compile the example, load the solution Spring.Data.GemFire.sln in the root of the Spring GemFire project directory.

  1. Load the Visual Studio 2008 solution Spring.Data.GemFire.sln located in the root of the Spring GemFire project directory. Compile the solution.

  2. Ensure that the Java Cache Server is running as described in the previous section.

  3. Set the startup project to be the Spring.Data.Gemfire.HelloWorld project or cd to the 'example\Spring.Data.Gemfire.HelloWorld\bin\Debug' directory and run HelloWorld.exe.

    [Note]Note

    You can pass as a command line argument the name that will appear before you enter commands in the shell. This is useful for distinguishing different members of the distributed system

  4. You can now execute commands in the shell, which will be described in the next section. Exit the shell by typing 'exit'.

4.2.2. Using the sample

Once started, the sample will create a client side cache that is replicated with the server cache contained in the Java Cache Server. For example, the command line 'HelloWorld.exe client-1' will result in the following greeting

Hello World!
Want to interact with the world ? ...
Supported commands are:

get <key> - retrieves an entry (by key) from the grid
put <key> <value> - puts a new entry into the grid
remove <key> - removes an entry (by key) from the grid
size - returns the size of the grid
clear - removes all mapping in the grid
keys - returns the keys contained by the grid
values - returns the values contained by the grid
containsKey <key> - indicates if the given key is contained by the grid
containsValue <value> - indicates if the given value is contained by the grid
map - returns a list of the key-value pairs in the grid

query <query> - executes a query on the grid

help - this info
exit - this node exists
client-1>

For example to add new items to the grid one can use:

client-1>put 1 unu
null
client-1>put 1 one
old value = [unu]
client-1>size
1
client-1>put 2 two
null
client-1>size
2
client-1>

Multiple instances can be created at the same time. Once started, the new clients automatically see the existing region and its information. Start a second client with the command line 'HelloWorld.exe client-2'

Hello World!
...

client-2>size
2
client-2>map
[2=two][1=one]
client-2>

Experiment with the example, start (and stop) as many instances as you want, run various commands in one instance and see how the others react. To preserve data, the Java Cache Servier needs to be running at all times.