use of software.amazon.awssdk.crt.mqtt.MqttException in project aws-greengrass-nucleus by aws-greengrass.
the class IotJobsClientWrapper method SubscribeToDescribeJobExecutionRejected.
@Override
public CompletableFuture<Integer> SubscribeToDescribeJobExecutionRejected(DescribeJobExecutionSubscriptionRequest request, QualityOfService qos, Consumer<RejectedError> handler, Consumer<Exception> exceptionHandler) {
if (request.jobId == null || request.thingName == null) {
CompletableFuture result = new CompletableFuture();
result.completeExceptionally(new MqttException("DescribeJobExecutionSubscriptionRequest must have a non-null jobId and a non-null thingName"));
return result;
}
String topic = String.format(JOB_DESCRIBE_REJECTED_TOPIC, request.thingName, request.jobId);
Consumer<MqttMessage> messageHandler = describeJobSubscriptionCbs.computeIfAbsent(new Pair<>(handler, exceptionHandler), (k) -> (message) -> {
try {
String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
RejectedError response = this.gson.fromJson(payload, RejectedError.class);
handler.accept(response);
} catch (Exception e) {
if (exceptionHandler != null) {
exceptionHandler.accept(e);
}
}
});
return this.connection.subscribe(topic, qos, messageHandler);
}
use of software.amazon.awssdk.crt.mqtt.MqttException in project aws-greengrass-nucleus by aws-greengrass.
the class ShadowDeploymentListener method subscribeToShadowTopics.
/*
Subscribe to "$aws/things/{thingName}/shadow/update/accepted" topic to get notified when shadow is updated
Subscribe to "$aws/things/{thingName}/shadow/update/rejected" topic to get notified when an update is rejected
Subscribe to "$aws/things/{thingName}/shadow/get/accepted" topic to retrieve shadow by publishing to get topic
*/
private void subscribeToShadowTopics() {
logger.atDebug().log(SUBSCRIBING_TO_SHADOW_TOPICS_MESSAGE);
while (true) {
try {
UpdateNamedShadowSubscriptionRequest updateNamedShadowSubscriptionRequest = new UpdateNamedShadowSubscriptionRequest();
updateNamedShadowSubscriptionRequest.shadowName = DEPLOYMENT_SHADOW_NAME;
updateNamedShadowSubscriptionRequest.thingName = thingName;
iotShadowClient.SubscribeToUpdateNamedShadowAccepted(updateNamedShadowSubscriptionRequest, QualityOfService.AT_LEAST_ONCE, updateShadowResponse -> shadowUpdated(updateShadowResponse.state.desired, updateShadowResponse.state.reported, updateShadowResponse.version), (e) -> logger.atError().log("Error processing updateShadowResponse", e)).get(TIMEOUT_FOR_SUBSCRIBING_TO_TOPICS_SECONDS, TimeUnit.SECONDS);
logger.debug("Subscribed to update named shadow accepted topic");
iotShadowClient.SubscribeToUpdateNamedShadowRejected(updateNamedShadowSubscriptionRequest, QualityOfService.AT_LEAST_ONCE, updateShadowRejected -> handleNamedShadowRejectedEvent(), (e) -> logger.atError().log("Error processing named shadow update rejected response", e)).get(TIMEOUT_FOR_SUBSCRIBING_TO_TOPICS_SECONDS, TimeUnit.SECONDS);
logger.debug("Subscribed to update named shadow rejected topic");
GetNamedShadowSubscriptionRequest getNamedShadowSubscriptionRequest = new GetNamedShadowSubscriptionRequest();
getNamedShadowSubscriptionRequest.shadowName = DEPLOYMENT_SHADOW_NAME;
getNamedShadowSubscriptionRequest.thingName = thingName;
iotShadowClient.SubscribeToGetNamedShadowAccepted(getNamedShadowSubscriptionRequest, QualityOfService.AT_LEAST_ONCE, getShadowResponse -> shadowUpdated(getShadowResponse.state.desired, getShadowResponse.state.reported, getShadowResponse.version), (e) -> logger.atError().log("Error processing getShadowResponse", e)).get(TIMEOUT_FOR_SUBSCRIBING_TO_TOPICS_SECONDS, TimeUnit.SECONDS);
logger.debug("Subscribed to get named shadow topic");
return;
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof MqttException || cause instanceof TimeoutException) {
logger.atWarn().setCause(cause).log("Caught exception while subscribing to shadow topics, " + "will retry shortly");
} else if (cause instanceof InterruptedException) {
logger.atWarn().log("Interrupted while subscribing to shadow topics");
return;
} else {
logger.atError().setCause(e).log("Caught exception while subscribing to shadow topics, will retry shortly");
}
} catch (TimeoutException e) {
logger.atWarn().setCause(e).log("Subscribe to shadow topics timed out, will retry shortly");
} catch (InterruptedException e) {
// Since this method can run as runnable cannot throw exception so handling exceptions here
logger.atWarn().log("Interrupted while subscribing to shadow topics");
return;
}
try {
// Wait for sometime and then try to subscribe again
Thread.sleep(WAIT_TIME_TO_SUBSCRIBE_AGAIN_IN_MS + JITTER.nextInt(10_000));
} catch (InterruptedException interruptedException) {
logger.atWarn().log("Interrupted while subscribing to device shadow topics");
return;
}
}
}
Aggregations