Search in sources :

Example 46 with BlockingConnection

use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.

the class MQTTTest method testAnycastAddressWorksWithMQTT.

@Test(timeout = 60 * 1000)
public void testAnycastAddressWorksWithMQTT() throws Exception {
    String anycastAddress = "foo/bar";
    getServer().addAddressInfo(new AddressInfo(SimpleString.toSimpleString("foo.bar"), RoutingType.ANYCAST));
    String clientId = "testMqtt";
    Topic[] mqttSubscription = new Topic[] { new Topic(anycastAddress, QoS.AT_LEAST_ONCE) };
    MQTT mqtt = createMQTTConnection();
    mqtt.setClientId(clientId);
    BlockingConnection connection1 = mqtt.blockingConnection();
    connection1.connect();
    connection1.subscribe(mqttSubscription);
    MQTT mqtt2 = createMQTTConnection();
    mqtt2.setClientId(clientId + "2");
    BlockingConnection connection2 = mqtt2.blockingConnection();
    connection2.connect();
    connection2.subscribe(mqttSubscription);
    String message1 = "TestMessage1";
    String message2 = "TestMessage2";
    connection1.publish(anycastAddress, message1.getBytes(), QoS.AT_LEAST_ONCE, false);
    connection2.publish(anycastAddress, message2.getBytes(), QoS.AT_LEAST_ONCE, false);
    assertNotNull(connection1.receive(1000, TimeUnit.MILLISECONDS));
    assertNull(connection1.receive(1000, TimeUnit.MILLISECONDS));
    assertNotNull(connection2.receive(1000, TimeUnit.MILLISECONDS));
    assertNull(connection2.receive(1000, TimeUnit.MILLISECONDS));
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Topic(org.fusesource.mqtt.client.Topic) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo) Test(org.junit.Test)

Example 47 with BlockingConnection

use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.

the class MQTTTest method testClientConnectionFailure.

@Test(timeout = 60 * 1000)
public void testClientConnectionFailure() throws Exception {
    MQTT mqtt = createMQTTConnection("reconnect", false);
    mqtt.setKeepAlive((short) 1);
    final BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    Wait.waitFor(() -> connection.isConnected());
    final String TOPIC = "TopicA";
    final byte[] qos = connection.subscribe(new Topic[] { new Topic(TOPIC, QoS.EXACTLY_ONCE) });
    assertEquals(QoS.EXACTLY_ONCE.ordinal(), qos[0]);
    connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
    // kill transport
    connection.kill();
    // FIXME Wait for the previous connection to timeout.  This is not required in ActiveMQ.  Needs investigating.
    Thread.sleep(10000);
    final BlockingConnection newConnection = mqtt.blockingConnection();
    newConnection.connect();
    Wait.waitFor(() -> newConnection.isConnected());
    assertEquals(QoS.EXACTLY_ONCE.ordinal(), qos[0]);
    Message msg = newConnection.receive(1000, TimeUnit.MILLISECONDS);
    assertNotNull(msg);
    assertEquals(TOPIC, new String(msg.getPayload()));
    msg.ack();
    newConnection.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) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 48 with BlockingConnection

use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.

the class MQTTTest method testConnectWithLargePassword.

@Test
public void testConnectWithLargePassword() throws Exception {
    for (String version : Arrays.asList("3.1", "3.1.1")) {
        String longString = new String(new char[65535]);
        BlockingConnection connection = null;
        try {
            MQTT mqtt = createMQTTConnection("test-" + version, true);
            mqtt.setUserName(longString);
            mqtt.setPassword(longString);
            mqtt.setConnectAttemptsMax(1);
            mqtt.setVersion(version);
            connection = mqtt.blockingConnection();
            connection.connect();
            BlockingConnection finalConnection = connection;
            assertTrue("Should be connected", Wait.waitFor(() -> finalConnection.isConnected(), 5000, 100));
        } finally {
            if (connection != null && connection.isConnected())
                connection.disconnect();
        }
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Test(org.junit.Test)

Example 49 with BlockingConnection

use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.

the class MQTTTest method testReuseConnection.

@Test(timeout = 60 * 1000)
public void testReuseConnection() throws Exception {
    MQTT mqtt = createMQTTConnection();
    mqtt.setClientId("Test-Client");
    {
        BlockingConnection connection = mqtt.blockingConnection();
        connection.connect();
        connection.disconnect();
        Thread.sleep(1000);
    }
    {
        BlockingConnection connection = mqtt.blockingConnection();
        connection.connect();
        connection.disconnect();
        Thread.sleep(1000);
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Test(org.junit.Test)

Example 50 with BlockingConnection

use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.

the class MQTTTest method testRetainedMessageOnVirtualTopics.

@Ignore
@Test(timeout = 120 * 1000)
public void testRetainedMessageOnVirtualTopics() throws Exception {
    MQTT mqtt = createMQTTConnection();
    mqtt.setKeepAlive((short) 60);
    final String RETAIN = "RETAIN";
    final String TOPICA = "VirtualTopic/TopicA";
    final String[] clientIds = { null, "foo", "durable" };
    for (String clientId : clientIds) {
        LOG.info("Testing now with Client ID: {}", clientId);
        mqtt.setClientId(clientId);
        mqtt.setCleanSession(!"durable".equals(clientId));
        BlockingConnection connection = mqtt.blockingConnection();
        connection.connect();
        // set retained message and check
        connection.publish(TOPICA, RETAIN.getBytes(), QoS.EXACTLY_ONCE, true);
        connection.subscribe(new Topic[] { new Topic(TOPICA, QoS.AT_LEAST_ONCE) });
        Message msg = connection.receive(5000, TimeUnit.MILLISECONDS);
        assertNotNull("No retained message for " + clientId, msg);
        assertEquals(RETAIN, new String(msg.getPayload()));
        msg.ack();
        assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
        // test duplicate subscription
        connection.subscribe(new Topic[] { new Topic(TOPICA, QoS.AT_LEAST_ONCE) });
        msg = connection.receive(15000, TimeUnit.MILLISECONDS);
        assertNotNull("No retained message on duplicate subscription for " + clientId, msg);
        assertEquals(RETAIN, new String(msg.getPayload()));
        msg.ack();
        assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
        connection.unsubscribe(new String[] { TOPICA });
        // clear retained message and check that we don't receive it
        connection.publish(TOPICA, "".getBytes(), QoS.AT_MOST_ONCE, true);
        connection.subscribe(new Topic[] { new Topic(TOPICA, QoS.AT_LEAST_ONCE) });
        msg = connection.receive(500, TimeUnit.MILLISECONDS);
        assertNull("Retained message not cleared for " + clientId, msg);
        connection.unsubscribe(new String[] { TOPICA });
        // set retained message again and check
        connection.publish(TOPICA, RETAIN.getBytes(), QoS.EXACTLY_ONCE, true);
        connection.subscribe(new Topic[] { new Topic(TOPICA, QoS.AT_LEAST_ONCE) });
        msg = connection.receive(5000, TimeUnit.MILLISECONDS);
        assertNotNull("No reset retained message for " + clientId, msg);
        assertEquals(RETAIN, new String(msg.getPayload()));
        msg.ack();
        assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
        // re-connect and check
        connection.disconnect();
        connection = mqtt.blockingConnection();
        connection.connect();
        connection.subscribe(new Topic[] { new Topic(TOPICA, QoS.AT_LEAST_ONCE) });
        msg = connection.receive(5000, TimeUnit.MILLISECONDS);
        assertNotNull("No reset retained message for " + clientId, msg);
        assertEquals(RETAIN, new String(msg.getPayload()));
        msg.ack();
        assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
        LOG.info("Test now unsubscribing from: {} for the last time", TOPICA);
        connection.unsubscribe(new String[] { TOPICA });
        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) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) Ignore(org.junit.Ignore) 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