use of software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsSubscriptionRequest in project aws-iot-device-sdk-java-v2 by aws.
the class IotJobsClient method SubscribeToGetPendingJobExecutionsAccepted.
/**
* Subscribes to the accepted topic for the GetPendingJobsExecutions 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-getpendingjobexecutions
*
* @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> SubscribeToGetPendingJobExecutionsAccepted(GetPendingJobExecutionsSubscriptionRequest request, QualityOfService qos, Consumer<GetPendingJobExecutionsResponse> handler, Consumer<Exception> exceptionHandler) {
String topic = "$aws/things/{thingName}/jobs/get/accepted";
if (request.thingName == null) {
CompletableFuture<Integer> result = new CompletableFuture<Integer>();
result.completeExceptionally(new MqttException("GetPendingJobExecutionsSubscriptionRequest must have a non-null thingName"));
return result;
}
topic = topic.replace("{thingName}", request.thingName);
Consumer<MqttMessage> messageHandler = (message) -> {
try {
String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
GetPendingJobExecutionsResponse response = gson.fromJson(payload, GetPendingJobExecutionsResponse.class);
handler.accept(response);
} catch (Exception e) {
if (exceptionHandler != null) {
exceptionHandler.accept(e);
}
}
};
return connection.subscribe(topic, qos, messageHandler);
}
use of software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsSubscriptionRequest in project aws-iot-device-sdk-java-v2 by aws.
the class IotJobsClient method SubscribeToGetPendingJobExecutionsRejected.
/**
* Subscribes to the rejected topic for the GetPendingJobsExecutions 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-getpendingjobexecutions
*
* @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> SubscribeToGetPendingJobExecutionsRejected(GetPendingJobExecutionsSubscriptionRequest request, QualityOfService qos, Consumer<RejectedError> handler, Consumer<Exception> exceptionHandler) {
String topic = "$aws/things/{thingName}/jobs/get/rejected";
if (request.thingName == null) {
CompletableFuture<Integer> result = new CompletableFuture<Integer>();
result.completeExceptionally(new MqttException("GetPendingJobExecutionsSubscriptionRequest must have a non-null thingName"));
return result;
}
topic = topic.replace("{thingName}", request.thingName);
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);
}
use of software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsSubscriptionRequest 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!");
}
Aggregations