Search in sources :

Example 1 with DescribeJobExecutionSubscriptionRequest

use of software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionSubscriptionRequest in project aws-greengrass-nucleus by aws-greengrass.

the class IotJobsHelper method subscribeToGetNextJobDescription.

/**
 * Subscribes to the describe job topic to get the job details for the next available pending job
 * It also publishes a message to get the next pending job.
 * It returns an empty message if nothing is available
 *
 * @param consumerAccept Consumer for when the job is accepted
 * @param consumerReject Consumer for when the job is rejected
 * @throws ExecutionException   if subscribing fails
 * @throws InterruptedException if the thread gets interrupted
 * @throws TimeoutException     if the operation does not complete within the given time
 */
protected void subscribeToGetNextJobDescription(Consumer<DescribeJobExecutionResponse> consumerAccept, Consumer<RejectedError> consumerReject) throws InterruptedException {
    logger.atDebug().log("Subscribing to deployment job execution update.");
    DescribeJobExecutionSubscriptionRequest describeJobExecutionSubscriptionRequest = new DescribeJobExecutionSubscriptionRequest();
    describeJobExecutionSubscriptionRequest.thingName = this.thingName;
    describeJobExecutionSubscriptionRequest.jobId = NEXT_JOB_LITERAL;
    while (true) {
        CompletableFuture<Integer> subscribed = iotJobsClientWrapper.SubscribeToDescribeJobExecutionAccepted(describeJobExecutionSubscriptionRequest, QualityOfService.AT_LEAST_ONCE, consumerAccept);
        try {
            subscribed.get(mqttClient.getMqttOperationTimeoutMillis(), TimeUnit.MILLISECONDS);
            subscribed = iotJobsClientWrapper.SubscribeToDescribeJobExecutionRejected(describeJobExecutionSubscriptionRequest, QualityOfService.AT_LEAST_ONCE, consumerReject);
            subscribed.get(mqttClient.getMqttOperationTimeoutMillis(), TimeUnit.MILLISECONDS);
            logger.atDebug().log("Subscribed to deployment job execution update.");
            break;
        } catch (ExecutionException e) {
            Throwable cause = e.getCause();
            if (cause instanceof MqttException || cause instanceof TimeoutException) {
                logger.atWarn().setCause(cause).log(SUBSCRIPTION_JOB_DESCRIPTION_RETRY_MESSAGE);
            }
            if (cause instanceof InterruptedException) {
                logger.atWarn().log(SUBSCRIPTION_JOB_DESCRIPTION_INTERRUPTED);
                break;
            }
        } catch (TimeoutException e) {
            logger.atWarn().setCause(e).log(SUBSCRIPTION_JOB_DESCRIPTION_RETRY_MESSAGE);
        } catch (InterruptedException e) {
            logger.atWarn().log(SUBSCRIPTION_JOB_DESCRIPTION_INTERRUPTED);
            throw e;
        }
        try {
            // Wait for sometime and then try to subscribe again
            Thread.sleep(waitTimeToSubscribeAgain + RANDOM.nextInt(10_000));
        } catch (InterruptedException interruptedException) {
            logger.atWarn().log(SUBSCRIPTION_JOB_DESCRIPTION_INTERRUPTED);
            break;
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) ExecutionException(java.util.concurrent.ExecutionException) DescribeJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionSubscriptionRequest) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with DescribeJobExecutionSubscriptionRequest

use of software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionSubscriptionRequest in project aws-iot-device-sdk-java-v2 by aws.

the class IotJobsClient method SubscribeToDescribeJobExecutionAccepted.

/**
 * Subscribes to the accepted topic for the DescribeJobExecution operation
 *
 * Once subscribed, `handler` is invoked each time a message matching
 * the `topic` is received. It is possible for such messages to arrive before
 * the SUBACK is received.
 *
 * AWS documentation: https://docs.aws.amazon.com/iot/latest/developerguide/jobs-api.html#mqtt-describejobexecution
 *
 * @param request Subscription request configuration
 * @param qos Maximum requested QoS that server may use when sending messages to the client.
 *            The server may grant a lower QoS in the SUBACK
 * @param handler callback function to invoke with messages received on the subscription topic
 * @param exceptionHandler callback function to invoke if an exception occurred deserializing a message
 *
 * @return a future containing the MQTT packet id used to perform the subscribe operation
 */
public CompletableFuture<Integer> SubscribeToDescribeJobExecutionAccepted(DescribeJobExecutionSubscriptionRequest request, QualityOfService qos, Consumer<DescribeJobExecutionResponse> handler, Consumer<Exception> exceptionHandler) {
    String topic = "$aws/things/{thingName}/jobs/{jobId}/get/accepted";
    if (request.thingName == null) {
        CompletableFuture<Integer> result = new CompletableFuture<Integer>();
        result.completeExceptionally(new MqttException("DescribeJobExecutionSubscriptionRequest must have a non-null thingName"));
        return result;
    }
    topic = topic.replace("{thingName}", request.thingName);
    if (request.jobId == null) {
        CompletableFuture<Integer> result = new CompletableFuture<Integer>();
        result.completeExceptionally(new MqttException("DescribeJobExecutionSubscriptionRequest must have a non-null jobId"));
        return result;
    }
    topic = topic.replace("{jobId}", request.jobId);
    Consumer<MqttMessage> messageHandler = (message) -> {
        try {
            String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
            DescribeJobExecutionResponse response = gson.fromJson(payload, DescribeJobExecutionResponse.class);
            handler.accept(response);
        } catch (Exception e) {
            if (exceptionHandler != null) {
                exceptionHandler.accept(e);
            }
        }
    };
    return connection.subscribe(topic, qos, messageHandler);
}
Also used : JobExecutionData(software.amazon.awssdk.iot.iotjobs.model.JobExecutionData) UpdateJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionRequest) NextJobExecutionChangedSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.NextJobExecutionChangedSubscriptionRequest) HashMap(java.util.HashMap) EnumSerializer(software.amazon.awssdk.iot.EnumSerializer) CompletableFuture(java.util.concurrent.CompletableFuture) RejectedError(software.amazon.awssdk.iot.iotjobs.model.RejectedError) JobExecutionState(software.amazon.awssdk.iot.iotjobs.model.JobExecutionState) StartNextPendingJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.StartNextPendingJobExecutionSubscriptionRequest) GsonBuilder(com.google.gson.GsonBuilder) ByteBuffer(java.nio.ByteBuffer) DescribeJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionRequest) DescribeJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionSubscriptionRequest) UpdateJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionSubscriptionRequest) GetPendingJobExecutionsSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsSubscriptionRequest) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) GetPendingJobExecutionsResponse(software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsResponse) Gson(com.google.gson.Gson) JobStatus(software.amazon.awssdk.iot.iotjobs.model.JobStatus) GetPendingJobExecutionsRequest(software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsRequest) StartNextPendingJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.StartNextPendingJobExecutionRequest) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) DescribeJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse) RejectedErrorCode(software.amazon.awssdk.iot.iotjobs.model.RejectedErrorCode) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) StandardCharsets(java.nio.charset.StandardCharsets) Consumer(java.util.function.Consumer) JobExecutionsChangedEvent(software.amazon.awssdk.iot.iotjobs.model.JobExecutionsChangedEvent) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) NextJobExecutionChangedEvent(software.amazon.awssdk.iot.iotjobs.model.NextJobExecutionChangedEvent) StartNextJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.StartNextJobExecutionResponse) JobExecutionsChangedSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.JobExecutionsChangedSubscriptionRequest) Timestamp(software.amazon.awssdk.iot.Timestamp) JobExecutionSummary(software.amazon.awssdk.iot.iotjobs.model.JobExecutionSummary) UpdateJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) CompletableFuture(java.util.concurrent.CompletableFuture) DescribeJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with DescribeJobExecutionSubscriptionRequest

use of software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionSubscriptionRequest in project aws-iot-device-sdk-java-v2 by aws.

the class IotJobsClient method SubscribeToDescribeJobExecutionRejected.

/**
 * Subscribes to the rejected topic for the DescribeJobExecution operation
 *
 * Once subscribed, `handler` is invoked each time a message matching
 * the `topic` is received. It is possible for such messages to arrive before
 * the SUBACK is received.
 *
 * AWS documentation: https://docs.aws.amazon.com/iot/latest/developerguide/jobs-api.html#mqtt-describejobexecution
 *
 * @param request Subscription request configuration
 * @param qos Maximum requested QoS that server may use when sending messages to the client.
 *            The server may grant a lower QoS in the SUBACK
 * @param handler callback function to invoke with messages received on the subscription topic
 * @param exceptionHandler callback function to invoke if an exception occurred deserializing a message
 *
 * @return a future containing the MQTT packet id used to perform the subscribe operation
 */
public CompletableFuture<Integer> SubscribeToDescribeJobExecutionRejected(DescribeJobExecutionSubscriptionRequest request, QualityOfService qos, Consumer<RejectedError> handler, Consumer<Exception> exceptionHandler) {
    String topic = "$aws/things/{thingName}/jobs/{jobId}/get/rejected";
    if (request.thingName == null) {
        CompletableFuture<Integer> result = new CompletableFuture<Integer>();
        result.completeExceptionally(new MqttException("DescribeJobExecutionSubscriptionRequest must have a non-null thingName"));
        return result;
    }
    topic = topic.replace("{thingName}", request.thingName);
    if (request.jobId == null) {
        CompletableFuture<Integer> result = new CompletableFuture<Integer>();
        result.completeExceptionally(new MqttException("DescribeJobExecutionSubscriptionRequest must have a non-null jobId"));
        return result;
    }
    topic = topic.replace("{jobId}", request.jobId);
    Consumer<MqttMessage> messageHandler = (message) -> {
        try {
            String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
            RejectedError response = gson.fromJson(payload, RejectedError.class);
            handler.accept(response);
        } catch (Exception e) {
            if (exceptionHandler != null) {
                exceptionHandler.accept(e);
            }
        }
    };
    return connection.subscribe(topic, qos, messageHandler);
}
Also used : JobExecutionData(software.amazon.awssdk.iot.iotjobs.model.JobExecutionData) UpdateJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionRequest) NextJobExecutionChangedSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.NextJobExecutionChangedSubscriptionRequest) HashMap(java.util.HashMap) EnumSerializer(software.amazon.awssdk.iot.EnumSerializer) CompletableFuture(java.util.concurrent.CompletableFuture) RejectedError(software.amazon.awssdk.iot.iotjobs.model.RejectedError) JobExecutionState(software.amazon.awssdk.iot.iotjobs.model.JobExecutionState) StartNextPendingJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.StartNextPendingJobExecutionSubscriptionRequest) GsonBuilder(com.google.gson.GsonBuilder) ByteBuffer(java.nio.ByteBuffer) DescribeJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionRequest) DescribeJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionSubscriptionRequest) UpdateJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionSubscriptionRequest) GetPendingJobExecutionsSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsSubscriptionRequest) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) GetPendingJobExecutionsResponse(software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsResponse) Gson(com.google.gson.Gson) JobStatus(software.amazon.awssdk.iot.iotjobs.model.JobStatus) GetPendingJobExecutionsRequest(software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsRequest) StartNextPendingJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.StartNextPendingJobExecutionRequest) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) DescribeJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse) RejectedErrorCode(software.amazon.awssdk.iot.iotjobs.model.RejectedErrorCode) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) StandardCharsets(java.nio.charset.StandardCharsets) Consumer(java.util.function.Consumer) JobExecutionsChangedEvent(software.amazon.awssdk.iot.iotjobs.model.JobExecutionsChangedEvent) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) NextJobExecutionChangedEvent(software.amazon.awssdk.iot.iotjobs.model.NextJobExecutionChangedEvent) StartNextJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.StartNextJobExecutionResponse) JobExecutionsChangedSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.JobExecutionsChangedSubscriptionRequest) Timestamp(software.amazon.awssdk.iot.Timestamp) JobExecutionSummary(software.amazon.awssdk.iot.iotjobs.model.JobExecutionSummary) UpdateJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RejectedError(software.amazon.awssdk.iot.iotjobs.model.RejectedError)

Example 4 with DescribeJobExecutionSubscriptionRequest

use of software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionSubscriptionRequest in project aws-iot-device-sdk-java-v2 by aws.

the class JobsSample method main.

public static void main(String[] args) {
    cmdUtils = new CommandLineUtils();
    cmdUtils.registerProgramName("JobsSample");
    cmdUtils.addCommonMQTTCommands();
    cmdUtils.registerCommand("key", "<path>", "Path to your key in PEM format.");
    cmdUtils.registerCommand("cert", "<path>", "Path to your client certificate in PEM format.");
    cmdUtils.registerCommand("client_id", "<int>", "Client id to use (optional, default='test-*').");
    cmdUtils.registerCommand("thing_name", "<str>", "The name of the IoT thing.");
    cmdUtils.registerCommand("port", "<int>", "Port to connect to on the endpoint (optional, default='8883').");
    cmdUtils.sendArguments(args);
    thingName = cmdUtils.getCommandRequired("thing_name", "");
    MqttClientConnectionEvents callbacks = new MqttClientConnectionEvents() {

        @Override
        public void onConnectionInterrupted(int errorCode) {
            if (errorCode != 0) {
                System.out.println("Connection interrupted: " + errorCode + ": " + CRT.awsErrorString(errorCode));
            }
        }

        @Override
        public void onConnectionResumed(boolean sessionPresent) {
            System.out.println("Connection resumed: " + (sessionPresent ? "existing session" : "clean session"));
        }
    };
    try {
        MqttClientConnection connection = cmdUtils.buildMQTTConnection(callbacks);
        IotJobsClient jobs = new IotJobsClient(connection);
        CompletableFuture<Boolean> connected = connection.connect();
        try {
            boolean sessionPresent = connected.get();
            System.out.println("Connected to " + (!sessionPresent ? "new" : "existing") + " session!");
        } catch (Exception ex) {
            throw new RuntimeException("Exception occurred during connect", ex);
        }
        {
            gotResponse = new CompletableFuture<>();
            GetPendingJobExecutionsSubscriptionRequest subscriptionRequest = new GetPendingJobExecutionsSubscriptionRequest();
            subscriptionRequest.thingName = thingName;
            CompletableFuture<Integer> subscribed = jobs.SubscribeToGetPendingJobExecutionsAccepted(subscriptionRequest, QualityOfService.AT_LEAST_ONCE, JobsSample::onGetPendingJobExecutionsAccepted);
            try {
                subscribed.get();
                System.out.println("Subscribed to GetPendingJobExecutionsAccepted");
            } catch (Exception ex) {
                throw new RuntimeException("Failed to subscribe to GetPendingJobExecutions", ex);
            }
            subscribed = jobs.SubscribeToGetPendingJobExecutionsRejected(subscriptionRequest, QualityOfService.AT_LEAST_ONCE, JobsSample::onRejectedError);
            subscribed.get();
            System.out.println("Subscribed to GetPendingJobExecutionsRejected");
            GetPendingJobExecutionsRequest publishRequest = new GetPendingJobExecutionsRequest();
            publishRequest.thingName = thingName;
            CompletableFuture<Integer> published = jobs.PublishGetPendingJobExecutions(publishRequest, QualityOfService.AT_LEAST_ONCE);
            try {
                published.get();
                gotResponse.get();
            } catch (Exception ex) {
                throw new RuntimeException("Exception occurred during publish", ex);
            }
        }
        if (availableJobs.isEmpty()) {
            System.out.println("No jobs queued, no further work to do");
        }
        for (String jobId : availableJobs) {
            gotResponse = new CompletableFuture<>();
            DescribeJobExecutionSubscriptionRequest subscriptionRequest = new DescribeJobExecutionSubscriptionRequest();
            subscriptionRequest.thingName = thingName;
            subscriptionRequest.jobId = jobId;
            jobs.SubscribeToDescribeJobExecutionAccepted(subscriptionRequest, QualityOfService.AT_LEAST_ONCE, JobsSample::onDescribeJobExecutionAccepted);
            jobs.SubscribeToDescribeJobExecutionRejected(subscriptionRequest, QualityOfService.AT_LEAST_ONCE, JobsSample::onRejectedError);
            DescribeJobExecutionRequest publishRequest = new DescribeJobExecutionRequest();
            publishRequest.thingName = thingName;
            publishRequest.jobId = jobId;
            publishRequest.includeJobDocument = true;
            publishRequest.executionNumber = 1L;
            jobs.PublishDescribeJobExecution(publishRequest, QualityOfService.AT_LEAST_ONCE);
            gotResponse.get();
        }
        for (int jobIdx = 0; jobIdx < availableJobs.size(); ++jobIdx) {
            {
                gotResponse = new CompletableFuture<>();
                // Start the next pending job
                StartNextPendingJobExecutionSubscriptionRequest subscriptionRequest = new StartNextPendingJobExecutionSubscriptionRequest();
                subscriptionRequest.thingName = thingName;
                jobs.SubscribeToStartNextPendingJobExecutionAccepted(subscriptionRequest, QualityOfService.AT_LEAST_ONCE, JobsSample::onStartNextPendingJobExecutionAccepted);
                jobs.SubscribeToStartNextPendingJobExecutionRejected(subscriptionRequest, QualityOfService.AT_LEAST_ONCE, JobsSample::onRejectedError);
                StartNextPendingJobExecutionRequest publishRequest = new StartNextPendingJobExecutionRequest();
                publishRequest.thingName = thingName;
                publishRequest.stepTimeoutInMinutes = 15L;
                jobs.PublishStartNextPendingJobExecution(publishRequest, QualityOfService.AT_LEAST_ONCE);
                gotResponse.get();
            }
            {
                // Update the service to let it know we're executing
                gotResponse = new CompletableFuture<>();
                UpdateJobExecutionSubscriptionRequest subscriptionRequest = new UpdateJobExecutionSubscriptionRequest();
                subscriptionRequest.thingName = thingName;
                subscriptionRequest.jobId = currentJobId;
                jobs.SubscribeToUpdateJobExecutionAccepted(subscriptionRequest, QualityOfService.AT_LEAST_ONCE, (response) -> {
                    System.out.println("Marked job " + currentJobId + " IN_PROGRESS");
                    gotResponse.complete(null);
                });
                jobs.SubscribeToUpdateJobExecutionRejected(subscriptionRequest, QualityOfService.AT_LEAST_ONCE, JobsSample::onRejectedError);
                UpdateJobExecutionRequest publishRequest = new UpdateJobExecutionRequest();
                publishRequest.thingName = thingName;
                publishRequest.jobId = currentJobId;
                publishRequest.executionNumber = currentExecutionNumber;
                publishRequest.status = JobStatus.IN_PROGRESS;
                publishRequest.expectedVersion = currentVersionNumber++;
                jobs.PublishUpdateJobExecution(publishRequest, QualityOfService.AT_LEAST_ONCE);
                gotResponse.get();
            }
            // Fake doing something
            Thread.sleep(1000);
            {
                // Update the service to let it know we're done
                gotResponse = new CompletableFuture<>();
                UpdateJobExecutionSubscriptionRequest subscriptionRequest = new UpdateJobExecutionSubscriptionRequest();
                subscriptionRequest.thingName = thingName;
                subscriptionRequest.jobId = currentJobId;
                jobs.SubscribeToUpdateJobExecutionAccepted(subscriptionRequest, QualityOfService.AT_LEAST_ONCE, (response) -> {
                    System.out.println("Marked job " + currentJobId + " SUCCEEDED");
                    gotResponse.complete(null);
                });
                jobs.SubscribeToUpdateJobExecutionRejected(subscriptionRequest, QualityOfService.AT_LEAST_ONCE, JobsSample::onRejectedError);
                UpdateJobExecutionRequest publishRequest = new UpdateJobExecutionRequest();
                publishRequest.thingName = thingName;
                publishRequest.jobId = currentJobId;
                publishRequest.executionNumber = currentExecutionNumber;
                publishRequest.status = JobStatus.SUCCEEDED;
                publishRequest.expectedVersion = currentVersionNumber++;
                jobs.PublishUpdateJobExecution(publishRequest, QualityOfService.AT_LEAST_ONCE);
                gotResponse.get();
            }
        }
        CompletableFuture<Void> disconnected = connection.disconnect();
        disconnected.get();
    } catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
        System.out.println("Exception encountered: " + ex.toString());
    }
    CrtResource.waitForNoResources();
    System.out.println("Complete!");
}
Also used : CommandLineUtils(utils.commandlineutils.CommandLineUtils) UpdateJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionRequest) CompletableFuture(java.util.concurrent.CompletableFuture) RejectedError(software.amazon.awssdk.iot.iotjobs.model.RejectedError) MqttClientConnectionEvents(software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents) StartNextPendingJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.StartNextPendingJobExecutionSubscriptionRequest) DescribeJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionRequest) DescribeJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionSubscriptionRequest) CrtResource(software.amazon.awssdk.crt.CrtResource) UpdateJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionSubscriptionRequest) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) GetPendingJobExecutionsSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsSubscriptionRequest) GetPendingJobExecutionsResponse(software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsResponse) AwsIotMqttConnectionBuilder(software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder) LinkedList(java.util.LinkedList) JobStatus(software.amazon.awssdk.iot.iotjobs.model.JobStatus) CRT(software.amazon.awssdk.crt.CRT) IotJobsClient(software.amazon.awssdk.iot.iotjobs.IotJobsClient) GetPendingJobExecutionsRequest(software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsRequest) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) StartNextPendingJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.StartNextPendingJobExecutionRequest) DescribeJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse) UUID(java.util.UUID) ExecutionException(java.util.concurrent.ExecutionException) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) List(java.util.List) StartNextJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.StartNextJobExecutionResponse) JobExecutionSummary(software.amazon.awssdk.iot.iotjobs.model.JobExecutionSummary) Log(software.amazon.awssdk.crt.Log) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) MqttClientConnectionEvents(software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents) GetPendingJobExecutionsSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsSubscriptionRequest) IotJobsClient(software.amazon.awssdk.iot.iotjobs.IotJobsClient) CompletableFuture(java.util.concurrent.CompletableFuture) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) CommandLineUtils(utils.commandlineutils.CommandLineUtils) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) GetPendingJobExecutionsRequest(software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsRequest) StartNextPendingJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.StartNextPendingJobExecutionRequest) DescribeJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionRequest) UpdateJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionSubscriptionRequest) ExecutionException(java.util.concurrent.ExecutionException) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) DescribeJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionSubscriptionRequest) StartNextPendingJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.StartNextPendingJobExecutionSubscriptionRequest) UpdateJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionRequest)

Example 5 with DescribeJobExecutionSubscriptionRequest

use of software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionSubscriptionRequest in project aws-greengrass-nucleus by aws-greengrass.

the class IotJobsHelperTest method GIVEN_jobsClient_and_mqttConnection_WHEN_mqtt_connected_THEN_subscribe_to_getNextJobDescription.

@Test
void GIVEN_jobsClient_and_mqttConnection_WHEN_mqtt_connected_THEN_subscribe_to_getNextJobDescription() throws Exception {
    iotJobsHelper.postInject();
    CompletableFuture<Integer> integerCompletableFuture = new CompletableFuture<>();
    integerCompletableFuture.complete(1);
    when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionAccepted(any(DescribeJobExecutionSubscriptionRequest.class), eq(QualityOfService.AT_LEAST_ONCE), eq(describeJobConsumer))).thenReturn(integerCompletableFuture);
    when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionRejected(any(DescribeJobExecutionSubscriptionRequest.class), eq(QualityOfService.AT_LEAST_ONCE), eq(rejectedErrorConsumer))).thenReturn(integerCompletableFuture);
    iotJobsHelper.subscribeToGetNextJobDescription(describeJobConsumer, rejectedErrorConsumer);
    ArgumentCaptor<DescribeJobExecutionSubscriptionRequest> requestArgumentCaptor = ArgumentCaptor.forClass(DescribeJobExecutionSubscriptionRequest.class);
    verify(mockIotJobsClientWrapper).SubscribeToDescribeJobExecutionAccepted(requestArgumentCaptor.capture(), eq(QualityOfService.AT_LEAST_ONCE), eq(describeJobConsumer));
    DescribeJobExecutionSubscriptionRequest actualRequest = requestArgumentCaptor.getValue();
    assertEquals(TEST_THING_NAME, actualRequest.thingName);
    assertEquals("$next", actualRequest.jobId);
    verify(mockIotJobsClientWrapper).SubscribeToDescribeJobExecutionRejected(requestArgumentCaptor.capture(), eq(QualityOfService.AT_LEAST_ONCE), eq(rejectedErrorConsumer));
    actualRequest = requestArgumentCaptor.getValue();
    assertEquals(TEST_THING_NAME, actualRequest.thingName);
    assertEquals("$next", actualRequest.jobId);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java.util.concurrent.CompletableFuture) DescribeJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionSubscriptionRequest) Test(org.junit.jupiter.api.Test)

Aggregations

DescribeJobExecutionSubscriptionRequest (software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionSubscriptionRequest)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 MqttClientConnection (software.amazon.awssdk.crt.mqtt.MqttClientConnection)3 MqttException (software.amazon.awssdk.crt.mqtt.MqttException)3 QualityOfService (software.amazon.awssdk.crt.mqtt.QualityOfService)3 DescribeJobExecutionRequest (software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionRequest)3 DescribeJobExecutionResponse (software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse)3 GetPendingJobExecutionsRequest (software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsRequest)3 GetPendingJobExecutionsResponse (software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsResponse)3 GetPendingJobExecutionsSubscriptionRequest (software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsSubscriptionRequest)3 JobExecutionSummary (software.amazon.awssdk.iot.iotjobs.model.JobExecutionSummary)3 JobStatus (software.amazon.awssdk.iot.iotjobs.model.JobStatus)3 RejectedError (software.amazon.awssdk.iot.iotjobs.model.RejectedError)3 StartNextJobExecutionResponse (software.amazon.awssdk.iot.iotjobs.model.StartNextJobExecutionResponse)3 StartNextPendingJobExecutionRequest (software.amazon.awssdk.iot.iotjobs.model.StartNextPendingJobExecutionRequest)3 StartNextPendingJobExecutionSubscriptionRequest (software.amazon.awssdk.iot.iotjobs.model.StartNextPendingJobExecutionSubscriptionRequest)3 UpdateJobExecutionRequest (software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionRequest)3 UpdateJobExecutionSubscriptionRequest (software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionSubscriptionRequest)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2