Beginner’s Guide to Hazelcast Part 6

This is the sixth post in a series of posts about Hazelcast.  If one has not read the last five, please go to the table of contents post I have created to catch up.

Native Clients

After the last post I decided that I am going to go native.  Yep, I am going to demonstrate Hazelcast’s own Java client.  Java is not the only language native clients come in, C++ and C# flavors are available for the enterprise version.

Why Go Native?

It is a good question.  Native clients can keep one fixed into a product line without chance to escape.  Hazelcast rewards the one going native with the following:

  • The client is part of the cluster.  That means one can create places to stash data and listen for events going on in the cluster.  It also means that all of the tricks that have been discussed in my earlier posts can be used as a client.  This advantage cannot be underestimated.
  • The configuration file is similar.  This means that one doesn’t have to translate from the Hazelcast configuration file to client configuration file.  One can copy the file over and like magic it works.  The less translating one has to do, the less that gets lost.

The Any Client Rule of Thumb

Hazelcast clients are the easiest I have ever had the pleasure to set up and use.


This simple example is a continuation of a theme started by the last post, caching expensive operations.

Pom File

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">



This client becomes part of the cluster creates a IMap named “fibmap”. The fibonacci result is stored in the map if it hasn’t been calculated before. If one runs the client once, the results are stored in fibmap. The second time the client is run, the cached values are displayed.

package com.darylmathison.hazelcastjavaclient;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.core.HazelcastInstance;
import java.util.Map;

 * @author Daryl
public class Main {

     * @param args the command line arguments
    public static void main(String[] args) {
        HazelcastInstance instance = HazelcastClient.newHazelcastClient();
        Map<Long, Long> cacheMap = instance.getMap("fibmap");
        for(long i = 1; i <= 10L; i++) {
            System.out.println("value is " + fibonacci(i, cacheMap));
    private static long fibonacci(long rounds, Map<Long, Long> cacheMap) {
        Long cached = cacheMap.get(rounds);
        if(cached != null) {
            System.out.print("cached ");
            return cached;
        long[] lastTwo = new long[] {1, 1};
        for(int i = 0; i < rounds; i++) {
            long last = lastTwo[1];
            lastTwo[1] = lastTwo[0] + lastTwo[1];
            lastTwo[0] = last;
        cacheMap.put(rounds, lastTwo[1]);
        return lastTwo[1];



In this post I discussed reasons to use Hazelcast’s native Java client. I also showed a quick example of how to use one. The code can be found here.


When it comes to Beginner’s Guide to Hazelcast. I always am looking at and


3 thoughts on “Beginner’s Guide to Hazelcast Part 6

  1. Pingback: Beginner’s Guide to Hazelcast Table of Contents | Daryl Mathison's Java Blog

  2. Guang

    When running examples in part5 and part6 using spyMemcached client and hazelcast native client, from hazelcast manageCenter, I can see two maps(fibmap and another for memcached) created and working as backend cache server, caching datas, but from the manage center, I also see another tab named caches, as in my understanding, the example in part5 is creating a cache from the client side right? why it’s not listed in the Caches tab? What kinds of operations from client can create a real cache in the hazelcast cluster? I mean listed under the Caches tab from manage center.

    PS Your serials of articles for hazelcast beginners are really helping.


  3. Daryl Mathison Post author


    1> I am really sorry about not answering sooner, time got away from me.
    2> With the native client the code is really creating a map named fibMap, so in reality, it is not a cache but being used as one.
    3> In the memcached example, a map is being created and being used as a cache just like in the fibMap example.
    4> I am going to look into the Caches tab and see what that is really listing. That section may only be what hazelcast is creating for itself but that is my best guess.

Comments are closed.