Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A Look at the Hazelcast IBM Dynacache Provider

DZone's Guide to

A Look at the Hazelcast IBM Dynacache Provider

This glimpse of the Hazelcast Dynacache Provider shows how you can use this solution for caching within and throughout distributed JEE systems.

· Java Zone ·
Free Resource

Learn how to build stream processing applications in Java-includes reference application. Brought to you in partnership with Hazelcast.

IBM has a cache abstraction called Dynacache in Websphere Application Server (WAS) distributions. That abstraction is handy if you want to use a configurable built-in cache solution in your JEE application deployed into WAS environment.

The following is the basic retrieval of a built-in Dynacache provider via JNDI:

private static DistributedObjectCache cache = retrieveDynacache();
static DistributedObjectCache retrieveDynacache() {
    try {
        InitialContext ic = new InitialContext();
        return (DistributedObjectCache) ic.lookup("services/cache/distributedmap");
    } catch (NamingException e) {
        // catch the exception
    }
}


IBM Dynacache is used internally to improve the performance of WAS deployments. Here are the list of features available to boost performance.

Hazelcast Dynacache Provider

IBM WAS has two Dynacache providers:

  1. Default Provider
  2. eXtreme Scale Dynacache Provider

The Default Provider is the one that ships with IBM WAS and has limited capabilities. It only provides an in-process caching solution. If you are in a distributed environment, you may want to use a distributed caching solution. IBM has a solution to that — with its eXtreme Scale Dynacache Provider. In case you have an eXtreme Scale cluster, you can store your caches across the eXtreme Scale cluster.

Besides those solutions, Hazelcast has developed its own Dynacache Provider for users interested in keeping their cache data in Hazelcast Clusters. Hazelcast Dynacache Provider has been released as OSS under Apache 2.0 License.

Configuring in WAS Liberty Profile

Download and install WebSphere Liberty version 16.0.0.3 from here. From this point on, I will refer to the directory you have installed WebSphere Liberty as $LIBERTY_HOME.

Install the Hazelcast Dynacache plugin from the GitHub releases page and install it to Liberty as follows:

$LIBERTY_HOME/bin/featureManager install hazelcast-dynacache-0.2.esa


Create a server named server1:

$LIBERTY_HOME/bin/server create server1


Open up $LIBERTY_HOME/usr/servers/server1/server.xml in your text editor and replace its content with the following:

<server>
    <featureManager>
        <feature>servlet-3.1</feature>
        <feature>distributedMap-1.0</feature>
        <feature>webCache-1.0</feature>
        <!-- hazelcast-dynacache depends on this -->
        <feature>javaee-7.0</feature>
        <feature>usr:hazelcast-dynacache</feature>
    </featureManager>
    <httpEndpoint id="defaultHttpEndpoint" host="*" httpPort="9080">
        <tcpOptions soReuseAddr="true"/>
    </httpEndpoint>
    <logging traceSpecification="com.hazelcast.*=finest=enabled"
        traceFileName="trace.log" maxFileSize="20"
        maxFiles="20" traceFormat="BASIC"/>
</server>


Using Hazelcast Cache Provider

Clone Hazelcast Code Samples repository from GitHub and build a Dynacache Client project:

git clone https://github.com/hazelcast/hazelcast-code-samples.git ch hazelcast-code-samples/hazelcast-integration/dynacache mvn clean package


Copy the resulting WAR file under the dropins folder of the Liberty server we had created:

cp target/dynacache-client-0.1-SNAPSHOT.war $LIBERTY_HOME/usr/servers/server1/dropins/dynacache-client.war


Add the following to a file named jvm.options under $LIBERTY_HOME/usr/servers/server1/

-Dhazelcast.osgi.start=true
-Dhazelcast.client.config=<path to hazelcast-code-samples dir>/hazelcast-integration/dynacache/hz-client.xml


Next, we start a Hazelcast 3.8 cluster with the default configuration (which starts up a member on port 5701).

Start the WebSphere Liberty server:

$LIBERTY_HOME/bin/server start server1


Navigate to http://localhost:9080/dynacache-client on your browser and you will see the following output:

PutResult: the value
the key => the value
the key 2 => the value 2
Invalidation took place...
the key => null
the key2 => the value 2
SomeClass{field1='field 1', field2='field2updated'}


Note that our web application has no direct dependency on Hazelcast. It doesn’t contain hazelcast.jar. It uses the com.ibm.websphere.cache.DistributedMap interface of Dynacache to retrieve the underlying Hazelcast IMap.

Conclusion

If you have a JEE application that is using the Dynacache API, you can simply switch to a Hazelcast Dynacache provider to use Hazelcast as a distributed caching solution. Hazelcast Dynacache Provider currently supports the Hazelcast Client/Server topology, but you can always ask for feature requests by submitting a GitHub issue.

Learn how to build distributed stream processing applications in Java that elastically scale to meet demand- includes reference application.  Brought to you in partnership with Hazelcast.

Topics:
dynacache ,distributed systems ,caching ,tutorial ,java

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}