use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class ClusteredQueueMQTTExample method main.
public static void main(final String[] args) throws Exception {
// Create a new MQTT connection to the broker. We are not setting the client ID. The broker will pick one for us.
System.out.println("Connecting to Artemis using MQTT");
BlockingConnection connection1 = retrieveMQTTConnection("tcp://localhost:1883");
System.out.println("Connected to Artemis 1");
BlockingConnection connection2 = retrieveMQTTConnection("tcp://localhost:1884");
System.out.println("Connected to Artemis 2");
// Subscribe to topics
Topic[] topics = { new Topic("test/+/some/#", QoS.AT_MOST_ONCE) };
connection1.subscribe(topics);
connection2.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";
connection1.publish("test/1/some/la", payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
connection1.publish("test/1/some/la", payload2.getBytes(), QoS.AT_MOST_ONCE, false);
connection1.publish("test/1/some/la", payload3.getBytes(), QoS.AT_MOST_ONCE, false);
System.out.println("Sent messages.");
Message message1 = connection1.receive(5, TimeUnit.SECONDS);
Message message2 = connection1.receive(5, TimeUnit.SECONDS);
Message message3 = connection1.receive(5, TimeUnit.SECONDS);
Message message4 = connection2.receive(5, TimeUnit.SECONDS);
Message message5 = connection2.receive(5, TimeUnit.SECONDS);
Message message6 = connection2.receive(5, TimeUnit.SECONDS);
System.out.println("Received messages.");
System.out.println("Broker 1: " + new String(message1.getPayload()));
System.out.println("Broker 1: " + new String(message2.getPayload()));
System.out.println("Broker 1: " + new String(message3.getPayload()));
System.out.println("Broker 2: " + new String(message4.getPayload()));
System.out.println("Broker 2: " + new String(message5.getPayload()));
System.out.println("Broker 2: " + new String(message6.getPayload()));
}
use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MQTTTest method testDefaultKeepAliveWhenClientSpecifiesZero.
@Test(timeout = 30 * 1000)
public void testDefaultKeepAliveWhenClientSpecifiesZero() throws Exception {
stopBroker();
protocolConfig = "transport.defaultKeepAlive=2000";
startBroker();
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("foo");
mqtt.setKeepAlive((short) 0);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
}
use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MQTTTest method testSubscribeMultipleTopics.
@Test(timeout = 30 * 10000)
public void testSubscribeMultipleTopics() throws Exception {
byte[] payload = new byte[1024 * 32];
for (int i = 0; i < payload.length; i++) {
payload[i] = '2';
}
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("MQTT-Client");
mqtt.setCleanSession(false);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
Topic[] topics = { new Topic("Topic/A", QoS.EXACTLY_ONCE), new Topic("Topic/B", QoS.EXACTLY_ONCE) };
Topic[] wildcardTopic = { new Topic("Topic/#", QoS.AT_LEAST_ONCE) };
connection.subscribe(wildcardTopic);
for (Topic topic : topics) {
connection.publish(topic.name().toString(), payload, QoS.AT_LEAST_ONCE, false);
}
int received = 0;
for (int i = 0; i < topics.length; ++i) {
Message message = connection.receive();
assertNotNull(message);
received++;
payload = message.getPayload();
String messageContent = new String(payload);
LOG.info("Received message from topic: " + message.getTopic() + " Message content: " + messageContent);
message.ack();
}
assertEquals("Should have received " + topics.length + " messages", topics.length, received);
}
use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MQTTTest method testMQTTPathPatterns.
@Test(timeout = 2 * 60 * 1000)
public void testMQTTPathPatterns() throws Exception {
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("");
mqtt.setCleanSession(true);
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
final String RETAINED = "RETAINED";
String[] topics = { "TopicA", "/TopicA", "/", "TopicA/", "//" };
for (String topic : topics) {
// test retained message
connection.publish(topic, (RETAINED + topic).getBytes(), QoS.AT_LEAST_ONCE, true);
connection.subscribe(new Topic[] { new Topic(topic, QoS.AT_LEAST_ONCE) });
Message msg = connection.receive(5, TimeUnit.SECONDS);
assertNotNull("No message for " + topic, msg);
assertEquals(RETAINED + topic, new String(msg.getPayload()));
msg.ack();
// test non-retained message
connection.publish(topic, topic.getBytes(), QoS.AT_LEAST_ONCE, false);
msg = connection.receive(1000, TimeUnit.MILLISECONDS);
assertNotNull(msg);
assertEquals(topic, new String(msg.getPayload()));
msg.ack();
connection.unsubscribe(new String[] { topic });
}
connection.disconnect();
// test wildcard patterns with above topics
String[] wildcards = { "#", "+", "+/#", "/+", "+/", "+/+", "+/+/", "+/+/+" };
for (String wildcard : wildcards) {
final Pattern pattern = Pattern.compile(wildcard.replaceAll("/?#", "(/?.*)*").replaceAll("\\+", "[^/]*"));
connection = mqtt.blockingConnection();
connection.connect();
final byte[] qos = connection.subscribe(new Topic[] { new Topic(wildcard, QoS.AT_LEAST_ONCE) });
assertNotEquals("Subscribe failed " + wildcard, (byte) 0x80, qos[0]);
// test retained messages
Message msg = connection.receive(5, TimeUnit.SECONDS);
do {
assertNotNull("RETAINED null " + wildcard, msg);
String msgPayload = new String(msg.getPayload());
assertTrue("RETAINED prefix " + wildcard + " msg " + msgPayload, msgPayload.startsWith(RETAINED));
assertTrue("RETAINED matching " + wildcard + " " + msg.getTopic(), pattern.matcher(msg.getTopic()).matches());
msg.ack();
msg = connection.receive(5000, TimeUnit.MILLISECONDS);
} while (msg != null);
// test non-retained message
for (String topic : topics) {
connection.publish(topic, topic.getBytes(), QoS.AT_LEAST_ONCE, false);
}
msg = connection.receive(1000, TimeUnit.MILLISECONDS);
do {
assertNotNull("Non-retained Null " + wildcard, msg);
assertTrue("Non-retained matching " + wildcard + " " + msg.getTopic(), pattern.matcher(msg.getTopic()).matches());
msg.ack();
msg = connection.receive(1000, TimeUnit.MILLISECONDS);
} while (msg != null);
connection.unsubscribe(new String[] { wildcard });
connection.disconnect();
}
}
use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MQTTTest method testWillMessageIsRetained.
@Test(timeout = 60 * 1000)
public void testWillMessageIsRetained() throws Exception {
getServer().createQueue(SimpleString.toSimpleString("will"), RoutingType.MULTICAST, SimpleString.toSimpleString("will"), null, true, false);
MQTT mqtt = createMQTTConnection("1", false);
mqtt.setKeepAlive((short) 1);
mqtt.setWillMessage("test message");
mqtt.setWillTopic("will");
mqtt.setWillQos(QoS.AT_LEAST_ONCE);
mqtt.setWillRetain(true);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
Wait.waitFor(() -> connection.isConnected());
// kill transport
connection.kill();
Thread.sleep(10000);
MQTT mqtt2 = createMQTTConnection("2", false);
BlockingConnection connection2 = mqtt2.blockingConnection();
connection2.connect();
connection2.subscribe(new Topic[] { new Topic("will", QoS.AT_LEAST_ONCE) });
Message m = connection2.receive(1000, TimeUnit.MILLISECONDS);
assertNotNull(m);
m.ack();
assertEquals("test message", new String(m.getPayload()));
}
Aggregations