diff --git a/spring/pom.xml b/spring/pom.xml index 96f2b5d66..4bdd709bd 100644 --- a/spring/pom.xml +++ b/spring/pom.xml @@ -42,6 +42,7 @@ spring-data-jpa-hazelcast-migration spring-hibernate-2ndlevel-cache springaware-annotation + spring-boot-caching-jcache diff --git a/spring/spring-boot-caching-jcache/README.md b/spring/spring-boot-caching-jcache/README.md new file mode 100644 index 000000000..cc6f0b4c6 --- /dev/null +++ b/spring/spring-boot-caching-jcache/README.md @@ -0,0 +1,3 @@ +# Caching with Spring Boot, JCache and Hazelcast + +See the link:https://docs.hazelcast.com/tutorials/caching-springboot-jcache[tutorial]. diff --git a/spring/spring-boot-caching-jcache/pom.xml b/spring/spring-boot-caching-jcache/pom.xml new file mode 100644 index 000000000..e94199ef1 --- /dev/null +++ b/spring/spring-boot-caching-jcache/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.4.0 + + + + com.hazelcast.samples + spring-boot-caching-jcache + 0.1-SNAPSHOT + + Caching with Spring Boot, JCache and Hazelcast + + + + org.springframework.boot + spring-boot-starter-cache + + + org.springframework.boot + spring-boot-starter-web + + + + + com.hazelcast + hazelcast + 5.5.0 + + + + + javax.cache + cache-api + 1.1.1 + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring/spring-boot-caching-jcache/src/main/java/com/hazelcast/springboot/caching/Application.java b/spring/spring-boot-caching-jcache/src/main/java/com/hazelcast/springboot/caching/Application.java new file mode 100644 index 000000000..9dafb11c6 --- /dev/null +++ b/spring/spring-boot-caching-jcache/src/main/java/com/hazelcast/springboot/caching/Application.java @@ -0,0 +1,31 @@ +/* + * + * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.hazelcast.springboot.caching; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; + +@SpringBootApplication +@EnableCaching +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring/spring-boot-caching-jcache/src/main/java/com/hazelcast/springboot/caching/BookController.java b/spring/spring-boot-caching-jcache/src/main/java/com/hazelcast/springboot/caching/BookController.java new file mode 100644 index 000000000..fd9ec2688 --- /dev/null +++ b/spring/spring-boot-caching-jcache/src/main/java/com/hazelcast/springboot/caching/BookController.java @@ -0,0 +1,24 @@ +package com.hazelcast.springboot.caching; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +// tag::BookController[] +@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); + } +} +// end::BookController[] diff --git a/spring/spring-boot-caching-jcache/src/main/java/com/hazelcast/springboot/caching/BookService.java b/spring/spring-boot-caching-jcache/src/main/java/com/hazelcast/springboot/caching/BookService.java new file mode 100644 index 000000000..8a8d03c69 --- /dev/null +++ b/spring/spring-boot-caching-jcache/src/main/java/com/hazelcast/springboot/caching/BookService.java @@ -0,0 +1,26 @@ +package com.hazelcast.springboot.caching; + +import org.springframework.stereotype.Service; + +import javax.cache.annotation.CacheResult; + +// tag::BookService[] +@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"; + } +} +// end::BookService[] diff --git a/spring/spring-boot-caching-jcache/src/main/resources/application-embedded.properties b/spring/spring-boot-caching-jcache/src/main/resources/application-embedded.properties new file mode 100644 index 000000000..97178ce5d --- /dev/null +++ b/spring/spring-boot-caching-jcache/src/main/resources/application-embedded.properties @@ -0,0 +1,3 @@ +spring.cache.type=jcache +spring.cache.jcache.provider=com.hazelcast.cache.impl.HazelcastServerCachingProvider +spring.hazelcast.config=classpath:hazelcast.yaml diff --git a/spring/spring-boot-caching-jcache/src/main/resources/application.properties b/spring/spring-boot-caching-jcache/src/main/resources/application.properties new file mode 100644 index 000000000..a4cff182b --- /dev/null +++ b/spring/spring-boot-caching-jcache/src/main/resources/application.properties @@ -0,0 +1,3 @@ +spring.cache.type=jcache +spring.cache.jcache.provider=com.hazelcast.client.cache.HazelcastClientCachingProvider +spring.hazelcast.config=classpath:hazelcast-client.yaml diff --git a/spring/spring-boot-caching-jcache/src/main/resources/hazelcast-client.yaml b/spring/spring-boot-caching-jcache/src/main/resources/hazelcast-client.yaml new file mode 100644 index 000000000..649e3adf6 --- /dev/null +++ b/spring/spring-boot-caching-jcache/src/main/resources/hazelcast-client.yaml @@ -0,0 +1,4 @@ +hazelcast-client: + network: + cluster-members: + - 127.0.0.1 diff --git a/spring/spring-boot-caching-jcache/src/main/resources/hazelcast.yaml b/spring/spring-boot-caching-jcache/src/main/resources/hazelcast.yaml new file mode 100644 index 000000000..95156a6cd --- /dev/null +++ b/spring/spring-boot-caching-jcache/src/main/resources/hazelcast.yaml @@ -0,0 +1,4 @@ +hazelcast: + cache: + books: + management-enabled: true diff --git a/spring/spring-boot-caching-jcache/src/test/java/com/hazelcast/springboot/caching/ApplicationClientServerTest.java b/spring/spring-boot-caching-jcache/src/test/java/com/hazelcast/springboot/caching/ApplicationClientServerTest.java new file mode 100644 index 000000000..f7acf4133 --- /dev/null +++ b/spring/spring-boot-caching-jcache/src/test/java/com/hazelcast/springboot/caching/ApplicationClientServerTest.java @@ -0,0 +1,45 @@ +package com.hazelcast.springboot.caching; + +import com.hazelcast.core.Hazelcast; +import com.hazelcast.core.HazelcastInstance; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.test.web.server.LocalServerPort; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +public class ApplicationClientServerTest { + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Autowired + private HazelcastInstance hazelcastInstance; + + @BeforeAll + public static void setUp() { + Hazelcast.newHazelcastInstance(); + } + + @Test + public void useCachedValue() { + // given + String isbn = "12345"; + String cachedValue = "cached-value"; + hazelcastInstance.getCacheManager().getCache("books").put(isbn, cachedValue); + + // when + String response = restTemplate.getForObject(String.format("http://localhost:%s/books/%s", port, isbn), String.class); + + // then + assertThat(response).isEqualTo(cachedValue); + } +} diff --git a/spring/spring-boot-caching-jcache/src/test/java/com/hazelcast/springboot/caching/ApplicationEmbeddedTest.java b/spring/spring-boot-caching-jcache/src/test/java/com/hazelcast/springboot/caching/ApplicationEmbeddedTest.java new file mode 100644 index 000000000..c490812c5 --- /dev/null +++ b/spring/spring-boot-caching-jcache/src/test/java/com/hazelcast/springboot/caching/ApplicationEmbeddedTest.java @@ -0,0 +1,40 @@ +package com.hazelcast.springboot.caching; + +import com.hazelcast.core.HazelcastInstance; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.test.context.ActiveProfiles; + +import static org.assertj.core.api.Assertions.assertThat; + +@ActiveProfiles("embedded") +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +public class ApplicationEmbeddedTest { + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Autowired + private HazelcastInstance hazelcastInstance; + + @Test + public void useCachedValue() { + // given + String isbn = "12345"; + String cachedValue = "cached-value"; + hazelcastInstance.getCacheManager().getCache("books").put(isbn, cachedValue); + + // when + String response = restTemplate.getForObject(String.format("http://localhost:%s/books/%s", port, isbn), String.class); + + // then + assertThat(response).isEqualTo(cachedValue); + } +} diff --git a/spring/springboot-caching-jcache/README.md b/spring/springboot-caching-jcache/README.md deleted file mode 100644 index 06d705f23..000000000 --- a/spring/springboot-caching-jcache/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Caching with Spring Boot and Hazelcast - -Moved to https://github.com/hazelcast-guides/springboot-jcache \ No newline at end of file