Hazelcast Cluster

Hazelcast Cluster

Hazelcast Cluster with Jelastic PaaS

Hazelcast is a platform designed specially for sharing and distributing the data inside the application cluster. In this tutorial we will show how to configure Hazelcast clustering inside the Jelastic cloud.

Create an environment

  • Log in to
    Jelastic Manager
  • Click the Create environment button to open the topology wizard. Pick up Tomcat 7 as an application server. Enable Public IP. Turn on High-availability. This will pair up the nodes and create Tomcat cluster in such a way. The NGINX load balancer will be added automatically. Set the resource limits within cloudlet sliders for the both instances and click Create.hazelcast environment

In about a minute your environment will appear at the dashboard.

Download Hazelcast package

  • Open the official web-site and download the latest stable Hazelcast platform release.

download hazelcast

  • Extract the downloaded .zip package.

Create the application

  • Let’s use the Hazelcast cluster to create a simple Java server and a client application.
    Java server
  • Create a new Java class server application.
  • Import the libraries from the Hazelcast package you’ve downloaded before (they are located in hazelcast-x.x.x/lib folder). In this example we will work with application, which runs the first server and uses the customer’s queue and map:
package com;
import com.hazelcast.core.Hazelcast;
import java.util.Map;
import java.util.Queue;
public class HazelcastServer {
    public void run() {
        Map<Integer, String> mapCustomers = Hazelcast.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 queueCustomers = Hazelcast.getQueue("customers");
        System.out.println("First customer: " + queueCustomers.poll());
        System.out.println("Second customer: " + queueCustomers.peek());
        System.out.println("Queue size: " + queueCustomers.size());
  • Create the listener for your servlet, which will be used for automatic running your code after it was deployed.

package com;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class AppServletContextListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("ServletContextListener destroyed");
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("ServletContextListener started");
new HazelcastServer().run();

  • Modify the web.xml file by adding the < listener > section to it.

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

Finally, build your .war project file.

Client app

Use the Native Hazelcast Java Client API to create a new Java class.

The following sample performs connection to all the cluster’s nodes and shows the amount of customers in the map:

package com;
import com.hazelcast.client.ClientConfig;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
public class HazelcastClientClass {
    public static void main(String[] args) {
        ClientConfig clientConfig1 = new ClientConfig();
        ClientConfig clientConfig2 = new ClientConfig();
        HazelcastInstance client1 = HazelcastClient.newHazelcastClient(clientConfig1);
        HazelcastInstance client2 = HazelcastClient.newHazelcastClient(clientConfig2);
        IMap map1 = client1.getMap("customers");
        IMap map2 = client2.getMap("customers");
        System.out.println("Map1 Size:" + map1.size());
        System.out.println("Map2 Size:" + map2.size());

Note: To see Public IPv4 addresses of your nodes click the additional button for the cluster.

hazelcast ip

In the case your cluster includes 2 different nodes the port number for each of them should be the similar.

Application deployment

  • Navigate back to the dashboard and use Deployment Manager for uploading earlier created .war application.

hazelcast ip

  • Deploy the uploaded package to the environment with Tomcat cluster.

hazelcast ip

  • Then press the Config button next to the Tomcat nodes to open the configuration manager. Navigate to the lib folder and upload hazelcast-all-2.6.jar file there.

hazelcast configuration

  • To apply the new configurations restart your application servers by clicking Restart node button.

restart hazelcast

  • To make sure your Hazelcast cluster was successfully created open the Tomcat server logs.

hazelcast cluster

  • To apply the new configurations restart your application servers by clicking Restart node button.

restart hazelcast

  • cAs it is shown in the picture above, both instances of Tomcat server were clustered and Hazelcast server apps can communicate between themselves.
  • Run your client application locally. It will connect to each of server nodes and display the map size for both of them

.hazelcast client

Now you have your own clustered highly reliable and scalable Hazelcast hosting.