use of software.amazon.awssdk.crt.mqtt.MqttMessage in project aws-crt-java by awslabs.
the class SelfPubSubTest method testPubSub.
@Test
public void testPubSub() {
skipIfNetworkUnavailable();
connect();
try {
CompletableFuture<MqttMessage> receivedFuture = new CompletableFuture<>();
Consumer<MqttMessage> messageHandler = (message) -> {
receivedFuture.complete(message);
};
CompletableFuture<Integer> subscribed = connection.subscribe(TEST_TOPIC, QualityOfService.AT_LEAST_ONCE, messageHandler);
subscribed.thenApply(unused -> subsAcked++);
int packetId = subscribed.get();
assertNotSame(0, packetId);
assertEquals("Single subscription", 1, subsAcked);
MqttMessage message = new MqttMessage(TEST_TOPIC, TEST_PAYLOAD.getBytes(), QualityOfService.AT_LEAST_ONCE, false);
CompletableFuture<Integer> published = connection.publish(message);
published.thenApply(unused -> pubsAcked++);
packetId = published.get();
assertNotSame(0, packetId);
assertEquals("Published", 1, pubsAcked);
published = connection.publish(message);
published.thenApply(unused -> pubsAcked++);
packetId = published.get();
assertNotSame(0, packetId);
assertEquals("Published", 2, pubsAcked);
MqttMessage received = receivedFuture.get();
assertEquals("Received", message.getTopic(), received.getTopic());
assertArrayEquals("Received", message.getPayload(), received.getPayload());
assertEquals("Received", message.getQos(), received.getQos());
assertEquals("Received", message.getRetain(), received.getRetain());
CompletableFuture<Integer> unsubscribed = connection.unsubscribe(TEST_TOPIC);
unsubscribed.thenApply(unused -> subsAcked--);
packetId = unsubscribed.get();
assertNotSame(0, packetId);
assertEquals("No Subscriptions", 0, subsAcked);
} catch (Exception ex) {
fail(ex.getMessage());
}
disconnect();
close();
}
use of software.amazon.awssdk.crt.mqtt.MqttMessage in project aws-crt-java by awslabs.
the class WillTest method testWill.
@Test
public void testWill() {
skipIfNetworkUnavailable();
setConnectionConfigTransformer((config) -> {
config.setWillMessage(new MqttMessage(TEST_TOPIC, TEST_WILL.getBytes(), QualityOfService.AT_LEAST_ONCE));
});
connect();
disconnect();
close();
}
use of software.amazon.awssdk.crt.mqtt.MqttMessage in project aws-crt-java by awslabs.
the class WillTest method testEmptyWill.
@Test
public void testEmptyWill() {
skipIfNetworkUnavailable();
setConnectionConfigTransformer((config) -> {
config.setWillMessage(new MqttMessage(TEST_TOPIC, TEST_EMPTY_WILL.getBytes(), QualityOfService.AT_LEAST_ONCE));
});
connect();
disconnect();
close();
}
use of software.amazon.awssdk.crt.mqtt.MqttMessage in project aws-iot-device-sdk-java-v2 by aws.
the class MQTTPublish method main.
public static void main(String[] args) {
if (!DATestUtils.init(DATestUtils.TestType.SUB_PUB)) {
throw new RuntimeException("Failed to initialize environment variables.");
}
try (AwsIotMqttConnectionBuilder builder = AwsIotMqttConnectionBuilder.newMtlsBuilderFromPath(DATestUtils.certificatePath, DATestUtils.keyPath)) {
builder.withClientId(clientId).withEndpoint(DATestUtils.endpoint).withPort((short) port).withCleanSession(true).withPingTimeoutMs(60000).withProtocolOperationTimeoutMs(60000);
try (MqttClientConnection connection = builder.build()) {
CompletableFuture<Boolean> connected = connection.connect();
try {
boolean sessionPresent = connected.get();
} catch (Exception ex) {
throw new RuntimeException("Exception occurred during connect", ex);
}
CompletableFuture<Integer> published = connection.publish(new MqttMessage(DATestUtils.topic, message.getBytes(), QualityOfService.AT_MOST_ONCE, false));
published.get();
Thread.sleep(1000);
CompletableFuture<Void> disconnected = connection.disconnect();
disconnected.get();
}
} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
onApplicationFailure(ex);
}
System.exit(0);
}
use of software.amazon.awssdk.crt.mqtt.MqttMessage in project aws-iot-device-sdk-java-v2 by aws.
the class IotShadowClient method PublishDeleteNamedShadow.
/**
* Deletes a named shadow for an AWS IoT thing.
*
* If the device is offline, the PUBLISH packet will be sent once the connection resumes.
*
* AWS documentation: https://docs.aws.amazon.com/iot/latest/developerguide/device-shadow-mqtt.html#delete-pub-sub-topic
*
* @param request Message to be serialized and sent
* @param qos Quality of Service for delivering this message
* @return a future containing the MQTT packet id used to perform the publish operation
*
* * For QoS 0, completes as soon as the packet is sent.
* * For QoS 1, completes when PUBACK is received.
* * QoS 2 is not supported by AWS IoT.
*/
public CompletableFuture<Integer> PublishDeleteNamedShadow(DeleteNamedShadowRequest request, QualityOfService qos) {
String topic = "$aws/things/{thingName}/shadow/name/{shadowName}/delete";
if (request.shadowName == null) {
CompletableFuture<Integer> result = new CompletableFuture<Integer>();
result.completeExceptionally(new MqttException("DeleteNamedShadowRequest must have a non-null shadowName"));
return result;
}
topic = topic.replace("{shadowName}", request.shadowName);
if (request.thingName == null) {
CompletableFuture<Integer> result = new CompletableFuture<Integer>();
result.completeExceptionally(new MqttException("DeleteNamedShadowRequest must have a non-null thingName"));
return result;
}
topic = topic.replace("{thingName}", request.thingName);
String payloadJson = gson.toJson(request);
MqttMessage message = new MqttMessage(topic, payloadJson.getBytes(StandardCharsets.UTF_8));
return connection.publish(message, qos, false);
}
Aggregations