14
14
15
15
package com .google .cloud .iot .examples ;
16
16
17
+ // [START cloudiotcore_mqtt_imports]
17
18
import io .jsonwebtoken .JwtBuilder ;
18
19
import io .jsonwebtoken .Jwts ;
19
20
import io .jsonwebtoken .SignatureAlgorithm ;
26
27
import org .eclipse .paho .client .mqttv3 .MqttMessage ;
27
28
import org .eclipse .paho .client .mqttv3 .persist .MemoryPersistence ;
28
29
import org .joda .time .DateTime ;
30
+ // [END cloudiotcore_mqtt_imports]
29
31
30
32
/**
31
33
* Java sample of connecting to Google Cloud IoT Core vice via MQTT, using JWT.
52
54
* </pre>
53
55
*/
54
56
public class MqttExample {
55
- /** Create a Cloud IoT Core JWT for the given project id, signed with the given private key. */
57
+ // [START cloudiotcore_mqtt_createjwt]
58
+ /** Create a Cloud IoT Core JWT for the given project id, signed with the given RSA key. */
56
59
private static String createJwtRsa (String projectId , String privateKeyFile ) throws Exception {
57
60
DateTime now = new DateTime ();
58
61
// Create a JWT to authenticate this device. The device will be disconnected after the token
@@ -71,6 +74,7 @@ private static String createJwtRsa(String projectId, String privateKeyFile) thro
71
74
return jwtBuilder .signWith (SignatureAlgorithm .RS256 , kf .generatePrivate (spec )).compact ();
72
75
}
73
76
77
+ /** Create a Cloud IoT Core JWT for the given project id, signed with the given ES key. */
74
78
private static String createJwtEs (String projectId , String privateKeyFile ) throws Exception {
75
79
DateTime now = new DateTime ();
76
80
// Create a JWT to authenticate this device. The device will be disconnected after the token
@@ -88,8 +92,11 @@ private static String createJwtEs(String projectId, String privateKeyFile) throw
88
92
89
93
return jwtBuilder .signWith (SignatureAlgorithm .ES256 , kf .generatePrivate (spec )).compact ();
90
94
}
95
+ // [END cloudiotcore_mqtt_createjwt]
91
96
97
+ /** Parse arguments, configure MQTT, and publish messages. */
92
98
public static void main (String [] args ) throws Exception {
99
+ // [START cloudiotcore_mqtt_configuremqtt]
93
100
MqttExampleOptions options = MqttExampleOptions .fromFlags (args );
94
101
if (options == null ) {
95
102
// Could not parse.
@@ -131,7 +138,9 @@ public static void main(String[] args) throws Exception {
131
138
throw new IllegalArgumentException (
132
139
"Invalid algorithm " + options .algorithm + ". Should be one of 'RS256' or 'ES256'." );
133
140
}
141
+ // [END cloudiotcore_mqtt_configuremqtt]
134
142
143
+ // [START cloudiotcore_mqtt_publish]
135
144
// Create a client, and connect to the Google MQTT bridge.
136
145
MqttClient client = new MqttClient (mqttServerAddress , mqttClientId , new MemoryPersistence ());
137
146
try {
@@ -171,5 +180,6 @@ public static void main(String[] args) throws Exception {
171
180
client .disconnect ();
172
181
}
173
182
System .out .println ("Finished loop successfully. Goodbye!" );
183
+ // [END cloudiotcore_mqtt_publish]
174
184
}
175
185
}
0 commit comments