Integrate Hazelcast IMDG to Spring Boot

Learn how to integrate Hazelcast IMDG to a Spring Boot application.

Thanh Tran
Nov 15, 2020 · 5 min read

In another article, I have introduced to Spring Cache Abstraction. Following that article, In this article, I am going to show you how to configure Hazelcast in the Spring Boot application.

Before getting started with code implement, we should go through the basics of what is Hazelcast and Spring Cache Abstraction.

What is Hazelcast IMDG?

Hazelcast IMDG is an open-source distributed in-memory object store supporting a wide variety of data structures.

You can use Hazelcast IMDG to store your data in RAM, spread and replicate it across your cluster of machines, and perform computations on it. Replication gives you the resilience to failures of cluster members.

Hazelcast IMDG is highly scalable and available. Distributed applications can use it for distributed caching, synchronization, clustering, processing, pub/sub messaging, etc.

It is implemented in Java language and has clients for Java, C++, .NET, REST, Python, Go, and Node.js. Hazelcast IMDG also speaks Memcached and REST protocols. It plugs into Hibernate and can easily be used with any existing database system.

Hazelcast IMDG makes distributed computing simple by offering distributed implementations of many developer-friendly interfaces. For example, the Map interface provides an In-Memory Key Value store which confers many of the advantages of NoSQL in terms of developer friendliness and developer productivity.

Read more at https://docs.hazelcast.org/docs/latest/manual/html-single/#hazelcast-overview

Spring Cache Abstraction

Simply, Spring Cache Abstraction provides us a simple approach to enable caching for our spring application. It provides built-in useful annotations that aim to help developers manage cache easily. It’s similar to how the transaction works in Spring.

To have a detailed about Spring Cache Abstraction, I already explained about it in another article here is the link.

Integrating Hazelcast

Hazelcast can be integrated into Spring Application following two approaches. The first approach is integrating Hazelcast as Cache Manager and the second way is using Hazelcast as a Jcache Provider.

In this article, we are using spring-boot-autoconfigure and spring-context-support to accomplish our demo.

See how the demo structure looks like.

The idea of this demo is a book finder which is built on top of CommandLineRunner. The reason to choose CommandLineRunner is just to make the demo to be simpler, faster to run.

See how we enable CommandLineRunner:

For the purpose of this guide, you will simply use a naive implementation that simulates network latency.

Implementation for Book service

As I mentioned above, this guide shows two approaches to integrate Hazelcast into Spring Application, the following sections will show you how to do.

#1, Using Hazelcast as Cache Manager

We want to use Hazelcast as the cache manager for a spring application. The good news is that all you have to do is add Hazelcast to your classpath.

We also have to include spring-boot-starter-cache into pom.xml file. Because the spring-boot-starter-cache already includes spring-boot-starter and spring-context-support modules.

Add then, we need to add Hazelcast configuration in one of the following manners.

  • Add hazelcast.yaml configuration OR
  • Add hazelcast.xml configuration OR
  • Define @Bean with Hazelcast configuration in the source code.

In this guide, I choose hazelcast.xml:

Test our work

In order to test our work, you should enable the debug mode by setting debug=true for application.properties.

The log is showing that an Hazelcast server is running at localhost with client port 5701.

The conditions evaluation report is showing that HazelcastAutoConfiguration, HazelcastCacheConfiguration, HazelcastClientConfiguration, HazelcastServerConfiguration, HazelcastServerConfiguration.HazelcastServerConfigFileConfiguration are matched

Scroll to the bottom, we duration from the first execution of findBook and the next execution long 3 seconds, but the result of the next executions returned immediately.

#2, Using Hazelcast as JCache Provider

Hazelcast is one of the most popular JSR-107 Implementations. Therefore, we able to integrate Hazelcast to Spring Application as JCache Provider.

In order to apply Hazelcast as Jcache Provider, we need to add javax.cache:cache-api dependency to your classpath. Let edit the pom.xml.

At this time, both Hazelcast and Jcache are present in the classpath, I recommend that we specify which type of cache the application is used. In order to do that add the following line to the application.properties.

We also need to specify Hazelcast configuration so we need to add hazelcast.xml there is to define the cache policy and configuration for Hazelcast.

Test our work

The following console should be printed in the console output.

Give me motivation

To get new article update please follow our publication or follow us on social

Facebook: https://www.facebook.com/programmingsharing

Twitter: http://twitter.com/progsharing

source code: https://github.com/Programming-Sharing/spring-caching-demo/tree/hazelcast-spring-boot-integration-as-jcache-provider

Conclusion

By now, I already introduced what Hazelcast is also as what Spring Cache Abstraction is.

This guide has shown you how to integrate Hazelcast as Cache Manager. Another introduced approach is that integrating Hazelcast as a JCache Provider.

Programming Sharing

We write about programming. We share about programming.

Sign up for Programming Sharings Newsletter

By Programming Sharing

Get new articles update by signing up our newsletter Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Thanh Tran

Written by

Software Engineer at Terralogic. Blogger and Amateur Investor

Programming Sharing

The publication to share programming knowledge

Thanh Tran

Written by

Software Engineer at Terralogic. Blogger and Amateur Investor

Programming Sharing

The publication to share programming knowledge

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store