Search in sources :

Example 51 with BlockingConnection

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();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Message(org.fusesource.mqtt.client.Message) BytesMessage(javax.jms.BytesMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Topic(org.fusesource.mqtt.client.Topic) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Test(org.junit.Test)

Example 52 with BlockingConnection

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();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Test(org.junit.Test)

Example 53 with BlockingConnection

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();
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Message(org.fusesource.mqtt.client.Message) BytesMessage(javax.jms.BytesMessage) Tracer(org.fusesource.mqtt.client.Tracer) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) MQTTFrame(org.fusesource.mqtt.codec.MQTTFrame) Test(org.junit.Test)

Example 54 with BlockingConnection

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();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Test(org.junit.Test)

Example 55 with BlockingConnection

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();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) AtomicReference(java.util.concurrent.atomic.AtomicReference) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Test(org.junit.Test)

Aggregations

BlockingConnection (org.fusesource.mqtt.client.BlockingConnection)71 MQTT (org.fusesource.mqtt.client.MQTT)61 Test (org.junit.Test)58 Topic (org.fusesource.mqtt.client.Topic)48 Message (org.fusesource.mqtt.client.Message)36 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)26 BytesMessage (javax.jms.BytesMessage)18 AmqpMessage (org.apache.activemq.transport.amqp.client.AmqpMessage)18 ProtocolException (java.net.ProtocolException)6 Tracer (org.fusesource.mqtt.client.Tracer)6 MQTTFrame (org.fusesource.mqtt.codec.MQTTFrame)6 Ignore (org.junit.Ignore)6 PUBLISH (org.fusesource.mqtt.codec.PUBLISH)4 ArrayList (java.util.ArrayList)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)3 AddressInfo (org.apache.activemq.artemis.core.server.impl.AddressInfo)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 EOFException (java.io.EOFException)2 URI (java.net.URI)2