Integrate Hazelcast IMDG to Spring Boot
Learn how to integrate Hazelcast IMDG to a Spring Boot application.
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.
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.
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.
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-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.
@Beanwith Hazelcast configuration in the source code.
In this guide, I choose
Test our work
In order to test our work, you should enable the debug mode by setting
The log is showing that an Hazelcast server is running at localhost with client port 5701.
The conditions evaluation report is showing that
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.
The Java Community Process(SM) Program - community process - implementations
Once a JSR has posted a Final Release of the Specification, the Maintenance Lead supplies a list of all implementations…
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
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.
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.