use of com.aws.greengrass.mqttclient.spool.SpoolMessage in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClient method publishSingleSpoolerMessage.
@SuppressWarnings({ "PMD.AvoidCatchingThrowable", "PMD.PreserveStackTrace" })
protected CompletableFuture<Integer> publishSingleSpoolerMessage(AwsIotMqttClient connection) throws InterruptedException {
long id = -1L;
try {
id = spool.popId();
SpoolMessage spooledMessage = spool.getMessageById(id);
PublishRequest request = spooledMessage.getRequest();
MqttMessage m = new MqttMessage(request.getTopic(), request.getPayload());
long finalId = id;
return connection.publish(m, request.getQos(), request.isRetain()).whenComplete((packetId, throwable) -> {
// packetId is the SDK assigned ID. Ignore this and instead use the spooler ID
if (throwable == null) {
spool.removeMessageById(finalId);
logger.atTrace().kv("id", finalId).kv("topic", request.getTopic()).log("Successfully published message");
} else {
if (maxPublishRetryCount == -1 || spooledMessage.getRetried().getAndIncrement() < maxPublishRetryCount) {
spool.addId(finalId);
logger.atError().log("Failed to publish the message via Spooler and will retry", throwable);
} else {
logger.atError().log("Failed to publish the message via Spooler" + " after retried {} times and will drop the message", maxPublishRetryCount, throwable);
}
}
});
} catch (Throwable t) {
// valid id is starting from 0
if (id >= 0) {
spool.addId(id);
}
if (Utils.getUltimateCause(t) instanceof InterruptedException) {
throw new InterruptedException("Interrupted while publishing from spooler");
}
CompletableFuture<Integer> fut = new CompletableFuture<>();
fut.completeExceptionally(t);
return fut;
}
}
use of com.aws.greengrass.mqttclient.spool.SpoolMessage in project aws-greengrass-nucleus by aws-greengrass.
the class InMemorySpoolTest method GIVEN_id_WHEN_remove_message_by_id_THEN_spooler_size_decreased.
@Test
void GIVEN_id_WHEN_remove_message_by_id_THEN_spooler_size_decreased() throws SpoolerStoreException, InterruptedException {
PublishRequest request = PublishRequest.builder().topic("spool").payload(ByteBuffer.allocate(10).array()).qos(QualityOfService.AT_LEAST_ONCE).build();
SpoolMessage message = spool.addMessage(request);
long id = message.getId();
spool.removeMessageById(id);
assertEquals(0, spool.getCurrentSpoolerSize());
}
use of com.aws.greengrass.mqttclient.spool.SpoolMessage in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientTest method GIVEN_publish_request_unsuccessfully_WHEN_spool_single_message_THEN_add_id_back_to_spooler_if_will_retry.
@Test
void GIVEN_publish_request_unsuccessfully_WHEN_spool_single_message_THEN_add_id_back_to_spooler_if_will_retry(ExtensionContext context) throws InterruptedException {
ignoreExceptionOfType(context, ExecutionException.class);
MqttClient client = spy(new MqttClient(deviceConfiguration, spool, true, (c) -> builder, executorService));
long id = 1L;
when(spool.popId()).thenReturn(id);
PublishRequest request = PublishRequest.builder().topic("spool").payload("What's up".getBytes(StandardCharsets.UTF_8)).qos(QualityOfService.AT_LEAST_ONCE).build();
SpoolMessage message = SpoolMessage.builder().id(id).request(request).build();
when(spool.getMessageById(id)).thenReturn(message);
AwsIotMqttClient awsIotMqttClient = mock(AwsIotMqttClient.class);
CompletableFuture<Integer> future = new CompletableFuture<>();
future.completeExceptionally(new ExecutionException("exception", new Throwable()));
when(awsIotMqttClient.publish(any(), any(), anyBoolean())).thenReturn(future);
client.publishSingleSpoolerMessage(awsIotMqttClient);
verify(awsIotMqttClient).publish(any(), any(), anyBoolean());
verify(spool, never()).removeMessageById(anyLong());
verify(spool).addId(anyLong());
}
use of com.aws.greengrass.mqttclient.spool.SpoolMessage in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientTest method GIVEN_reserved_topic_including_prefix_equal_to_topic_size_limit_WHEN_publish_THEN_future_complete.
@Test
void GIVEN_reserved_topic_including_prefix_equal_to_topic_size_limit_WHEN_publish_THEN_future_complete() throws SpoolerStoreException, InterruptedException, ExecutionException {
MqttClient client = spy(new MqttClient(deviceConfiguration, spool, false, (c) -> builder, executorService));
String topic = String.join("", Collections.nCopies(MAX_LENGTH_OF_TOPIC, "a"));
PublishRequest request = PublishRequest.builder().topic(reservedTopicPrefix + topic).payload(new byte[1]).qos(QualityOfService.AT_LEAST_ONCE).build();
SpoolMessage message = SpoolMessage.builder().id(0L).request(request).build();
when(spool.addMessage(request)).thenReturn(message);
CompletableFuture<Integer> future = client.publish(request);
assertEquals(0, future.get());
verify(spool, times(1)).addMessage(request);
verify(spool, never()).getSpoolConfig();
}
use of com.aws.greengrass.mqttclient.spool.SpoolMessage in project aws-greengrass-nucleus by aws-greengrass.
the class MqttClientTest method GIVEN_qos_is_1_and_mqtt_is_offline_WHEN_publish_THEN_return_future_complete.
@Test
void GIVEN_qos_is_1_and_mqtt_is_offline_WHEN_publish_THEN_return_future_complete() throws ExecutionException, InterruptedException, SpoolerStoreException {
MqttClient client = spy(new MqttClient(deviceConfiguration, spool, false, (c) -> builder, executorService));
PublishRequest request = PublishRequest.builder().topic("spool").payload(new byte[0]).qos(QualityOfService.AT_LEAST_ONCE).build();
SpoolMessage message = SpoolMessage.builder().id(0L).request(request).build();
when(spool.addMessage(request)).thenReturn(message);
CompletableFuture<Integer> future = client.publish(request);
assertEquals(0, future.get());
verify(spool, times(1)).addMessage(request);
verify(spool, never()).getSpoolConfig();
}
Aggregations