Using Spring Boot with JCache and Hazelcast
See something wrong? Edit this page.
This guide will get you started to use Hazelcast via JCache provider for your Spring Boot application.
You can see the whole project here.
What You’ll Learn
In this guide, you’ll deploy a Spring Boot application that uses JCache with Hazelcast implementation.
Spring Boot Application
To leverage JCache in your Spring Boot application, you will need to:
-
add
org.springframework.boot:spring-boot-starter-cache
dependency -
add
@EnableCaching
annotation to your main class -
add
@CacheResult(cacheName = "books")
annotation to every method you want to cache -
add
spring.cache.type=jcache
to yourapplication.properties
file
For more explanation on the Spring Boot and JCache topic, please check the related Spring Boot blog post: Cache Abstraction: JCache.
In our case, let’s have a simple web service with two classes defined as follows:
@RestController
@RequestMapping("/books")
public class BookController {
private final BookService bookService;
BookController(BookService bookService) {
this.bookService = bookService;
}
@GetMapping("/{isbn}")
public String getBookNameByIsbn(@PathVariable("isbn") String isbn) {
return bookService.getBookNameByIsbn(isbn);
}
}
@Service
public class BookService {
@CacheResult(cacheName = "books")
public String getBookNameByIsbn(String isbn) {
return findBookInSlowSource(isbn);
}
private String findBookInSlowSource(String isbn) {
// some long processing
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
return "Sample Book Name";
}
}
The idea is that every call to the endpoint /books/<isbn>
would go to the method findBookNameByIsbn()
, which would attempt to return cached results.
If there’s no cached value, the method would be executed and results cached.
Using Hazelcast as JCache Provider
We want to use Hazelcast as the JCache provider. The good news is that all you have to do is to add Hazelcast to your classpath:
<dependency> <groupId>com.hazelcast</groupId> <artifactId>hazelcast-all</artifactId> </dependency>
Then, you need to create a Hazelcast configuration with the books
map configured. You can define it as src/main/resources/hazelcast.yaml
.
hazelcast:
cache:
books:
management-enabled: true
Finally, you can configure your application to use Hazelcast. You can use either client-server or embedded topology.
Client-server Configuration
You first need to start a Hazelcast server with the Hazelcast configuration defined in the previous step.
docker run --rm -p 5701:5701 -v $(pwd)/src/main/resources:/hazelcast -e JAVA_OPTS="-Dhazelcast.config=/hazelcast/hazelcast.yaml" hazelcast/hazelcast
# Install Hazelcast CLI as described at
# https://github.com/hazelcast/hazelcast-command-line#installation
wget https://repo1.maven.org/maven2/javax/cache/cache-api/1.1.1/cache-api-1.1.1.jar
hz start -c src/main/resources/hazelcast.yaml -j cache-api-1.1.1.jar
Then, use the following application.properties
for your Spring Boot application.
spring.cache.type=jcache
spring.cache.jcache.provider=com.hazelcast.client.cache.HazelcastClientCachingProvider
spring.hazelcast.config=classpath:hazelcast-client.yaml
To start the application, run the following command.
mvn spring-boot:run
Embedded Hazelcast Configuration
If you prefer to run Hazelcast embedded in your Spring Boot application, then you need to use the following application-embedded.properties
file.
spring.cache.type=jcache
spring.cache.jcache.provider=com.hazelcast.cache.impl.HazelcastServerCachingProvider
spring.hazelcast.config=classpath:hazelcast.yaml
To start the application, run the following command.
mvn spring-boot:run -Dspring-boot.run.profiles=embedded
Testing the Application
You should see that your application is successfully connected to Hazelcast.
Members {size:1, ver:1} [ Member [172.30.63.9]:5701 - 75cd0b19-ee36-4e0a-9d9c-38c49f67f842 this ]
Then, you can test the application by executing the following command.
curl localhost:8080/books/12345 Sample Book Name
The first time you execute this command, it should take some time to get the response. However, when you try it again, it should be instant. That means that the cache is used.
curl localhost:8080/books/12345 Sample Book Name
What’s more?
If your want to learn more about Hazelcast as JCache provider, please check the official Hazelcast documentation: Hazelcast JCache.