Skip to content

Commit b089de2

Browse files
authored
Move Spring Boot Caching with Hazelcast Cache Manager back [DEX-298] (#691)
Some code samples were previously moved out into separate repositories. This makes them hard to maintain. Changes: - added hazelcast-spring dependency (needed because we no longer use hazelcast-all)
1 parent 2a8e42f commit b089de2

File tree

8 files changed

+181
-0
lines changed

8 files changed

+181
-0
lines changed

spring/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
</properties>
3838

3939
<modules>
40+
<module>spring-boot-caching-hazelcast-cache-manager</module>
4041
<module>spring-configuration</module>
4142
<module>spring-data-hazelcast-chemistry-sample</module>
4243
<module>spring-data-jpa-hazelcast-migration</module>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See the link:https://docs.hazelcast.com/tutorials/caching-springboot[tutorial].
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.4.0</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
12+
<groupId>com.hazelcast.samples</groupId>
13+
<artifactId>spring-boot-caching-hazelcast-cache-manager</artifactId>
14+
<version>0.1-SNAPSHOT</version>
15+
<packaging>jar</packaging>
16+
<name>Spring Boot Caching with Hazelcast Cache Manager</name>
17+
18+
<properties>
19+
<hazelcast.version>5.5.0</hazelcast.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-cache</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-web</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>com.hazelcast</groupId>
34+
<artifactId>hazelcast</artifactId>
35+
<version>${hazelcast.version}</version>
36+
</dependency>
37+
<!-- The hazelcast-spring dependency is required for com.hazelcast.spring.cache.HazelcastCacheManager-->
38+
<dependency>
39+
<groupId>com.hazelcast</groupId>
40+
<artifactId>hazelcast-spring</artifactId>
41+
<version>${hazelcast.version}</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-test</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-maven-plugin</artifactId>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
*
3+
* Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package com.hazelcast.springboot.caching;
20+
21+
import org.springframework.boot.SpringApplication;
22+
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.cache.annotation.EnableCaching;
24+
25+
@SpringBootApplication
26+
@EnableCaching
27+
public class Application {
28+
public static void main(String[] args) {
29+
SpringApplication.run(Application.class, args);
30+
}
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.hazelcast.springboot.caching;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
@RequestMapping("/books")
10+
public class BookController {
11+
12+
private final BookService bookService;
13+
14+
BookController(BookService bookService) {
15+
this.bookService = bookService;
16+
}
17+
18+
@GetMapping("/{isbn}")
19+
public String getBookNameByIsbn(@PathVariable("isbn") String isbn) {
20+
return bookService.getBookNameByIsbn(isbn);
21+
}
22+
}
23+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.hazelcast.springboot.caching;
2+
3+
import org.springframework.cache.annotation.Cacheable;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class BookService {
8+
@Cacheable("books")
9+
public String getBookNameByIsbn(String isbn) {
10+
return findBookInSlowSource(isbn);
11+
}
12+
13+
private String findBookInSlowSource(String isbn) {
14+
// some long processing
15+
try {
16+
Thread.sleep(3000);
17+
} catch (InterruptedException e) {
18+
Thread.currentThread().interrupt();
19+
throw new RuntimeException(e);
20+
}
21+
return "Sample Book Name, isbn: " + isbn;
22+
}
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
hazelcast:
2+
network:
3+
join:
4+
multicast:
5+
enabled: true
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.hazelcast.springboot.caching;
2+
3+
import com.hazelcast.core.HazelcastInstance;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
8+
import org.springframework.boot.test.web.client.TestRestTemplate;
9+
import org.springframework.boot.test.web.server.LocalServerPort;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
13+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
14+
public class ApplicationTest {
15+
16+
@LocalServerPort
17+
private int port;
18+
19+
@Autowired
20+
private TestRestTemplate restTemplate;
21+
22+
@Autowired
23+
private HazelcastInstance hazelcastInstance;
24+
25+
@Test
26+
public void useCachedValue() {
27+
// given
28+
String isbn = "12345";
29+
String cachedValue = "cached-value";
30+
hazelcastInstance.getMap("books").put(isbn, cachedValue);
31+
32+
// when
33+
String response = restTemplate.getForObject(String.format("http://localhost:%s/books/%s", port, isbn), String.class);
34+
35+
// then
36+
assertThat(response).isEqualTo(cachedValue);
37+
}
38+
}

0 commit comments

Comments
 (0)