Search in sources :

Example 51 with MqttException

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);
}
Also used : MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) CompletableFuture(java.util.concurrent.CompletableFuture) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) RejectedError(software.amazon.awssdk.iot.iotjobs.model.RejectedError)

Example 52 with MqttException

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;
        }
    }
}
Also used : GetNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowRequest) Deployment(com.aws.greengrass.deployment.model.Deployment) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) MqttClientConnectionEvents(software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents) UpdateNamedShadowRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowRequest) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) LogManager(com.aws.greengrass.logging.impl.LogManager) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) DEPLOYMENT_STATUS_KEY_NAME(com.aws.greengrass.deployment.DeploymentStatusKeeper.DEPLOYMENT_STATUS_KEY_NAME) UUID(java.util.UUID) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) WrapperMqttClientConnection(com.aws.greengrass.mqttclient.WrapperMqttClientConnection) ARN_FOR_STATUS_KEY(com.aws.greengrass.status.DeploymentInformation.ARN_FOR_STATUS_KEY) FAILURE_CAUSE_KEY(com.aws.greengrass.status.StatusDetails.FAILURE_CAUSE_KEY) GetNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowSubscriptionRequest) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) DEPLOYMENT_DETAILED_STATUS_KEY(com.aws.greengrass.deployment.DeploymentService.DEPLOYMENT_DETAILED_STATUS_KEY) DETAILED_STATUS_KEY(com.aws.greengrass.status.StatusDetails.DETAILED_STATUS_KEY) SerializerFactory(com.aws.greengrass.util.SerializerFactory) Setter(lombok.Setter) Getter(lombok.Getter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) STATUS_DETAILS_KEY(com.aws.greengrass.status.DeploymentInformation.STATUS_DETAILS_KEY) HashMap(java.util.HashMap) DEPLOYMENT_STATUS_DETAILS_KEY_NAME(com.aws.greengrass.deployment.DeploymentStatusKeeper.DEPLOYMENT_STATUS_DETAILS_KEY_NAME) Configuration(com.amazon.aws.iot.greengrass.configuration.common.Configuration) Coerce(com.aws.greengrass.util.Coerce) AtomicReference(java.util.concurrent.atomic.AtomicReference) STATUS_KEY(com.aws.greengrass.status.DeploymentInformation.STATUS_KEY) ShadowState(software.amazon.awssdk.iot.iotshadow.model.ShadowState) Inject(javax.inject.Inject) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) IotShadowClient(software.amazon.awssdk.iot.iotshadow.IotShadowClient) JobStatus(software.amazon.awssdk.iot.iotjobs.model.JobStatus) InjectionActions(com.aws.greengrass.dependency.InjectionActions) ExecutorService(java.util.concurrent.ExecutorService) ServiceLoadException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException) DeploymentType(com.aws.greengrass.deployment.model.Deployment.DeploymentType) DEPLOYMENT_FAILURE_CAUSE_KEY(com.aws.greengrass.deployment.DeploymentService.DEPLOYMENT_FAILURE_CAUSE_KEY) DEPLOYMENT_ID_KEY_NAME(com.aws.greengrass.deployment.DeploymentStatusKeeper.DEPLOYMENT_ID_KEY_NAME) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) WhatHappened(com.aws.greengrass.config.WhatHappened) DeploymentTaskMetadata(com.aws.greengrass.deployment.model.DeploymentTaskMetadata) UpdateNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowSubscriptionRequest) DeviceConfigurationException(com.aws.greengrass.deployment.exceptions.DeviceConfigurationException) MqttClient(com.aws.greengrass.mqttclient.MqttClient) Logger(com.aws.greengrass.logging.api.Logger) NoArgsConstructor(lombok.NoArgsConstructor) GetNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowSubscriptionRequest) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) ExecutionException(java.util.concurrent.ExecutionException) UpdateNamedShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowSubscriptionRequest) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

MqttException (software.amazon.awssdk.crt.mqtt.MqttException)52 CompletableFuture (java.util.concurrent.CompletableFuture)48 MqttMessage (software.amazon.awssdk.crt.mqtt.MqttMessage)46 Consumer (java.util.function.Consumer)29 MqttClientConnection (software.amazon.awssdk.crt.mqtt.MqttClientConnection)29 QualityOfService (software.amazon.awssdk.crt.mqtt.QualityOfService)29 Gson (com.google.gson.Gson)28 GsonBuilder (com.google.gson.GsonBuilder)28 UnsupportedEncodingException (java.io.UnsupportedEncodingException)28 ByteBuffer (java.nio.ByteBuffer)28 StandardCharsets (java.nio.charset.StandardCharsets)28 EnumSerializer (software.amazon.awssdk.iot.EnumSerializer)28 Timestamp (software.amazon.awssdk.iot.Timestamp)28 HashMap (java.util.HashMap)27 GetNamedShadowRequest (software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowRequest)17 GetNamedShadowSubscriptionRequest (software.amazon.awssdk.iot.iotshadow.model.GetNamedShadowSubscriptionRequest)17 ShadowState (software.amazon.awssdk.iot.iotshadow.model.ShadowState)17 UpdateNamedShadowRequest (software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowRequest)17 UpdateNamedShadowSubscriptionRequest (software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowSubscriptionRequest)17 ShadowStateFactory (software.amazon.awssdk.iot.ShadowStateFactory)16