Search in sources :

Example 31 with Topic

use of org.fusesource.mqtt.client.Topic 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);
}
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 32 with Topic

use of org.fusesource.mqtt.client.Topic 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();
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) Pattern(java.util.regex.Pattern) 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 33 with Topic

use of org.fusesource.mqtt.client.Topic 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()));
}
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 34 with Topic

use of org.fusesource.mqtt.client.Topic 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 35 with Topic

use of org.fusesource.mqtt.client.Topic 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)

Aggregations

Topic (org.fusesource.mqtt.client.Topic)52 BlockingConnection (org.fusesource.mqtt.client.BlockingConnection)48 Test (org.junit.Test)41 MQTT (org.fusesource.mqtt.client.MQTT)39 Message (org.fusesource.mqtt.client.Message)36 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)21 AmqpMessage (org.apache.activemq.transport.amqp.client.AmqpMessage)18 BytesMessage (javax.jms.BytesMessage)17 Tracer (org.fusesource.mqtt.client.Tracer)6 MQTTFrame (org.fusesource.mqtt.codec.MQTTFrame)6 ProtocolException (java.net.ProtocolException)5 QoS (org.fusesource.mqtt.client.QoS)4 PUBLISH (org.fusesource.mqtt.codec.PUBLISH)4 Ignore (org.junit.Ignore)4 ArrayList (java.util.ArrayList)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AddressInfo (org.apache.activemq.artemis.core.server.impl.AddressInfo)3 URI (java.net.URI)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 WildcardConfiguration (org.apache.activemq.artemis.core.config.WildcardConfiguration)2