use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MQTTTest method testNoMessageReceivedAfterUnsubscribeMQTT.
@Test(timeout = 60 * 1000)
public void testNoMessageReceivedAfterUnsubscribeMQTT() throws Exception {
Topic[] topics = { new Topic("TopicA", QoS.EXACTLY_ONCE) };
MQTT mqttPub = createMQTTConnection("MQTTPub-Client", true);
// mqttPub.setVersion("3.1.1");
MQTT mqttSub = createMQTTConnection("MQTTSub-Client", false);
// mqttSub.setVersion("3.1.1");
BlockingConnection connectionPub = mqttPub.blockingConnection();
connectionPub.connect();
BlockingConnection connectionSub = mqttSub.blockingConnection();
connectionSub.connect();
connectionSub.subscribe(topics);
connectionSub.disconnect();
for (int i = 0; i < 5; i++) {
String payload = "Message " + i;
connectionPub.publish(topics[0].name().toString(), payload.getBytes(), QoS.EXACTLY_ONCE, false);
}
connectionSub = mqttSub.blockingConnection();
connectionSub.connect();
int received = 0;
for (int i = 0; i < 5; ++i) {
Message message = connectionSub.receive(5, TimeUnit.SECONDS);
assertNotNull("Missing message " + i, message);
LOG.info("Message is " + new String(message.getPayload()));
received++;
message.ack();
}
assertEquals(5, received);
// unsubscribe from topic
connectionSub.unsubscribe(new String[] { "TopicA" });
// send more messages
for (int i = 0; i < 5; i++) {
String payload = "Message " + i;
connectionPub.publish(topics[0].name().toString(), payload.getBytes(), QoS.EXACTLY_ONCE, false);
}
// these should not be received
assertNull(connectionSub.receive(5, TimeUnit.SECONDS));
connectionSub.disconnect();
connectionPub.disconnect();
}
use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MQTTTest method testTurnOffInactivityMonitor.
@Test(timeout = 60 * 1000)
public void testTurnOffInactivityMonitor() throws Exception {
stopBroker();
protocolConfig = "transport.useInactivityMonitor=false";
startBroker();
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("foo3");
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
connection.disconnect();
}
use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MQTTTest method testMQTTRetainQoS.
@Test(timeout = 60 * 1000)
public void testMQTTRetainQoS() throws Exception {
String[] topics = { "AT_MOST_ONCE", "AT_LEAST_ONCE", "EXACTLY_ONCE" };
for (int i = 0; i < topics.length; i++) {
final String topic = topics[i];
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("foo");
mqtt.setKeepAlive((short) 2);
final int[] actualQoS = { -1 };
mqtt.setTracer(new Tracer() {
@Override
public void onReceive(MQTTFrame frame) {
// validate the QoS
if (frame.messageType() == PUBLISH.TYPE) {
actualQoS[0] = frame.qos().ordinal();
}
}
});
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
connection.publish(topic, topic.getBytes(), QoS.EXACTLY_ONCE, true);
connection.subscribe(new Topic[] { new Topic(topic, QoS.valueOf(topic)) });
final Message msg = connection.receive(5000, TimeUnit.MILLISECONDS);
assertNotNull(msg);
assertEquals(topic, new String(msg.getPayload()));
int waitCount = 0;
while (actualQoS[0] == -1 && waitCount < 10) {
Thread.sleep(1000);
waitCount++;
}
assertEquals(i, actualQoS[0]);
msg.ack();
connection.unsubscribe(new String[] { topic });
connection.disconnect();
}
}
use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MQTTTest method testPingKeepsInactivityMonitorAlive.
@Test(timeout = 60 * 1000)
public void testPingKeepsInactivityMonitorAlive() throws Exception {
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("foo");
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
connection.disconnect();
}
use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MQTTTest method testRepeatedLinkStealing.
@Test(timeout = 60 * 1000)
public void testRepeatedLinkStealing() throws Exception {
final String clientId = "duplicateClient";
final AtomicReference<BlockingConnection> oldConnection = new AtomicReference<>();
final String TOPICA = "TopicA";
for (int i = 1; i <= 10; ++i) {
LOG.info("Creating MQTT Connection {}", i);
MQTT mqtt = createMQTTConnection(clientId, false);
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
connection.publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true);
assertTrue("Client connect failed for attempt: " + i, Wait.waitFor(() -> connection.isConnected(), 3000, 200));
if (oldConnection.get() != null) {
assertTrue("Old client still connected on attempt: " + i, Wait.waitFor(() -> !oldConnection.get().isConnected(), 3000, 200));
}
oldConnection.set(connection);
}
oldConnection.get().publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true);
oldConnection.get().disconnect();
}
Aggregations