GridGain Developers Hub

Starting With Embedded Mode

In most scenarios, you would use GridGain CLI tool to start and manage your GridGain cluster. However, in some scenarios it is preferable to manage the cluster from a Java project. Starting and working with the cluster from code is called "embedded mode".

This tutorial covers how you can start GridGain 9 from your Java project.

Prerequisites

GridGain 9.0.0 was officially tested on:

JDK

11 and later

OS

Linux (Debian and Red Hat flavours), Windows 10 or 11

ISA

x86 or x64

Add GridGain to Your Project

First, you need to add GridGain to your project. The easiest way to do this is add GridGain to your project is by using Maven:


<dependency>
  <groupId>org.gridgain</groupId>
  <artifactId>ignite-api</artifactId>
  <version>9.0.0</version>
</dependency>

<dependency>
  <groupId>org.gridgain</groupId>
  <artifactId>ignite-runner</artifactId>
  <version>9.0.0</version>
</dependency>

Prepare GridGain Configuration

To start a GridGain node, you will need a GridGain configuration file that specifies all configuration properties of the node. For this tutorial, we recommend installing GridGain 9 and using a default configuration file from it. This file is stored in the gridgain9-db-9.0.0/etc/gridgain-config.conf file.

Pass JVM Parameters

The following JVM parameters need to be passed to your application to make proprietary SDK APIs available:

--add-opens java.base/java.lang=ALL-UNNAMED
--add-opens java.base/java.lang.invoke=ALL-UNNAMED
--add-opens java.base/java.lang.reflect=ALL-UNNAMED
--add-opens java.base/java.io=ALL-UNNAMED
--add-opens java.base/java.nio=ALL-UNNAMED
--add-opens java.base/java.math=ALL-UNNAMED
--add-opens java.base/java.util=ALL-UNNAMED
--add-opens=java.base/java.time=ALL-UNNAMED
--add-opens java.base/jdk.internal.misc=ALL-UNNAMED
--add-opens java.base/jdk.internal.access=ALL-UNNAMED
--add-opens java.base/sun.nio.ch=ALL-UNNAMED
-Dio.netty.tryReflectionSetAccessible=true

Start GridGain Server Nodes

To start a GridGain node, use the following code snippet:

IgniteServer node = IgniteServer.start("node", configFilePath, workDir);

This code snippet starts a GridGain node with the name node1, that uses the configuration from the file specified in the configFilePath path parameter and uses the folder specified in the workDir path parameter to store data. When the node is started, this method returns an instance of IgniteServer class that can be used to work with the node.

Initiate a Cluster

Started nodes find each other by default, but they do not form an intractable cluster unless the cluster is initiated. You need to initiate the cluster to activate the node. If there are multiple nodes, once the cluster is activated, they will form a topology and automatically distribute workload between each other.

Use the code snippet below to initiate a cluster:

InitParameters initParameters = InitParameters.builder()
    .metaStorageNodeNames("node")
    .clusterName("cluster")
    .clusterConfiguration({license-string})
    .build();

node.initCluster(initParameters);

Get an Ignite Instance

Now that the cluster is started, you can get an instance of the Ignite class:

Ignite ignite = node.api();

This instance can be used to start working with the cluster. The future will be returned once the cluster is active.

In the following example, you interact with the cluster using the SQL API:

ignite.sql().execute(null, "CREATE TABLE IF NOT EXISTS Person (id int primary key, name varchar, age int);");
ignite.sql().execute(null, "insert into Person (id, name, age) values ('1', 'Person Man', '501'");
try (ResultSet<SqlRow> rs = ignite.sql().execute(null, "SELECT id, name, age from Person")) {
    while (rs.hasNext()) {
        SqlRow row = rs.next();
        System.out.println("    "
                + row.value(1) + ", "
                + row.value(2));
    }
}

More examples of working with GridGain can be found in the examples repository.

Next Steps

From here, you may want to: