Want to try Hazelcast Platform?

We’ve combined the in-memory storage of IMDG with the stream processing power of Jet to bring you the all new Hazelcast Platform.

Starting the Members and Clients

Having installed Hazelcast and assuming you have the required libraries of your preferred client language, let’s see how we can start the members and clients.

Members:

  • Shell

  • Docker

  • Java

// Starts a standalone member:

sh bin/start.sh
docker run hazelcast/hazelcast:$HAZELCAST_VERSION
Config cfg = new Config();
HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
start.sh / start.bat scripts lets you start one Hazelcast instance per directory. To start a new instance, please unzip Hazelcast ZIP or TAR.GZ package in a new directory.
You can also use the start scripts to deploy your own library to a Hazelcast member. See the Adding User Library to CLASSPATH section.

Clients:

  • Java

  • C++

  • C Sharp

  • Node.js

  • Python

  • Go

ClientConfig clientConfig = new ClientConfig();
HazelcastInstance hzclient = HazelcastClient.newHazelcastClient(clientConfig);
hazelcast::client::ClientConfig config;
hazelcast::client::HazelcastClient hzclient(config);
var cfg = new ClientConfig();
var hzclient = HazelcastClient.NewHazelcastClient(cfg);
const { Client } = require('hazelcast-client');
const hzclient = await Client.newHazelcastClient();
config = hazelcast.ClientConfig()
hzclient = hazelcast.HazelcastClient(config)
config := hazelcast.NewConfig()
hzclient , _ := hazelcast.NewClientWithConfig(config)

Hazelcast also offers a tool, Management Center, that enables you to monitor your cluster. It is included in your Hazelcast IMDG download package and can also be downloaded from the Hazelcast website’s download page. You can use this tool to monitor your cluster, cluster members, clients, data structures and WAN replications. See the documentation for details on Hazelcast Management Center.

By default, Hazelcast uses multicast to discover other members that can form a cluster. If you are working with other Hazelcast developers on the same network, you may find yourself joining their clusters under the default settings. Hazelcast provides a way to segregate clusters within the same network when using multicast. See the Creating Clusters section for more information. Alternatively, if you do not wish to use the default multicast mechanism, you can provide a fixed list of IP addresses that are allowed to join. See the Join configuration section for more information.

If you prefer to use this mechanism, make sure that your network is enclosed and secure. See the Discovery Mechanisms section.
You can also check the video tutorials here.

Example Application

In this short tutorial, you perform the following activities:

  1. Create a simple Java application using the Hazelcast distributed map and queue.

  2. Run our application twice to have a cluster with two members (JVMs).

  3. Connect to our cluster from another Java application by using the Hazelcast Native Java Client API.

Let’s begin.

  • The following code starts the first Hazelcast member and creates and uses the customers map and queue.

            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");
            queueCustomers.offer("Tom");
            queueCustomers.offer("Mary");
            queueCustomers.offer("Jane");
            System.out.println("First customer: " + queueCustomers.poll());
            System.out.println("Second customer: "+ queueCustomers.peek());
            System.out.println("Queue size: " + queueCustomers.size());
  • Run the above code for the second time to get the second member started. The members form a cluster and the output is similar to the following.

    Members {size:2, ver:2} [
        Member [127.0.0.1]:5701 - e40081de-056a-4ae5-8ffe-632caf8a6cf1 this
        Member [127.0.0.1]:5702 - 93e82109-16bf-4b16-9c87-f4a6d0873080
    ]

    Here, you can see the size of your cluster (size) and member list version (ver). The member list version is incremented when changes happen to the cluster, e.g., a member leaving from or joining to the cluster.

  • Now, add the hazelcast-client-4.2.2.jar library to your classpath. This is required to use a Hazelcast client.

  • The following code starts a Hazelcast Client, connects to our cluster, and prints the size of the customers map.

    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 see the client properly connecting to the cluster and printing the map size as 3.