Hazelcast Java Example

In the previous post we saw how to install, run and use Hazelcast – A Open Source In Memory Data Grid NoSQL based distributed cache, in this post we shall see Hazelcast Java Example.

1. Project Configuration

Lets create a simple maven Java Project with the below pom.xml file. Here we include two dependencies for Hazelcast one is for the server and the other is the client dependency.

Below is the screenshot of the Project in Eclipse

Hazelcast_Project_Structure

2. Code a Java Server Program

Lets code a simple Server program which starts a Hazelcast instance, gets cache (creates one the first time) adds data to the cache and then prints the same.

on running the above program we get the following output.

Hazelcast_Server_1_Output

Note: Observe the below lines in the log which describes the number of nodes present in the distributed cache.

Hazelcast supports various data structures such as Map, Queue etc, we shall consider Map for our example. The above program starts a server process that keeps running.

3. Starting the second node and adding/updating cache

Lets modify the same program above but with some changes to the cache such as updating an entry and adding two new entries.

On running the above program we get the below output.

Hazelcast_Server_Two_Output

Note: Observe the below lines in the log which describes the number of nodes now two, the current one and the previous one present in the distributed cache.

4. Code a Java Client Program

Lets code a simple Hazelcast client program which accesses the hazelcast server gets the cache and reads/updates it.

The above program basically accesses the server, gets the cache  prints it, updates entry with key 5 and adds new entry and again prints the same.

Below is the output of the above program.

Hazelcast_Client_Program_Output

5. Check Distributed Property

Now lets check the distributed property of Hazelcast by stopping one of the server instances and verifying the data. On stopping one of the server instance below is the output seen in console of another server

We could see that the server instance running on port 5702 was stopped and the data shared on that node was rebalanced by the cluster.

Lets verify the data we added to the cluster by running the client program again, this time we just print and do nothing other than that.

Hazelcast_Client_Output

We could see that there are 6 entries in all and no data was lost when one of the node on the cluster went down.

Download Maven Project

I hope this has been useful for you and I’d like to thank you for reading. If you like this article, please leave a helpful comment and share it with your friends.

Comments

  1. By Vinay

    Reply

  2. By Abhinav

    Reply

    • By manju

      Reply

  3. By Ramesh

    Reply

    • By manju

      Reply

  4. By Jennifer

    Reply

  5. By siddhartha

    Reply

  6. By praveen

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *