Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<module>spring-data-jpa-hazelcast-migration</module>
<module>spring-hibernate-2ndlevel-cache</module>
<module>springaware-annotation</module>
<module>spring-boot-caching-jcache</module>
</modules>

<dependencies>
Expand Down
3 changes: 3 additions & 0 deletions spring/spring-boot-caching-jcache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Caching with Spring Boot, JCache and Hazelcast

See the link:https://docs.hazelcast.com/tutorials/caching-springboot-jcache[tutorial].
63 changes: 63 additions & 0 deletions spring/spring-boot-caching-jcache/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.hazelcast.samples</groupId>
<artifactId>spring-boot-caching-jcache</artifactId>
<version>0.1-SNAPSHOT</version>

<name>Caching with Spring Boot, JCache and Hazelcast</name>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- tag::hazelcast[] -->
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>5.5.0</version>
</dependency>
<!-- end::hazelcast[] -->

<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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[]
Original file line number Diff line number Diff line change
@@ -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[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring.cache.type=jcache
spring.cache.jcache.provider=com.hazelcast.cache.impl.HazelcastServerCachingProvider
spring.hazelcast.config=classpath:hazelcast.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring.cache.type=jcache
spring.cache.jcache.provider=com.hazelcast.client.cache.HazelcastClientCachingProvider
spring.hazelcast.config=classpath:hazelcast-client.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
hazelcast-client:
network:
cluster-members:
- 127.0.0.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
hazelcast:
cache:
books:
management-enabled: true
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 0 additions & 3 deletions spring/springboot-caching-jcache/README.md

This file was deleted.

Loading