GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Adds credential refresh to MQTT example by gguuss · Pull Request #917 · GoogleCloudPlatform/java-docs-samples

Adds credential refresh to MQTT example #917

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.

Sign up for GitHub

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
kurtisvg merged 2 commits into master from iot-mqtt-refresh
Nov 17, 2017

Uh oh!

There was an error while loading. Please reload this page.

Merged

Adds credential refresh to MQTT example #917

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
50f52bf
Adds credential refresh to MQTT example
gguuss Nov 14, 2017
e52f4ac
Merge branch 'master' into iot-mqtt-refresh
gguuss Nov 16, 2017
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

20 changes: 19 additions & 1 deletion 20 iot/api-client/mqtt_example/src/main/java/com/google/cloud/iot/examples/MqttExample.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static void main(String[] args) throws Exception {
// to authorize the device.
connectOptions.setUserName("unused");

System.out.println(options.algorithm);
DateTime iat = new DateTime();
if (options.algorithm.equals("RS256")) {
connectOptions.setPassword(
createJwtRsa(options.projectId, options.privateKeyFile).toCharArray());
Expand Down Expand Up @@ -161,6 +161,24 @@ public static void main(String[] args) throws Exception {
"Publishing %s message %d/%d: '%s'\n",
options.messageType, i, options.numMessages, payload);

// Refresh the connection credentials before the JWT expires.
long secsSinceRefresh = ((new DateTime()).getMillis() - iat.getMillis()) / 1000;
if (secsSinceRefresh > (options.tokenExpMins * 60)) {
System.out.format("\tRefreshing token after: %d seconds\n", secsSinceRefresh);
iat = new DateTime();
if (options.algorithm.equals("RS256")) {
connectOptions.setPassword(
createJwtRsa(options.projectId, options.privateKeyFile).toCharArray());
} else if (options.algorithm.equals("ES256")) {
connectOptions.setPassword(
createJwtEs(options.projectId, options.privateKeyFile).toCharArray());
} else {
throw new IllegalArgumentException(
"Invalid algorithm " + options.algorithm
+ ". Should be one of 'RS256' or 'ES256'.");
}
}

// Publish "payload" to the MQTT topic. qos=1 means at least once delivery. Cloud IoT Core
// also supports qos=0 for at most once delivery.
MqttMessage message = new MqttMessage(payload.getBytes());
Expand Down
12 changes: 12 additions & 0 deletions 12 ...i-client/mqtt_example/src/main/java/com/google/cloud/iot/examples/MqttExampleOptions.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class MqttExampleOptions {
String algorithm;
String cloudRegion = "us-central1";
int numMessages = 100;
int tokenExpMins = 20;
String mqttBridgeHostname = "mqtt.googleapis.com";
short mqttBridgePort = 8883;
String messageType = "event";
Expand Down Expand Up @@ -101,6 +102,13 @@ public static MqttExampleOptions fromFlags(String[] args) {
.hasArg()
.desc("MQTT bridge hostname.")
.build());
options.addOption(
Option.builder()
.type(Number.class)
.longOpt("token_exp_minutes")
.hasArg()
.desc("Minutes to JWT token refresh (token expiration time).")
.build());
options.addOption(
Option.builder()
.type(Number.class)
Expand Down Expand Up @@ -133,6 +141,10 @@ public static MqttExampleOptions fromFlags(String[] args) {
if (commandLine.hasOption("num_messages")) {
res.numMessages = ((Number) commandLine.getParsedOptionValue("num_messages")).intValue();
}
if (commandLine.hasOption("token_exp_minutes")) {
res.tokenExpMins =
((Number) commandLine.getParsedOptionValue("token_exp_minutes")).intValue();
}
if (commandLine.hasOption("mqtt_bridge_hostname")) {
res.mqttBridgeHostname = commandLine.getOptionValue("mqtt_bridge_hostname");
}
Expand Down
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.