From 6a9e4c05e8a99f472f175f1e2f1e4c8072e968d0 Mon Sep 17 00:00:00 2001
From: Antoine Toulme Subscriptions in MQTT are realised by subscribing to a particular Topic. Each Topic has an address
and a quality of service level (QoS level). Subscriptions also support wildcards. In the code below we
- subscribe to a Topic with address "mqtt/example/publish" and also a wildcard address "mqtt/#" which will
- match anything starting with "mqtt/".
- Topic[] topics = { new Topic("mqtt/example/publish", QoS.AT_LEAST_ONCE), new Topic("mqtt/#", QoS.EXACTLY_ONCE) }; + Topic[] topics = { new Topic("mqtt/example/publish", QoS.AT_LEAST_ONCE), new Topic("test/#", QoS.EXACTLY_ONCE), new Topic("foo/+/bar", QoS.AT_LEAST_ONCE) }; connection.subscribe(topics);
There is no type system in MQTT, messages simply consist of a number of bytes. Below we send two messages with - UTF8 encoded strings (as a byte array). Notice the second message is sent to "mqtt/test" which should match - our wildcard subscription we defined previously.
+There is no type system in MQTT, messages simply consist of a number of bytes. Below we send three messages with + UTF8 encoded strings (as a byte array). Notice the second message is sent to "test/test" which should match + the first wildcard subscription we defined previously. The third message is sent to "foo/1/bar", which matches the second wildcard subscription.
String payload1 = "This is message 1"; String payload2 = "This is message 2"; + String payload3 = "This is message 3"; connection.publish("mqtt/example/publish", payload1.getBytes(), QoS.AT_LEAST_ONCE, false); connection.publish("mqtt/test", payload2.getBytes(), QoS.AT_MOST_ONCE, false); + connection.publish("foo/1/bar", payload3.getBytes(), QoS.AT_MOST_ONCE, false);
Message message1 = connection.receive(5, TimeUnit.SECONDS); Message message2 = connection.receive(5, TimeUnit.SECONDS); + Message message3 = connection.receive(5, TimeUnit.SECONDS); System.out.println(new String(message1.getPayload())); System.out.println(new String(message2.getPayload())); + System.out.println(new String(message3.getPayload()));diff --git a/examples/protocols/mqtt/basic-pubsub/src/main/java/org/apache/activemq/artemis/mqtt/example/MQTTBasicPubSubExample.java b/examples/protocols/mqtt/basic-pubsub/src/main/java/org/apache/activemq/artemis/mqtt/example/MQTTBasicPubSubExample.java index 93fb9468b6..dd64731e10 100644 --- a/examples/protocols/mqtt/basic-pubsub/src/main/java/org/apache/activemq/artemis/mqtt/example/MQTTBasicPubSubExample.java +++ b/examples/protocols/mqtt/basic-pubsub/src/main/java/org/apache/activemq/artemis/mqtt/example/MQTTBasicPubSubExample.java @@ -39,23 +39,27 @@ public class MQTTBasicPubSubExample { System.out.println("Connected to Artemis"); // Subscribe to topics - Topic[] topics = {new Topic("mqtt/example/publish", QoS.AT_LEAST_ONCE), new Topic("mqtt/#", QoS.EXACTLY_ONCE)}; + Topic[] topics = {new Topic("mqtt/example/publish", QoS.AT_LEAST_ONCE), new Topic("test/#", QoS.EXACTLY_ONCE), new Topic("foo/+/bar", QoS.AT_LEAST_ONCE)}; connection.subscribe(topics); System.out.println("Subscribed to topics."); // Publish Messages String payload1 = "This is message 1"; String payload2 = "This is message 2"; + String payload3 = "This is message 3"; connection.publish("mqtt/example/publish", payload1.getBytes(), QoS.AT_LEAST_ONCE, false); - connection.publish("mqtt/test", payload2.getBytes(), QoS.AT_MOST_ONCE, false); + connection.publish("test/test", payload2.getBytes(), QoS.AT_MOST_ONCE, false); + connection.publish("foo/1/bar", payload3.getBytes(), QoS.AT_MOST_ONCE, false); System.out.println("Sent messages."); Message message1 = connection.receive(5, TimeUnit.SECONDS); Message message2 = connection.receive(5, TimeUnit.SECONDS); + Message message3 = connection.receive(5, TimeUnit.SECONDS); System.out.println("Received messages."); System.out.println(new String(message1.getPayload())); System.out.println(new String(message2.getPayload())); + System.out.println(new String(message3.getPayload())); } }