Skip to content

Commit a4866b4

Browse files
munkhuushmglgguuss
authored andcommitted
merging java end-to-end example (GoogleCloudPlatform#1308)
Adds end-to-end example in Java for device and server
1 parent e114569 commit a4866b4

File tree

8 files changed

+1305
-0
lines changed

8 files changed

+1305
-0
lines changed
+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Google Cloud IoT Core Java Samples
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=iot/api-client/manager/README.md">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
This directory contains samples for Google Cloud IoT Core. [Google Cloud IoT Core](https://cloud.google.com/iot/docs/ "Google Cloud IoT Core") allows developers to easily integrate Publish and Subscribe functionality with devices and programmatically manage device authorization.
7+
8+
## Prerequisites
9+
10+
### Java
11+
12+
We recommend using [Java 8 JDK](https://java.com/en/download) for this example.
13+
14+
### Download Maven
15+
16+
These samples use the [Apache Maven][maven] build system. Before getting
17+
started, be sure to [download][maven-download] and [install][maven-install] it.
18+
When you use Maven as described here, it will automatically download the needed
19+
client libraries.
20+
21+
[maven]: https://maven.apache.org
22+
[maven-download]: https://maven.apache.org/download.cgi
23+
[maven-install]: https://maven.apache.org/install.html
24+
25+
### Create a Project in the Google Cloud Platform Console
26+
27+
If you haven't already created a project, create one now. Projects enable you to
28+
manage all Google Cloud Platform resources for your app, including deployment,
29+
access control, billing, and services.
30+
31+
1. Open the [Cloud Platform Console][cloud-console].
32+
2. In the drop-down menu at the top, select **Create a project**.
33+
3. Give your project a name.
34+
4. Make a note of the project ID, which might be different from the project
35+
name. The project ID is used in commands and in configurations.
36+
37+
[cloud-console]: https://console.cloud.google.com/
38+
39+
## Setup
40+
41+
Note that before you can run the sample, you must configure a Google Cloud
42+
PubSub topic for Cloud IoT as described in [the parent README](../README.md).
43+
44+
Before running the samples, you can set the `GOOGLE_CLOUD_PROJECT` and
45+
`GOOGLE_APPLICATION_CREDENTIALS` environment variables to avoid passing them to
46+
the sample every time you run it.
47+
48+
For example, on most *nix systems you can do this as:
49+
50+
export GOOGLE_CLOUD_PROJECT=your-project-id
51+
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/[FILE_NAME].json"
52+
53+
54+
### Authentication
55+
56+
This sample requires you to have authentication setup. Refer to the [Authentication Getting Started Guide](https://cloud.google.com/docs/authentication/getting-started "Google Cloud IoT Core") for instructions on setting up credentials for applications.
57+
58+
Run the following command to install the libraries and build the sample with Maven:
59+
60+
mvn clean compile assembly:single
61+
62+
63+
Before you begin, you will need to create the Google Cloud PubSub message queue, create a subscription to it, create a device registry, and add a device to the registry.
64+
65+
1. Create the PubSub topic as:
66+
67+
gcloud pubsub topics create java-e2e
68+
69+
2. Create the subscription:
70+
71+
gcloud pubsub subscriptions create java-e2e-sub --topic=java-e2e
72+
73+
3. Create the device registry:
74+
75+
gcloud iot registries create java-ed2e-registry --event-notification-config=topic=java-e2e --region=us-central1
76+
77+
4. Run the `generate_keys.sh` shell script:
78+
79+
../generate_keys.sh
80+
81+
5. Add a device to the registry using the keys you generated:
82+
83+
gcloud iot devices create device-id --registry=java-ed2e-sub --region=us-central1 --public-key=path=./rsa_cert.pem,type=RS256
84+
85+
86+
## Samples
87+
88+
### Server
89+
90+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=iot/api-client/manager/README.md">
91+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
92+
93+
To run this sample:
94+
95+
mvn exec:java \
96+
-Dexec.mainClass="com.example.cloud.iot.endtoend.CloudiotPubsubExampleServer" \
97+
-Dexec.args="-project_id=<your-iot-project> \
98+
-pubsub_subscription=<your-pubsub-subscription>"
99+
100+
### Device
101+
102+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=iot/api-client/manager/README.md">
103+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
104+
105+
To run this sample:
106+
107+
mvn exec:java \
108+
-Dexec.mainClass="com.example.cloud.iot.endtoend.CloudiotPubsubExampleMqttDevice" \
109+
-Dexec.args="-project_id=<your-iot-project> \
110+
-registry_id=<your-registry-id> \
111+
-device_id=<device-id> \
112+
-private_key_file=<path-to-keyfile> \
113+
-algorithm=<RS256|ES256>"
114+
115+
For example, if your project ID is `blue-jet-123`, your device registry id is
116+
`my-registry`, your device id is `my-device` and you have generated your
117+
credentials using the [`generate_keys.sh`](../generate_keys.sh) script
118+
provided in the parent folder, you can run the sample as:
119+
120+
mvn exec:java \
121+
-Dexec.mainClass="com.example.cloud.iot.endtoend.CloudiotPubsubExampleMqttDevice" \
122+
-Dexec.args="-project_id=blue-jet-123 \
123+
-registry_id=my-registry \
124+
-device_id=my-device \
125+
-private_key_file=../rsa_private_pkcs8 \
126+
-algorithm=RS256"
+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<!--
2+
Copyright 2019 Google Inc.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
-->
13+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
14+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
15+
<modelVersion>4.0.0</modelVersion>
16+
<groupId>com.example.cloud</groupId>
17+
<artifactId>cloudiot-manager-demo</artifactId>
18+
<packaging>jar</packaging>
19+
<version>1.0</version>
20+
<name>cloudiot-manager-demo</name>
21+
<url>http://maven.apache.org</url>
22+
23+
<!--
24+
The parent pom defines common style checks and testing strategies for our samples.
25+
Removing or replacing it should not affect the execution of the samples in anyway.
26+
-->
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.0.9</version>
31+
</parent>
32+
<properties>
33+
<maven.compiler.target>1.8</maven.compiler.target>
34+
<maven.compiler.source>1.8</maven.compiler.source>
35+
</properties>
36+
37+
<dependencies>
38+
<dependency>
39+
<groupId>org.eclipse.paho</groupId>
40+
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
41+
<version>1.2.0</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.json</groupId>
45+
<artifactId>json</artifactId>
46+
<version>20090211</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.jsonwebtoken</groupId>
50+
<artifactId>jjwt</artifactId>
51+
<version>0.7.0</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>joda-time</groupId>
55+
<artifactId>joda-time</artifactId>
56+
<version>2.1</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.google.apis</groupId>
60+
<artifactId>google-api-services-cloudiot</artifactId>
61+
<version>v1-rev20181120-1.27.0</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>com.google.cloud</groupId>
65+
<artifactId>google-cloud-pubsub</artifactId>
66+
<version>1.53.0</version>
67+
</dependency>
68+
<dependency>
69+
<groupId>com.google.oauth-client</groupId>
70+
<artifactId>google-oauth-client</artifactId>
71+
<version>1.23.0</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>com.google.guava</groupId>
75+
<artifactId>guava</artifactId>
76+
<version>23.0</version>
77+
</dependency>
78+
<dependency>
79+
<groupId>com.google.api-client</groupId>
80+
<artifactId>google-api-client</artifactId>
81+
<version>1.23.0</version>
82+
</dependency>
83+
<dependency>
84+
<groupId>commons-cli</groupId>
85+
<artifactId>commons-cli</artifactId>
86+
<version>1.3</version>
87+
</dependency>
88+
89+
<!-- Test dependencies -->
90+
<dependency>
91+
<groupId>junit</groupId>
92+
<artifactId>junit</artifactId>
93+
<version>4.12</version>
94+
<scope>test</scope>
95+
</dependency>
96+
<dependency>
97+
<groupId>com.google.truth</groupId>
98+
<artifactId>truth</artifactId>
99+
<version>0.34</version>
100+
<scope>test</scope>
101+
</dependency>
102+
</dependencies>
103+
104+
<build>
105+
<plugins>
106+
<plugin>
107+
<artifactId>maven-assembly-plugin</artifactId>
108+
<configuration>
109+
<archive>
110+
<manifest>
111+
<mainClass>com.example.cloudiot.Manage</mainClass>
112+
</manifest>
113+
</archive>
114+
<descriptorRefs>
115+
<descriptorRef>jar-with-dependencies</descriptorRef>
116+
</descriptorRefs>
117+
</configuration>
118+
</plugin>
119+
</plugins>
120+
</build>
121+
</project>

0 commit comments

Comments
 (0)