Hazelcast Hello World Example- Getting started with Hazelcast | JavaInUse



Hazelcast Hello World Example- Getting started with Hazelcast

In this tutorial we will create a simple application to get started with Hazelcast.

Video

This tutorial is explained in the below Youtube Video.

Lets Begin

In this example we set up a basic Hazelcast cluster. We will create multiple Hazelcast nodes.
We will create Eclipse maven project as follows-

Our pom file will be as follows-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.javainuse</groupId>
  <artifactId>hazelcast-helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <properties>
  	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies>
  	
    <dependency>
	  	<groupId>com.hazelcast</groupId>
  		<artifactId>hazelcast-client</artifactId>
  		<version>3.7.1</version>
  	</dependency>
  </dependencies>
  
  <build>
  	<plugins>
  		<plugin>
  			<artifactId>maven-compiler-plugin</artifactId>
  			<version>3.5.1</version>
  			<configuration>
  				<source>1.8</source>
  				<target>1.8</target>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
</project>  
We make use of XML configuration which is used by default. Hazelcast searches for hazelcast.xml on classpath.
By default the configuration makes use of multicast discover mechanism.
<hazelcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.hazelcast.com/schema/config
                               http://www.hazelcast.com/schema/config/hazelcast-config-3.6.xsd"
           xmlns="http://www.hazelcast.com/schema/config">

    <group>
        <name>MyCluster</name>
        <password>MyCluster</password>
    </group>
    
</hazelcast>
Next we start the Hazelcast instance. It by default loads the hazelcast.xml.
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

public class HazelcastMain {
	public static void main(String[] args) {
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
	}
}
Start the Hazelcast node instances by running the HazelcastMain.java. We can see that Hazelcast node with port 5701 gets started.
Run this program again- Another Hazel cast node with port 5702 gets started and added to the cluster.

By running this program multiple times we can add multiple Hazelcast nodes to our cluster. We can run multiple Hazelcast instances in one JVM. Its useful for testing.

Enabling TCP discover mechanism

In the hazelcast.xml we disable the default multicast discover mechanism and enable the TCP enabled hazelcast node discovery mechanism for the cluster.
<hazelcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.hazelcast.com/schema/config
                               http://www.hazelcast.com/schema/config/hazelcast-config-3.6.xsd"
	xmlns="http://www.hazelcast.com/schema/config">

	<group>
		<name>MyCluster</name>
		<password>MyCluster</password>
	</group>
	
	
	<network>
		<join>
			<multicast enabled="false" />
			<tcp-ip enabled="true">
				<member>192.168.10.10</member>
				<member>192.168.10.12</member>
			</tcp-ip>
		</join>
	</network>


</hazelcast>
Here we specify that the nodes with ip address 192.168.10.10 and 192.168.10.12 will get added to the cluster.For this the Hazelcast node should be up on these servers.

Download Source Code

Download it - Hazelcast Hello World application
	

See Also

Hazelcast Tutorial- Working with Hazelcast collections- Map,List,Set,Queue. Hazelcast Tutorial- Understanding Replicated Maps using example. Hazelcast - Main Menu