Search in sources :

Example 1 with MqttClientConnectionEvents

use of software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents in project aws-iot-device-sdk-java-v2 by aws.

the class PubSubStress method initConnections.

static void initConnections() {
    List<ConnectionState> connectionsInProgress = new ArrayList<>();
    for (int i = 0; i < connectionCount; ++i) {
        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"));
            }
        };
        String newClientId = String.format("%s%d", clientId, i);
        MqttClientConnection connection = cmdUtils.buildMQTTConnection(callbacks);
        try {
            ConnectionState connectionState = new ConnectionState();
            connectionState.clientId = newClientId;
            connectionState.connectFuture = connection.connect();
            connectionState.connection = connection;
            connectionsInProgress.add(connectionState);
            if ((i + 1) % PROGRESS_OP_COUNT == 0) {
                System.out.println(String.format("(Main Thread) Connect start count: %d", i + 1));
            }
            // Simple throttle to avoid Iot Connect/Second limit
            Thread.sleep(5);
        } catch (Exception ignored) {
            connection.disconnect();
            connection.close();
        }
    }
    System.out.println(String.format("(Main Thread) Started %d connections", connectionsInProgress.size()));
    for (int i = 0; i < connectionsInProgress.size(); ++i) {
        ConnectionState connectionState = connectionsInProgress.get(i);
        CompletableFuture<Boolean> connectFuture = connectionState.connectFuture;
        if (connectFuture == null) {
            continue;
        }
        try {
            connectFuture.get(5, TimeUnit.SECONDS);
            String clientTopic = String.format("%s%d", topic, i);
            connectionState.topic = clientTopic;
            connectionState.subscribeFuture = connectionState.connection.subscribe(clientTopic, QualityOfService.AT_LEAST_ONCE, (message) -> {
                try {
                    String payload = new String(message.getPayload(), "UTF-8");
                } catch (UnsupportedEncodingException ex) {
                    System.out.println(String.format("(Topic %s): Unable to decode payload: %s", clientTopic, ex.getMessage()));
                }
            });
            if ((i + 1) % PROGRESS_OP_COUNT == 0) {
                System.out.println(String.format("(Main Thread) Subscribe start count: %d", i + 1));
            }
            // Simple throttle to avoid Iot Subscribe/Second limit
            Thread.sleep(5);
        } catch (Exception e) {
            connectionState.connection.disconnect();
            connectionState.connection.close();
            connectionState.connection = null;
        }
    }
    System.out.println(String.format("(Main Thread) Started subscriptions for %d connections", connectionsInProgress.size()));
    for (int i = 0; i < connectionsInProgress.size(); ++i) {
        ConnectionState connectionState = connectionsInProgress.get(i);
        CompletableFuture<Integer> subscribeFuture = connectionState.subscribeFuture;
        if (subscribeFuture == null) {
            continue;
        }
        try {
            subscribeFuture.get(5, TimeUnit.SECONDS);
            connections.put(connectionState.clientId, connectionState.connection);
            validClientIds.add(connectionState.clientId);
            validTopics.add(connectionState.topic);
        } catch (Exception e) {
            connectionState.connection.disconnect();
            connectionState.connection.close();
            connectionState.connection = null;
        }
    }
    System.out.println(String.format("(Main Thread) Successfully established %d connections", connections.size()));
}
Also used : CommandLineUtils(utils.commandlineutils.CommandLineUtils) java.util(java.util) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) CompletableFuture(java.util.concurrent.CompletableFuture) RejectedError(software.amazon.awssdk.iot.iotjobs.model.RejectedError) MqttClientConnectionEvents(software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents) TimeUnit(java.util.concurrent.TimeUnit) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) CrtResource(software.amazon.awssdk.crt.CrtResource) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) AwsIotMqttConnectionBuilder(software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder) HttpProxyOptions(software.amazon.awssdk.crt.http.HttpProxyOptions) Log(software.amazon.awssdk.crt.Log) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CRT(software.amazon.awssdk.crt.CRT) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MqttClientConnectionEvents(software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with MqttClientConnectionEvents

use of software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents in project aws-iot-device-sdk-java-v2 by aws.

the class ShadowSample method main.

public static void main(String[] args) {
    cmdUtils = new CommandLineUtils();
    cmdUtils.registerProgramName("ShadowSample");
    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("port", "<int>", "Port to use (optional, default='8883').");
    cmdUtils.registerCommand("thing_name", "<str>", "The name of the IoT thing.");
    cmdUtils.registerCommand("client_id", "<int>", "Client id to use (optional, default='test-*')");
    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);
        shadow = new IotShadowClient(connection);
        CompletableFuture<Boolean> connected = connection.connect();
        try {
            boolean sessionPresent = connected.get();
            System.out.println("Connected to " + (!sessionPresent ? "clean" : "existing") + " session!");
        } catch (Exception ex) {
            throw new RuntimeException("Exception occurred during connect", ex);
        }
        System.out.println("Subscribing to shadow delta events...");
        ShadowDeltaUpdatedSubscriptionRequest requestShadowDeltaUpdated = new ShadowDeltaUpdatedSubscriptionRequest();
        requestShadowDeltaUpdated.thingName = thingName;
        CompletableFuture<Integer> subscribedToDeltas = shadow.SubscribeToShadowDeltaUpdatedEvents(requestShadowDeltaUpdated, QualityOfService.AT_LEAST_ONCE, ShadowSample::onShadowDeltaUpdated);
        subscribedToDeltas.get();
        System.out.println("Subscribing to update respones...");
        UpdateShadowSubscriptionRequest requestUpdateShadow = new UpdateShadowSubscriptionRequest();
        requestUpdateShadow.thingName = thingName;
        CompletableFuture<Integer> subscribedToUpdateAccepted = shadow.SubscribeToUpdateShadowAccepted(requestUpdateShadow, QualityOfService.AT_LEAST_ONCE, ShadowSample::onUpdateShadowAccepted);
        CompletableFuture<Integer> subscribedToUpdateRejected = shadow.SubscribeToUpdateShadowRejected(requestUpdateShadow, QualityOfService.AT_LEAST_ONCE, ShadowSample::onUpdateShadowRejected);
        subscribedToUpdateAccepted.get();
        subscribedToUpdateRejected.get();
        System.out.println("Subscribing to get responses...");
        GetShadowSubscriptionRequest requestGetShadow = new GetShadowSubscriptionRequest();
        requestGetShadow.thingName = thingName;
        CompletableFuture<Integer> subscribedToGetShadowAccepted = shadow.SubscribeToGetShadowAccepted(requestGetShadow, QualityOfService.AT_LEAST_ONCE, ShadowSample::onGetShadowAccepted);
        CompletableFuture<Integer> subscribedToGetShadowRejected = shadow.SubscribeToGetShadowRejected(requestGetShadow, QualityOfService.AT_LEAST_ONCE, ShadowSample::onGetShadowRejected);
        subscribedToGetShadowAccepted.get();
        subscribedToGetShadowRejected.get();
        gotResponse = new CompletableFuture<>();
        System.out.println("Requesting current shadow state...");
        GetShadowRequest getShadowRequest = new GetShadowRequest();
        getShadowRequest.thingName = thingName;
        CompletableFuture<Integer> publishedGetShadow = shadow.PublishGetShadow(getShadowRequest, QualityOfService.AT_LEAST_ONCE);
        publishedGetShadow.get();
        gotResponse.get();
        String newValue = "";
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print(SHADOW_PROPERTY + "> ");
            System.out.flush();
            newValue = scanner.next();
            if (newValue.compareToIgnoreCase("quit") == 0) {
                break;
            }
            gotResponse = new CompletableFuture<>();
            changeShadowValue(newValue).get();
            gotResponse.get();
        }
        scanner.close();
        CompletableFuture<Void> disconnected = connection.disconnect();
        disconnected.get();
    } catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
        System.out.println("Exception encountered: " + ex.toString());
    }
    System.out.println("Complete!");
    CrtResource.waitForNoResources();
}
Also used : Scanner(java.util.Scanner) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) MqttClientConnectionEvents(software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents) UpdateShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.UpdateShadowSubscriptionRequest) IotShadowClient(software.amazon.awssdk.iot.iotshadow.IotShadowClient) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) GetShadowSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.GetShadowSubscriptionRequest) CommandLineUtils(utils.commandlineutils.CommandLineUtils) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) GetShadowRequest(software.amazon.awssdk.iot.iotshadow.model.GetShadowRequest) ExecutionException(java.util.concurrent.ExecutionException) ShadowDeltaUpdatedSubscriptionRequest(software.amazon.awssdk.iot.iotshadow.model.ShadowDeltaUpdatedSubscriptionRequest) ExecutionException(java.util.concurrent.ExecutionException) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException)

Example 3 with MqttClientConnectionEvents

use of software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents in project aws-iot-device-sdk-java-v2 by aws.

the class BasicConnect method main.

public static void main(String[] args) {
    cmdUtils = new CommandLineUtils();
    cmdUtils.registerProgramName("BasicConnect");
    cmdUtils.addCommonMQTTCommands();
    cmdUtils.addCommonProxyCommands();
    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("port", "<int>", "Port to connect to on the endpoint (optional, default='8883').");
    cmdUtils.sendArguments(args);
    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 {
        // Create a connection using a certificate and key
        // Note: The data for the connection is gotten from cmdUtils.
        // (see buildDirectMQTTConnection for implementation)
        MqttClientConnection connection = cmdUtils.buildDirectMQTTConnection(callbacks);
        if (connection == null) {
            onApplicationFailure(new RuntimeException("MQTT connection creation failed!"));
        }
        // Connect and disconnect using the connection we created
        // (see sampleConnectAndDisconnect for implementation)
        cmdUtils.sampleConnectAndDisconnect(connection);
    } catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
        onApplicationFailure(ex);
    }
    CrtResource.waitForNoResources();
    System.out.println("Complete!");
}
Also used : CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) CommandLineUtils(utils.commandlineutils.CommandLineUtils) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) MqttClientConnectionEvents(software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with MqttClientConnectionEvents

use of software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents in project aws-iot-device-sdk-java-v2 by aws.

the class FleetProvisioningSample method main.

public static void main(String[] args) {
    cmdUtils = new CommandLineUtils();
    cmdUtils.registerProgramName("FleetProvisioningSample");
    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("port", "<int>", "Port to connect to on the endpoint (optional, default='8883').");
    cmdUtils.registerCommand("template_name", "<str>", "Provisioning template name.");
    cmdUtils.registerCommand("template_parameters", "<json>", "Provisioning template parameters.");
    cmdUtils.registerCommand("csr", "<path>", "Path to the CSR file (optional).");
    cmdUtils.sendArguments(args);
    templateName = cmdUtils.getCommandRequired("template_name", "");
    templateParameters = cmdUtils.getCommandRequired("template_parameters", "");
    csrPath = cmdUtils.getCommandOrDefault("csr", csrPath);
    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);
        iotIdentityClient = new IotIdentityClient(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);
        }
        try {
            if (csrPath == null) {
                createKeysAndCertificateWorkflow();
            } else {
                createCertificateFromCsrWorkflow();
            }
        } catch (Exception e) {
            throw new RuntimeException("Exception occurred during connect", e);
        }
        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 : MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) MqttClientConnectionEvents(software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents) ExecutionException(java.util.concurrent.ExecutionException) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) CommandLineUtils(utils.commandlineutils.CommandLineUtils) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) IotIdentityClient(software.amazon.awssdk.iot.iotidentity.IotIdentityClient) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with MqttClientConnectionEvents

use of software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents 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)

Aggregations

MqttClientConnection (software.amazon.awssdk.crt.mqtt.MqttClientConnection)9 MqttClientConnectionEvents (software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents)9 CommandLineUtils (utils.commandlineutils.CommandLineUtils)9 ExecutionException (java.util.concurrent.ExecutionException)8 CrtRuntimeException (software.amazon.awssdk.crt.CrtRuntimeException)8 CompletableFuture (java.util.concurrent.CompletableFuture)2 CRT (software.amazon.awssdk.crt.CRT)2 CrtResource (software.amazon.awssdk.crt.CrtResource)2 Log (software.amazon.awssdk.crt.Log)2 MqttMessage (software.amazon.awssdk.crt.mqtt.MqttMessage)2 QualityOfService (software.amazon.awssdk.crt.mqtt.QualityOfService)2 AwsIotMqttConnectionBuilder (software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder)2 RejectedError (software.amazon.awssdk.iot.iotjobs.model.RejectedError)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 java.util (java.util)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Scanner (java.util.Scanner)1 UUID (java.util.UUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1