Beginner’s Guide to Hazelcast Part 6

Standard

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.

Example

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="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.darylmathison</groupId>
    <artifactId>HazelcastJavaClient</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.3.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.darylmathison.hazelcastjavaclient.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast-client</artifactId>
            <version>3.3.2</version>
        </dependency>
    </dependencies>

</project>

Client

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));
        }
        instance.shutdown();
    }
    
    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];
     }

}

Conclusion

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.

Reference

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

Advertisements

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

  1. 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.

    Thanks!

  2. Guang,

    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.

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