Using Hazelcast ( In-Memory Data Grid )

Hazelcast is open source  in memory data grid that allows architects and developers easily design and develop faster, highly scalable and reliable applications.

Features

  • Distributed implementations of java.util.{Queue, Set, List, Map}.
  • Distributed Topic for publish/subscribe messaging.
  • Distributed listeners and events.
  • Dynamic clustering.
  • Dynamic fail-over.

Hazelcast is implemented in Java , it  uses Java NIO to implement non blocking I/O and it can dynamically discover and join the server cluster using IP Multicast .

Some use cases for  Hazelcast .

  • Designing highly scalable data store shared by multiple processes , multiple applications, or multiple servers.
  • Designing highly available distributed cache for your web application.
  • Designing distributed pub/sub.
  • Designing distributed queues.

Similar to Redis, Hazelcast can also be used to store session data in web application which can survive web server restarts and crashes. This setup also enables nice horizontal scalability of web application as shown below.

web-arch

As Event Router

One of the other uses case is to use it as distributed publish/subscribe server to  allows different application on the  same machine or across network to exchange messages in a loosely coupled fashion.

Following picture shows the architecture of such as system.  Clients ( publisher/subscribers ) connect to Event Router server to send/receive messages.

event-router

Server application source code

import com.hazelcast.core.*;
import com.hazelcast.config.Config;
import java.util.Map;
import java.util.Queue;
import java.io.*;
import java.util.*;
public class DemoRouter {	
    
    public static void main(String[] args) {
        
        Config cfg = new Config();		
        HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
        
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        try {
            in.readLine();
        }catch(Exception exp) { }
        
        System.out.println("Exiting ..");		
    }
}

Subscriber client application source code

import com.hazelcast.core.*;
import com.hazelcast.client.config.*;
import com.hazelcast.client.*;
import java.util.Map;
import java.util.Queue;
import java.io.*;
import java.util.*;

public class SubscribeClient {

    public static void main(String[] args) {
        
        ClientConfig clientConfig = new ClientConfig();
        clientConfig.addAddress("127.0.0.1:5701");
        
        HazelcastInstance hz = HazelcastClient.newHazelcastClient(clientConfig);
        ITopic topic = hz.getTopic("default");
        topic.addMessageListener(new MessageListener<String>() {		
            public void onMessage(Message<String> message) {			
                String myEvent = message.getMessageObject();
                System.out.println("Message received = " + myEvent.toString());
            }
        });		
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        try {
            in.readLine();
        }catch(Exception exp) { }
        System.out.println("Exiting ..");	
    }
}

Publisher  client application source code

import com.hazelcast.core.*;
import com.hazelcast.client.*;
import com.hazelcast.client.config.*;
import java.util.Map;
import java.util.Queue;
import java.io.*;
import java.util.*;

public class PublishClient {

    public static void main(String[] args) {
        ClientConfig clientConfig = new ClientConfig();
        clientConfig.addAddress("127.0.0.1:5701");
        
        HazelcastInstance hz = HazelcastClient.newHazelcastClient(clientConfig);
        ITopic topic = hz.getTopic("default");
        
        topic.publish("Hello from Client1");
        topic.publish("Hello from Client2");
        topic.publish("Hello from Client3");
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        try {
            in.readLine();
        } catch (Exception exp) {
        }
        System.out.println("Exiting ..");
    }
}

Resource

1. Hazelcast documentation 

2. Using Hazelcast screencast

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s