-
Notifications
You must be signed in to change notification settings - Fork 603
Move Spring Boot with embedded Hazelcast back [DEX-296] #692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JamesHazelcast
merged 4 commits into
hazelcast:master
from
frant-hartm:spring/re-add/spring-boot-embedded
Dec 18, 2024
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
31 changes: 31 additions & 0 deletions
31
spring/spring-boot-embedded/src/main/java/guides/hazelcast/springboot/CommandController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
spring/spring-boot-embedded/src/main/java/guides/hazelcast/springboot/CommandResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package guides.hazelcast.springboot; | ||
|
|
||
| public record CommandResponse(String value) { | ||
| } |
21 changes: 21 additions & 0 deletions
21
.../spring-boot-embedded/src/main/java/guides/hazelcast/springboot/HazelcastApplication.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
2
spring/spring-boot-embedded/src/main/resources/hazelcast.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| hazelcast: | ||
| cluster-name: hazelcast-cluster | ||
83 changes: 83 additions & 0 deletions
83
...spring-boot-embedded/src/test/java/guides/hazelcast/springboot/CommandControllerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = makePostRequest("/put?key={key}&value={value}", "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 | ||
| makePostRequest("/put?key={key}&value={value}", "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 makePostRequest(String uri, Object... parameters) { | ||
| return webTestClient | ||
| .post() | ||
| .uri(uri, 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
6
spring/spring-boot-embedded/src/test/resources/hazelcast.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| hazelcast: | ||
| cluster-name: hazelcast-cluster | ||
| network: | ||
| join: | ||
| multicast: | ||
| enabled: true |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.