This example demonstrates the basic concepts behind GigaSpaces data grid.
There are two counter parts: a client and a server. The client, "HelloWorld.java" updates a data-grid with "Hello" and "World!" data entities and then reads them back.
A Plain Old Java Object (POJO) is the entity behind the updates to the data-grid. It consists of getters and setters for the 'msg' field, and a @SpaceId
for uniqueness (similar to a Map key).
Additional annotations can be applied - here are a couple:
@SpaceRouting
annotation can be applied to any field to allow routing to partition instances. If not specified, @SpaceId
will act as the routing field.@SpaceIndex
annotation can be applied to any field to allow indexing. @SpaceId
is by default indexed.This main class can either start a single data-grid instance (embedded) in it's JVM for easy development, or connect to an existing (remote) data-grid (by specifying its name).
This example is a standard maven project - Use maven to execute the main class:
mvn compile && mvn exec:java -Dexec.mainClass=HelloWorld -Dexec.args="-name myDataGrid -mode embedded"
Alternatively, if you have an IDE available, import the sources as a maven project using the POM:
examples/hello-world/pom.xml
The HelloWorld
main class accepts the following arguments: -name
{data-grid name} -mode
{embedded | remote}
Launch the HelloWorld
main (arguments: -name
myDataGrid -mode
embedded)
This will start an embedded data-grid followed by write and read of Message entities.
```
Created embedded data-grid: myDataGrid
write - 'Hello'
write - 'World!'
read - ['Hello', 'World!']
```
To connect to a remote data-grid, you need a cluster manager to host the data grid.
A data grid requires a cluster manager. The following cluster managers are available:
The "Service Grid" is recommended for beginners, which is what we'll show here. If you're using the open source package, you'll need to use the "Standalone" cluster manager, which is discussed later in this page.
Tip: The cluster manager includes a web-based UI which is started at http://localhost:8090
To start the service grid locally with a single container, run the bin/gs.(sh|bat)
as follows:
To deploy a data grid called myDataGrid
, run:
Now that we have a remote data-grid, we can connect to it.
Using maven:
mvn compile && mvn exec:java -Dexec.mainClass=HelloWorld -Dexec.args="-name myDataGrid -mode remote"
Using IDE:
Launch the HelloWorld
main (arguments: -name
myDataGrid -mode
remote)
This will connect your client to your remote data-grid followed by write and read of Message entities.
```
Connected to remote data-grid: myDataGrid
write - 'Hello'
write - 'World!'
read - ['Hello', 'World!']
```
To terminate the local service grid agent, run:
Use the same commands, but specify 2 containers (1 per instance), and add the --partitions
parameter:
Use the same commands, but specify 4 containers (1 per instance), and add the --ha
parameter:
Without the Service Grid, you will need to run the following commands using bin/gs.(sh|bat)
Each partition instance loads separately, as follows:
--partitions=2
for two partitions--instances=1_1
or --instances=2_1
for each partition instanceFrom the ${GS_HOME}/bin directory, run (in 2 separate terminals):
This will simulate a data-grid of 2 partitioned instances (without backups).
Each partition instance can be assigned a backup, as follows:
--partitions=2
for two partitions, --ha
for high availability meaning a single backup for each partition.--instances=1_1
to load primary of partition id=1, --instances=1_2
to load the backup instance of partition id=1First partition:
Second partition: