Hazelcast in the cloud - The art of scaling

2013-07-18 - Reading time: 4 minutes #hazelcast #jelastic

Today’s applications are getting more and more distributed everyday and it is well-known that distributed programming is hard. With Hazelcast though, distributed programming is easy and lots of fun. Hazelcast is an open source, highly scalable, transactional, distributed/partitioned implementation of queue, map, set, list, lock and executor services for Java.

Hazelcast is for you if you like to easily: share data/state among many servers (e.g. web session sharing), cache your data (distributed cache), cluster your application, partition your in-memory data, send/receive messages among applications, distribute workload onto many servers, take advantage of parallel processing or provide fail-safe data management.

This post is fully focused on clustering. We’ll show you how you can use clustering and data distribution platform Hazelcast on Jelastic cloud. It’s very fast, fail-safe, highly scalable and user-friendly solution. If you need to share data among multiple servers, cluster your Java application, cache your data, provide your servers’ secure communication or fail-safe data management, Hazelcast is what you need.

Hazelcast uses multicast for discovery by default or TCP/IP for environments where multicast is not available (Our platform uses multicast). So, Jelastic is a perfect solution for your Hazelcast cluster hosting. Let’s take a look on how these two platforms work together.

Create the environment

  1. Go to elastx.com and click Get started now to create account or log in with your Jelastic credentials here.
  2. Create an environment with a Tomcat 7 HA cluster, set the cloudlet limit and switch on Public IPv4.

Wait a few minutes until your environment is created.

Download Hazelcast

  1. Navigate to hazelcast.com and download the latest stable release of the platform.

  1. Extract the files from the package you’ve just downloaded.

Create Server application

We will create a simple Java server and client apps using Hazelcast cluster for further deployment into the Jelastic environment we have created earlier.

  1. Create your server application Java class and import all the necessary Hazelcast libraries, which are included in the package you’ve downloaded (hazelcast-x.x.x/lib). In our case we use sample Hazelcast application, which starts the first server and uses the customer’s map and queue:

    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());
  2. Create your servlet listener, which will run your code automatically after its deployment.

    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();
  3. Add section to your web.xml file.

    <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">
  4. Build your project into a WAR archive.

Create Client application

Create the Java class using Hazelcast Native Java Client API. We use Hazelcast sample, which connects to both nodes in our cluster and displays the number 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: You can see your Public IPs by clicking on the additional button for your server nodes like it’s shown in the next screenshot:

The port number for each node should be the same for the case when you use two different nodes for your cluster.

Deploy application

  1. Go back to the Jelastic Dashboard and upload your server app WAR package to the Deployment Manager.

  1. Deploy it to the clustered environment you’ve created.

  1. Click the Config button for Tomcat and upload hazelcast-all-2.6.jarto the lib folder.

  1. Restart your servers in order to apply new settings.

  1. Check your server logs to ensure that your Hazelcast cluster was created.

Note: According to logs you can see that both our Tomcat instances have been successfully clustered and Hazelcast server applications successfully communicate between each other.

  1. Run you client application locally. You’ll see that client connects to both servers and gets the size of the map on each Hazelcast cluster node.

As you can see, Jelastiс was specially designed for highly scalable, clustered application hosting. That’s why it so easy to host such apps on our cloud platform. Now developers can save a lot of their time developing and deploying large clusters, especially when application is distributed.

This post was orginally posted by Marina Sprava at Jelastic, 17th July, 2013.

Henrik Grankvist

#hazelcast #jelastic