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 @@ -37,6 +37,7 @@
</properties>

<modules>
<module>spring-boot-embedded</module>
<module>spring-boot-caching-hazelcast-cache-manager</module>
<module>spring-configuration</module>
<module>spring-data-hazelcast-chemistry-sample</module>
Expand Down
1 change: 1 addition & 0 deletions spring/spring-boot-embedded/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See the link:https://docs.hazelcast.com/tutorials/hazelcast-embedded-springboot[tutorial].
51 changes: 51 additions & 0 deletions spring/spring-boot-embedded/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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/>
</parent>

<groupId>com.hazelcast.samples</groupId>
<artifactId>spring-boot-hazelcast-embedded</artifactId>
<version>0.1-SNAPSHOT</version>
<name>Spring Boot with Hazelcast Embedded</name>

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

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<scope>test</scope>
</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 @@
package guides.hazelcast.springboot;

import com.hazelcast.map.IMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static java.util.Objects.requireNonNull;

@RestController
public class CommandController {

private final IMap<String, String> keyValueMap;

public CommandController(IMap<String, String> keyValueMap) {
this.keyValueMap = requireNonNull(keyValueMap);
}

@PostMapping("/put")
public CommandResponse put(@RequestParam(value = "key") String key, @RequestParam(value = "value") String value) {
keyValueMap.put(key, value);
return new CommandResponse(value);
}

@GetMapping("/get")
public CommandResponse get(@RequestParam(value = "key") String key) {
String value = keyValueMap.get(key);
return new CommandResponse(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package guides.hazelcast.springboot;

public record CommandResponse(String value) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package guides.hazelcast.springboot;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class HazelcastApplication {

public static void main(String[] args) {
SpringApplication.run(HazelcastApplication.class, args);
}

@Bean
public IMap<String, String> keyValueMap(@Autowired HazelcastInstance hazelcast) {
return hazelcast.getMap("keyValueMap");
}
}
2 changes: 2 additions & 0 deletions spring/spring-boot-embedded/src/main/resources/hazelcast.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hazelcast:
cluster-name: hazelcast-cluster
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package guides.hazelcast.springboot;

import com.hazelcast.core.Hazelcast;
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.test.web.reactive.server.WebTestClient;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.HttpHeaders.ACCEPT;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CommandControllerTest {

@Autowired
private WebTestClient webTestClient;

@Autowired
private HazelcastInstance hazelcastInstance;

@Autowired
private Map<String, String> keyValueMap;

@Test
public void testPutRequest() {
//when
WebTestClient.ResponseSpec responseSpec = makePutRequest("key1", "value1");

//then
responseSpec.expectStatus()
.is2xxSuccessful()
.expectHeader()
.contentType(APPLICATION_JSON)
.expectBody()
.jsonPath("$.value").isEqualTo("value1");

assertThat(keyValueMap).containsEntry("key1", "value1");
}

@Test
public void testGetRequest() {
//given
makePutRequest("key1", "value1");

//when
WebTestClient.ResponseSpec responseSpec = webTestClient
.get()
.uri("/get?key={key}", "key1")
.header(ACCEPT, APPLICATION_JSON_VALUE)
.exchange();

//then
responseSpec.expectStatus()
.is2xxSuccessful()
.expectHeader()
.contentType(APPLICATION_JSON)
.expectBody()
.jsonPath("$.value").isEqualTo("value1");
}

private WebTestClient.ResponseSpec makePutRequest(Object... parameters) {
return webTestClient
.post()
.uri("/put?key={key}&value={value}", parameters)
.header(ACCEPT, APPLICATION_JSON_VALUE)
.exchange();
}

@Test
public void testHazelcastCluster() {
//given
Hazelcast.newHazelcastInstance();

//then
assertThat(hazelcastInstance.getCluster().getMembers())
.hasSize(2);
}
}
6 changes: 6 additions & 0 deletions spring/spring-boot-embedded/src/test/resources/hazelcast.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
hazelcast:
cluster-name: hazelcast-cluster
network:
join:
multicast:
enabled: true
Loading