Skip to content

Commit 47739c9

Browse files
committed
Add Spring Boot sample
1 parent a79a4e2 commit 47739c9

File tree

16 files changed

+907
-62
lines changed

16 files changed

+907
-62
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
19+
20+
import java.net.*;
21+
import java.io.*;
22+
import java.nio.channels.*;
23+
import java.util.Properties;
24+
25+
public class MavenWrapperDownloader {
26+
27+
/**
28+
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
29+
*/
30+
private static final String DEFAULT_DOWNLOAD_URL =
31+
"https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar";
32+
33+
/**
34+
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
35+
* use instead of the default one.
36+
*/
37+
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
38+
".mvn/wrapper/maven-wrapper.properties";
39+
40+
/**
41+
* Path where the maven-wrapper.jar will be saved to.
42+
*/
43+
private static final String MAVEN_WRAPPER_JAR_PATH =
44+
".mvn/wrapper/maven-wrapper.jar";
45+
46+
/**
47+
* Name of the property which should be used to override the default download url for the wrapper.
48+
*/
49+
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
50+
51+
public static void main(String args[]) {
52+
System.out.println("- Downloader started");
53+
File baseDirectory = new File(args[0]);
54+
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
55+
56+
// If the maven-wrapper.properties exists, read it and check if it contains a custom
57+
// wrapperUrl parameter.
58+
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
59+
String url = DEFAULT_DOWNLOAD_URL;
60+
if(mavenWrapperPropertyFile.exists()) {
61+
FileInputStream mavenWrapperPropertyFileInputStream = null;
62+
try {
63+
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
64+
Properties mavenWrapperProperties = new Properties();
65+
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
66+
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
67+
} catch (IOException e) {
68+
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
69+
} finally {
70+
try {
71+
if(mavenWrapperPropertyFileInputStream != null) {
72+
mavenWrapperPropertyFileInputStream.close();
73+
}
74+
} catch (IOException e) {
75+
// Ignore ...
76+
}
77+
}
78+
}
79+
System.out.println("- Downloading from: : " + url);
80+
81+
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
82+
if(!outputFile.getParentFile().exists()) {
83+
if(!outputFile.getParentFile().mkdirs()) {
84+
System.out.println(
85+
"- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'");
86+
}
87+
}
88+
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
89+
try {
90+
downloadFileFromURL(url, outputFile);
91+
System.out.println("Done");
92+
System.exit(0);
93+
} catch (Throwable e) {
94+
System.out.println("- Error downloading");
95+
e.printStackTrace();
96+
System.exit(1);
97+
}
98+
}
99+
100+
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
101+
URL website = new URL(urlString);
102+
ReadableByteChannel rbc;
103+
rbc = Channels.newChannel(website.openStream());
104+
FileOutputStream fos = new FileOutputStream(destination);
105+
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
106+
fos.close();
107+
rbc.close();
108+
}
109+
110+
}

.mvn/wrapper/maven-wrapper.jar

47.2 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip

README.adoc

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1-
// Replace <filename> with the name of your repository, and replace <tutorial name> with the title of the tutorial.
2-
// For guidance on using this template, see .github/CONTRIBUTING.adoc
3-
This repository hosts the documentation and code samples for the link:https://docs.hazelcast.com/tutorials/<filename>[<tutorial name> tutorial].
1+
= Spring Boot Application for Hazelcast Viridian
2+
:experimental: true
3+
4+
This is an example of how to use Spring Boot with Hazelcast Viridian.
5+
6+
TIP: For step-by-step instructions of how to run this app, see the link:https://docs.hazelcast.com/tutorials/spring-boot-client[tutorial].
7+
8+
== Quickstart
9+
10+
. Add the required properties to `spring-sample/src/main/resources/application.properties`:
11+
12+
- `clusterName`
13+
- `discoveryToken`
14+
- `keyStorePassword`
15+
- `trustStorePassword` (same as `keyStorePassword`)
16+
17+
. Add the keystore and truststore for your cluster to `spring-sample/src/main/resources`.
18+
19+
. Execute the following to run the sample:
20+
21+
```
22+
./mvnw spring-boot:run
23+
```
24+
25+
== Internal Hazelcast Developers
26+
27+
If you want to try this application in the UAT or DEV environment, edit the `src/main/java/sample/com/hazelcast/demo/viridian/HzViridianDemoApplication.java` file to include the environment URL:
28+
29+
```java
30+
// For DEV, use https://test.dev.hazelcast.cloud
31+
config.setProperty("hazelcast.client.cloud.url", "https://uat.hazelcast.cloud");
32+
```

antora-playbook-local.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ content:
1313
start_paths: [tutorials, home]
1414
- url: https://github.com/hazelcast-guides/adoc-templates.git
1515
branches: antora-doc
16+
- url: .
17+
branches: master
18+
start_paths: docs
1619
ui:
1720
bundle:
1821
url: https://github.com/hazelcast/hazelcast-docs-ui/releases/latest/download/ui-bundle.zip

docs/antora.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
name: tutorials
2-
version: ~
3-
2+
version: ~

docs/modules/ROOT/pages/rename-me.adoc

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
= Create a Spring Boot Application for Hazelcast {hazelcast-cloud}
2+
:description: In this tutorial, you'll learn how to create a basic Spring Boot application that connects to a cluster.
3+
:page-product: cloud
4+
:page-layout: tutorial
5+
:page-categories: Spring Boot, Get Started
6+
:page-lang: java
7+
:page-est-time: 10 mins
8+
:page-serverless: true
9+
:url-spring-boot: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#getting-started
10+
:url-spring-hazelcast: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#io.hazelcast
11+
:github-directory: https://github.com/hazelcast-guides/spring-boot-sample
12+
13+
{description}
14+
15+
== Context
16+
17+
The Spring Framework is a Java framework for creating standalone applications that run on the Java Virtual Machine (JVM).
18+
19+
Spring Boot is a tool that makes it easier and faster to develop applications with the Spring Framework by providing many features, including autoconfiguration.
20+
21+
If you're unfamiliar with Spring or Spring Boot, see the link:{url-spring-boot}[Spring Boot documentation].
22+
23+
== Before you Begin
24+
25+
You need the following:
26+
27+
- A xref:cloud:ROOT:create-serverless-cluster.adoc[{hazelcast-cloud} Serverless cluster].
28+
- link:https://git-scm.com/book/en/v2/Getting-Started-Installing-Git[Git]
29+
- link:https://maven.apache.org/install.html[Maven]
30+
- JDK 8
31+
- `JAVA_HOME` environment variable set to the path of the JDK
32+
33+
34+
[[step-one]]
35+
== Step 1. Clone the Sample Project
36+
37+
All the code for this project is available on GitHub. In this step, you'll clone the project and learn how it works.
38+
39+
. Clone the GitHub repository.
40+
+
41+
[tabs]
42+
====
43+
HTTPS::
44+
+
45+
--
46+
```bash
47+
git clone https://github.com/hazelcast-guides/spring-boot-sample.git
48+
49+
cd spring-boot-sample
50+
```
51+
--
52+
SSH::
53+
+
54+
--
55+
```bash
56+
git clone [email protected]:hazelcast-guides/spring-boot-sample.git
57+
58+
cd spring-boot-sample
59+
```
60+
--
61+
====
62+
63+
. Open the `src/main/resources/application.properties` file.
64+
65+
. Add connection credentials for your cluster to the `application.properties` file. You can find these credentials in the Hazelcast {hazelcast-cloud} console under *Connect Your Application* > *Advanced Setup*.
66+
+
67+
- `clusterName`
68+
- `discoveryToken`
69+
- `keyStorePassword`
70+
- `trustStorePassword` (same as `keyStorePassword`)
71+
72+
. Download the keystore and truststore files from the Hazelcast {hazelcast-cloud} console and add them to the `src/main/resources/` directory. You can find these files in the Hazelcast {hazelcast-cloud} console under *Connect Your Application* > *Advanced Setup*.
73+
74+
.`pom.xml`
75+
[%collapsible]
76+
====
77+
If the Hazelcast client is on the classpath and a suitable configuration is found, Spring Boot automatically configures a `HazelcastInstance` that you can inject into your application.
78+
79+
[source,xml,indent=0]
80+
----
81+
include::spring-boot-sample:example$pom.xml[tag=client]
82+
----
83+
84+
link:{github-directory}/pom.xml[View source]
85+
====
86+
87+
.`HzViridianDemoApplication.java`
88+
[%collapsible]
89+
====
90+
Spring Boot first attempts to create a Hazelcast client by checking the presence of a `com.hazelcast.client.config.ClientConfig` bean. This bean is configured using the information in the `application.properties` file.
91+
92+
[source,java]
93+
----
94+
include::spring-boot-sample:example$src/main/java/sample/com/hazelcast/demo/viridian/HzViridianDemoApplication.java[tag=class]
95+
----
96+
97+
link:{github-directory}/src/main/java/sample/com/hazelcast/demo/viridian/HzViridianDemoApplication.java[View source]
98+
====
99+
100+
.`MapService.java`
101+
[%collapsible]
102+
====
103+
When the `com.hazelcast.client.config.ClientConfig` bean is present, a `HazelcastInstance` is injected into the application classes. This instance is a Hazelcast client with an established connection to the {hazelcast-cloud} Serverless cluster.
104+
105+
[source,java]
106+
----
107+
include::spring-boot-sample:example$src/main/java/sample/com/hazelcast/demo/viridian/MapService.java[tag=class]
108+
----
109+
110+
link:{github-directory}/src/main/java/sample/com/hazelcast/demo/viridian/MapService.java[View source]
111+
====
112+
113+
== Step 2. Connect the Client
114+
115+
In the `spring-boot-sample/` directory, execute the following:
116+
117+
[tabs]
118+
====
119+
Linux and Mac::
120+
+
121+
--
122+
```bash
123+
./mvnw spring-boot:run
124+
```
125+
--
126+
Windows::
127+
+
128+
--
129+
```bash
130+
mvnw.cmd spring-boot:run
131+
```
132+
--
133+
====
134+
135+
The client connects to the cluster and adds 10 random entries to a map called `MyMap`.
136+
137+
```
138+
...
139+
Authenticated with server
140+
...
141+
Map prepopulated [mapName=MyMap,size=10]
142+
...
143+
BUILD SUCCESS
144+
```
145+
146+
== Summary
147+
148+
In this tutorial, you learned how to set up a Spring Boot application to connect to a {hazelcast-cloud} Serverless cluster.
149+
150+
== Next Steps
151+
152+
Use this boilerplate application as a foundation to start building your own application.
153+
154+
See the xref:cloud:ROOT:overview.adoc[Hazelcast {hazelcast-cloud} documentation] to continue learning or try another tutorial.
155+
156+
See the link:{url-spring-hazelcast}[Spring Boot] documentation for more about using Hazelcast with Spring.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../pom.xml
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../src

0 commit comments

Comments
 (0)