use of software.amazon.awssdk.iot.iotjobs.model.JobExecutionsChangedEvent in project aws-greengrass-nucleus by aws-greengrass.
the class IotJobsClientWrapper method SubscribeToJobExecutionsChangedEvents.
@Override
public CompletableFuture<Integer> SubscribeToJobExecutionsChangedEvents(JobExecutionsChangedSubscriptionRequest request, QualityOfService qos, Consumer<JobExecutionsChangedEvent> handler, Consumer<Exception> exceptionHandler) {
if (request.thingName == null) {
CompletableFuture<Integer> result = new CompletableFuture();
result.completeExceptionally(new MqttException("JobExecutionsChangedSubscriptionRequest must have a non-null thingName"));
return result;
}
String topic = String.format(JOB_EXECUTIONS_CHANGED_TOPIC, request.thingName);
Consumer<MqttMessage> messageHandler = jobExecutionCbs.computeIfAbsent(new Pair<>(handler, exceptionHandler), (k) -> (message) -> {
try {
String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
JobExecutionsChangedEvent response = this.gson.fromJson(payload, JobExecutionsChangedEvent.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.iot.iotjobs.model.JobExecutionsChangedEvent in project aws-greengrass-nucleus by aws-greengrass.
the class IotJobsHelperTest method GIVEN_ongoing_job_deployment_WHEN_notification_with_empty_jobs_list_THEN_cancel_current_deployment.
@Test
void GIVEN_ongoing_job_deployment_WHEN_notification_with_empty_jobs_list_THEN_cancel_current_deployment() throws Exception {
iotJobsHelper.postInject();
iotJobsHelper.setDeploymentQueue(mockDeploymentQueue);
when(mockDeploymentQueue.isEmpty()).thenReturn(true);
CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.completedFuture(1);
when(mockIotJobsClientWrapper.SubscribeToJobExecutionsChangedEvents(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionRejected(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
DeploymentTaskMetadata mockCurrentDeploymentTaskMetadata = mock(DeploymentTaskMetadata.class);
when(mockCurrentDeploymentTaskMetadata.getDeploymentType()).thenReturn(IOT_JOBS);
when(mockCurrentDeploymentTaskMetadata.isCancellable()).thenReturn(true);
when(mockDeploymentService.getCurrentDeploymentTaskMetadata()).thenReturn(mockCurrentDeploymentTaskMetadata);
iotJobsHelper.subscribeToJobsTopics();
verify(mockIotJobsClientWrapper, times(2)).SubscribeToJobExecutionsChangedEvents(any(), eq(QualityOfService.AT_LEAST_ONCE), eventChangeResponseCaptor.capture());
JobExecutionsChangedEvent event = new JobExecutionsChangedEvent();
event.jobs = new HashMap<>();
eventChangeResponseCaptor.getValue().accept(event);
verify(mockIotJobsClientWrapper, times(2)).PublishDescribeJobExecution(any(), eq(QualityOfService.AT_LEAST_ONCE));
ArgumentCaptor<Deployment> deploymentArgumentCaptor = ArgumentCaptor.forClass(Deployment.class);
verify(mockDeploymentQueue).offer(deploymentArgumentCaptor.capture());
Deployment actualDeployment = deploymentArgumentCaptor.getValue();
assertTrue(actualDeployment.isCancelled());
assertEquals(IOT_JOBS, actualDeployment.getDeploymentType());
}
use of software.amazon.awssdk.iot.iotjobs.model.JobExecutionsChangedEvent in project aws-iot-device-sdk-java-v2 by aws.
the class IotJobsClient method SubscribeToJobExecutionsChangedEvents.
/**
* Subscribes to JobExecutionsChanged notifications for a given IoT thing.
*
* 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-jobexecutionschanged
*
* @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> SubscribeToJobExecutionsChangedEvents(JobExecutionsChangedSubscriptionRequest request, QualityOfService qos, Consumer<JobExecutionsChangedEvent> handler, Consumer<Exception> exceptionHandler) {
String topic = "$aws/things/{thingName}/jobs/notify";
if (request.thingName == null) {
CompletableFuture<Integer> result = new CompletableFuture<Integer>();
result.completeExceptionally(new MqttException("JobExecutionsChangedSubscriptionRequest 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);
JobExecutionsChangedEvent response = gson.fromJson(payload, JobExecutionsChangedEvent.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.JobExecutionsChangedEvent in project aws-greengrass-nucleus by aws-greengrass.
the class IotJobsHelperTest method GIVEN_connected_to_iot_WHEN_subscribe_to_jobs_topics_THEN_get_notification_for_queued_jobs.
@Test
void GIVEN_connected_to_iot_WHEN_subscribe_to_jobs_topics_THEN_get_notification_for_queued_jobs() throws Exception {
iotJobsHelper.postInject();
CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.completedFuture(1);
when(mockIotJobsClientWrapper.SubscribeToJobExecutionsChangedEvents(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionRejected(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
iotJobsHelper.subscribeToJobsTopics();
verify(mockIotJobsClientWrapper, times(2)).SubscribeToJobExecutionsChangedEvents(any(), eq(QualityOfService.AT_LEAST_ONCE), eventChangeResponseCaptor.capture());
JobExecutionsChangedEvent event = new JobExecutionsChangedEvent();
HashMap<JobStatus, List<JobExecutionSummary>> jobs = new HashMap<>();
jobs.put(JobStatus.QUEUED, Arrays.asList(new JobExecutionSummary()));
event.jobs = jobs;
eventChangeResponseCaptor.getValue().accept(event);
verify(mockIotJobsClientWrapper, times(3)).PublishDescribeJobExecution(any(), eq(QualityOfService.AT_LEAST_ONCE));
}
use of software.amazon.awssdk.iot.iotjobs.model.JobExecutionsChangedEvent in project aws-greengrass-nucleus by aws-greengrass.
the class IotJobsHelperTest method GIVEN_connected_to_iot_WHEN_subscribe_to_jobs_topics_THEN_get_notification_for_in_progress_jobs.
@Test
void GIVEN_connected_to_iot_WHEN_subscribe_to_jobs_topics_THEN_get_notification_for_in_progress_jobs() throws Exception {
iotJobsHelper.postInject();
CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.completedFuture(1);
when(mockIotJobsClientWrapper.SubscribeToJobExecutionsChangedEvents(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionRejected(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
iotJobsHelper.subscribeToJobsTopics();
verify(mockIotJobsClientWrapper, times(2)).SubscribeToJobExecutionsChangedEvents(any(), eq(QualityOfService.AT_LEAST_ONCE), eventChangeResponseCaptor.capture());
JobExecutionsChangedEvent event = new JobExecutionsChangedEvent();
HashMap<JobStatus, List<JobExecutionSummary>> jobs = new HashMap<>();
jobs.put(JobStatus.IN_PROGRESS, Arrays.asList(new JobExecutionSummary()));
event.jobs = jobs;
eventChangeResponseCaptor.getValue().accept(event);
verify(mockIotJobsClientWrapper, times(2)).PublishDescribeJobExecution(any(), eq(QualityOfService.AT_LEAST_ONCE));
}
Aggregations