Table of Contents
Hazelcast is a clustering and highly scalable data distribution platform for Java. Hazelcast helps architects and developers to easily design and develop faster, highly scalable and reliable applications for their businesses.
Distributed implementations of
java.util.{Queue, Set, List, Map}
Distributed implementation of
java.util.concurrent.ExecutorService
Distributed implementation of
java.util.concurrency.locks.Lock
Distributed
Topic
for publish/subscribe messaging
Transaction support and J2EE container integration via JCA
Distributed listeners and events
Support for cluster info and membership events
Dynamic HTTP session clustering
Dynamic clustering
Dynamic scaling to hundreds of servers
Dynamic partitioning with backups
Dynamic fail-over
Super simple to use; include a single jar
Super fast; thousands of operations per sec.
Super small; less than a MB
Super efficient; very nice to CPU and RAM
To install Hazelcast:
Download hazelcast-_version_.zip from www.hazelcast.com
Unzip hazelcast-_version_.zip file
Add hazelcast.jar file into your classpath
Hazelcast is pure Java. JVMs that are running Hazelcast will dynamically cluster. Although by default Hazelcast will
use multicast for discovery, it can also be configured to only use TCP/IP for environments where multicast is not
available or preferred (Click here for more info). Communication among
cluster members is always
TCP/IP with Java NIO beauty. Default configuration comes with 1 backup so if one node fails, no data will be lost.
It is as simple as usingjava.util.{Queue, Set, List, Map}
. Just add the hazelcast.jar into your
classpath and start coding.
In this short tutorial, we will create simple Java application using Hazelcast distributed map and queue. Then we will run our application twice to have two nodes (JVMs) clustered and finalize this tutorial with connecting to our cluster from another Java application by using Hazelcast Native Java Client API.
Download the latest Hazelcast zip.
Unzip it and add the
lib/hazelcast.jar
to your class path.
Create a Java class and import Hazelcast libraries.
Following code will start the first node and create and use
customers
map and queue.
import com.hazelcast.core.Hazelcast; import java.util.Map; import java.util.Queue; public class GettingStarted { public static void main(String[] args) { 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<String> queueCustomers = Hazelcast.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 this class second time to get the second node started.
Have you seen they formed a cluster? You should see something like this:
Members [2] { Member [127.0.0.1:5701] Member [127.0.0.1:5702] this }
Connecting Hazelcast Cluster with Java Client API
Besides
hazelcast.jar
you should also add
hazelcast-client.jar
to your classpath.
Following code will start a Hazelcast Client, connect to our two node cluster
and print the size of our
customers
map.
package com.hazelcast.test; import com.hazelcast.client.ClientConfig; import com.hazelcast.client.HazelcastClient; import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.IMap; public class GettingStartedClient { public static void main(String[] args) { ClientConfig clientConfig = new ClientConfig(); clientConfig.addAddress("127.0.0.1:5701"); HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig); IMap map = client.getMap("customers"); System.out.println("Map Size:" + map.size()); } }
When you run it, you will see the client properly connects to the cluster and print the map size as 3.
What is Next?
You can browse documentation and resources for detailed features and examples.
You can email your questions to Hazelcast mail group.
You can browse Hazelcast source code.