Open Gitter Chat
FREE, online, self-paced, learning for Hazelcast at the Hazelcast Training Center. Learn More »

Get Started with Hazelcast IMDG

Create your first Hazelcast IMDG Application

In this short tutorial, we will create simple Java application using Hazelcast distributed map and queue. Then we will run our application twice to have two nodes (JVMs) clustered and finalize this tutorial with connecting to our cluster from another Java application by using Hazelcast Native Java Client API.

Note: When you create a new project in the latest version of IntelliJ, you will not see a “Command Line App” option. Instead, when the New Project window appears, select the “Java” option in the left sidebar, select your Java SDK version in the Project SDK, and click the “Next” button. Then check the “Create project from template” checkbox, select “Command Line App,” and click the “Next” button. Then you can fill in Project name, etc., and go through the rest of the tutorial.

  1. Download the latest Hazelcast IMDG.
  2. Unzip it and add the lib/hazelcast-3.x.jar to your class path.
  3. Create a Java class and import Hazelcast libraries.
  4. The following code will start the first node and create and use customers map and queue:
import com.hazelcast.core.*;
import com.hazelcast.config.*;

import java.util.Map;
import java.util.Queue;

public class GettingStarted {
    public static void main(String[] args) {
        Config cfg = new Config();
        HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
        Map<Integer, String> mapCustomers = instance.getMap("customers");
        mapCustomers.put(1, "Joe");
        mapCustomers.put(2, "Ali");
        mapCustomers.put(3, "Avi");

        System.out.println("Customer with key 1: "+ mapCustomers.get(1));
        System.out.println("Map Size:" + mapCustomers.size());

        Queue<String> queueCustomers = instance.getQueue("customers");
        System.out.println("First customer: " + queueCustomers.poll());
        System.out.println("Second customer: "+ queueCustomers.peek());
        System.out.println("Queue size: " + queueCustomers.size());
  • Run this class a second time to get the second node started.
  • Have you seen they formed a cluster? You should see something like this:
Members [2] {
    Member []
    Member [] this

Connecting Hazelcast Cluster with Java Client API

  • Besides hazelcast.jar you should also add hazelcast-client.jar to your classpath.
  • The following code will start a Hazelcast Client, connect to our two node cluster and print the size of our customers map.
package com.hazelcast.test;

import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;

public class GettingStartedClient {
    public static void main(String[] args) {
        ClientConfig clientConfig = new ClientConfig();
        HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
        IMap map = client.getMap("customers");
        System.out.println("Map Size:" + map.size());

When you run it, you will see the client properly connects to the cluster and print the map size as 3.

What’s Next

Get Hands-on Training

Sign up for hands-on training from Hazelcast experts, online or in the classroom.

Learn more at Hazelcast University

Hazelcast IMDG Deployment and Operations Guide

Download it Now