Having hazelcast-
<version>
.jar
added to your classpath, it is time to get started.
In this short tutorial, we will:
Let`s begin.
customers
map and queue.import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import java.util.Map;
import java.util.Queue;
public class GettingStarted {
public static void main(String[] args) {
Config cfg = new Config();
HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
Map<Integer, String> mapCustomers = instance.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 = instance.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());
}
}
Members [2] {
Member [127.0.0.1:5701]
Member [127.0.0.1:5702] this
}
Now, add hazelcast-client-
<version>
.jar
to your classpath, too. This is required to be able to use a Hazelcast client.
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.config.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();
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
IMap map = client.getMap("customers");
System.out.println("Map Size:" + map.size());
}
}
Hazelcast also offers a tool, Management Center, that enables monitoring your cluster. To be able to use it, deploy the mancenter-
<version>
.war
included in the ZIP file to your web server. You can use it to monitor your maps, queues, other distributed data structures and nodes. Please see Management Center for usage explanations.
You can also check the video tutorials here.