Search in sources :

Example 1 with JobExecutionsChangedEvent

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

Example 2 with JobExecutionsChangedEvent

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JobExecutionsChangedEvent(software.amazon.awssdk.iot.iotjobs.model.JobExecutionsChangedEvent) Deployment(com.aws.greengrass.deployment.model.Deployment) DeploymentTaskMetadata(com.aws.greengrass.deployment.model.DeploymentTaskMetadata) Test(org.junit.jupiter.api.Test)

Example 3 with JobExecutionsChangedEvent

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);
}
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) JobExecutionsChangedEvent(software.amazon.awssdk.iot.iotjobs.model.JobExecutionsChangedEvent) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with JobExecutionsChangedEvent

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));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JobStatus(software.amazon.awssdk.iot.iotjobs.model.JobStatus) HashMap(java.util.HashMap) JobExecutionsChangedEvent(software.amazon.awssdk.iot.iotjobs.model.JobExecutionsChangedEvent) JobExecutionSummary(software.amazon.awssdk.iot.iotjobs.model.JobExecutionSummary) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 5 with JobExecutionsChangedEvent

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));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JobStatus(software.amazon.awssdk.iot.iotjobs.model.JobStatus) HashMap(java.util.HashMap) JobExecutionsChangedEvent(software.amazon.awssdk.iot.iotjobs.model.JobExecutionsChangedEvent) JobExecutionSummary(software.amazon.awssdk.iot.iotjobs.model.JobExecutionSummary) List(java.util.List) Test(org.junit.jupiter.api.Test)

Aggregations

JobExecutionsChangedEvent (software.amazon.awssdk.iot.iotjobs.model.JobExecutionsChangedEvent)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Test (org.junit.jupiter.api.Test)4 HashMap (java.util.HashMap)3 JobExecutionSummary (software.amazon.awssdk.iot.iotjobs.model.JobExecutionSummary)3 JobStatus (software.amazon.awssdk.iot.iotjobs.model.JobStatus)3 DeploymentTaskMetadata (com.aws.greengrass.deployment.model.DeploymentTaskMetadata)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 MqttException (software.amazon.awssdk.crt.mqtt.MqttException)2 MqttMessage (software.amazon.awssdk.crt.mqtt.MqttMessage)2 Deployment (com.aws.greengrass.deployment.model.Deployment)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ByteBuffer (java.nio.ByteBuffer)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Consumer (java.util.function.Consumer)1 MqttClientConnection (software.amazon.awssdk.crt.mqtt.MqttClientConnection)1 QualityOfService (software.amazon.awssdk.crt.mqtt.QualityOfService)1