Search in sources :

Example 56 with BlockingConnection

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

the class MQTTTest method testUniqueMessageIds.

@Test(timeout = 60 * 1000)
public void testUniqueMessageIds() throws Exception {
    MQTT mqtt = createMQTTConnection();
    mqtt.setClientId("foo");
    mqtt.setKeepAlive((short) 2);
    mqtt.setCleanSession(true);
    final List<PUBLISH> publishList = new ArrayList<>();
    mqtt.setTracer(new Tracer() {

        @Override
        public void onReceive(MQTTFrame frame) {
            LOG.info("Client received:\n" + frame);
            if (frame.messageType() == PUBLISH.TYPE) {
                PUBLISH publish = new PUBLISH();
                try {
                    publish.decode(frame);
                } catch (ProtocolException e) {
                    fail("Error decoding publish " + e.getMessage());
                }
                publishList.add(publish);
            }
        }

        @Override
        public void onSend(MQTTFrame frame) {
            LOG.info("Client sent:\n" + frame);
        }
    });
    final BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    // create overlapping subscriptions with different QoSs
    QoS[] qoss = { QoS.AT_MOST_ONCE, QoS.AT_LEAST_ONCE, QoS.EXACTLY_ONCE };
    final String TOPIC = "TopicA/";
    // publish retained message
    connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, true);
    String[] subs = { TOPIC, "TopicA/#", "TopicA/+" };
    for (int i = 0; i < qoss.length; i++) {
        connection.subscribe(new Topic[] { new Topic(subs[i], qoss[i]) });
    }
    // publish non-retained message
    connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
    int received = 0;
    Message msg = connection.receive(5000, TimeUnit.MILLISECONDS);
    do {
        assertNotNull(msg);
        assertEquals(TOPIC, new String(msg.getPayload()));
        msg.ack();
        int waitCount = 0;
        while (publishList.size() <= received && waitCount < 10) {
            Thread.sleep(1000);
            waitCount++;
        }
        msg = connection.receive(5000, TimeUnit.MILLISECONDS);
    } while (msg != null && received++ < subs.length * 2);
    assertEquals("Unexpected number of messages", subs.length * 2, received + 1);
    // AT_MOST_ONCE
    for (int i = 0; i < publishList.size(); i++) {
        for (int j = i + 1; j < publishList.size(); j++) {
            final PUBLISH publish1 = publishList.get(i);
            final PUBLISH publish2 = publishList.get(j);
            boolean qos0 = false;
            if (publish1.qos() == QoS.AT_MOST_ONCE) {
                qos0 = true;
                assertEquals(0, publish1.messageId());
            }
            if (publish2.qos() == QoS.AT_MOST_ONCE) {
                qos0 = true;
                assertEquals(0, publish2.messageId());
            }
            if (!qos0) {
                assertNotEquals(publish1.messageId(), publish2.messageId());
            }
        }
    }
    connection.unsubscribe(subs);
    connection.disconnect();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) ProtocolException(java.net.ProtocolException) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Message(org.fusesource.mqtt.client.Message) BytesMessage(javax.jms.BytesMessage) Tracer(org.fusesource.mqtt.client.Tracer) ArrayList(java.util.ArrayList) PUBLISH(org.fusesource.mqtt.codec.PUBLISH) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) MQTTFrame(org.fusesource.mqtt.codec.MQTTFrame) QoS(org.fusesource.mqtt.client.QoS) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 57 with BlockingConnection

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

the class MQTTSecurityTest method testConnection.

@Test(timeout = 30000)
public void testConnection() throws Exception {
    for (String version : Arrays.asList("3.1", "3.1.1")) {
        BlockingConnection connection = null;
        try {
            MQTT mqtt = createMQTTConnection("test-" + version, true);
            mqtt.setUserName(fullUser);
            mqtt.setPassword(fullPass);
            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) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Test(org.junit.Test)

Example 58 with BlockingConnection

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

the class MqttClusterWildcardTest method loadBalanceRequests.

@Test
public void loadBalanceRequests() throws Exception {
    final String TOPIC = "test/+/some/#";
    WildcardConfiguration wildcardConfiguration = new WildcardConfiguration();
    wildcardConfiguration.setAnyWords('#');
    wildcardConfiguration.setDelimiter('/');
    wildcardConfiguration.setRoutingEnabled(true);
    wildcardConfiguration.setSingleWord('+');
    setupServer(0, false, isNetty());
    servers[0].getConfiguration().setWildCardConfiguration(wildcardConfiguration);
    setupServer(1, false, isNetty());
    servers[1].getConfiguration().setWildCardConfiguration(wildcardConfiguration);
    setupClusterConnection("cluster0", "", MessageLoadBalancingType.ON_DEMAND, 1, isNetty(), 0, 1);
    setupClusterConnection("cluster1", "", MessageLoadBalancingType.ON_DEMAND, 1, isNetty(), 1, 0);
    startServers(0, 1);
    BlockingConnection connection1 = null;
    BlockingConnection connection2 = null;
    try {
        connection1 = retrieveMQTTConnection("tcp://localhost:61616");
        connection2 = retrieveMQTTConnection("tcp://localhost:61617");
        // Subscribe to topics
        Topic[] topics = { new Topic(TOPIC, QoS.AT_MOST_ONCE) };
        connection1.subscribe(topics);
        connection2.subscribe(topics);
        waitForBindings(0, TOPIC, 1, 1, true);
        waitForBindings(1, TOPIC, 1, 1, true);
        waitForBindings(0, TOPIC, 1, 1, false);
        waitForBindings(1, TOPIC, 1, 1, false);
        // 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);
        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);
        assertEquals(payload1, new String(message1.getPayload()));
        assertEquals(payload2, new String(message2.getPayload()));
        assertEquals(payload3, new String(message3.getPayload()));
        assertEquals(payload1, new String(message4.getPayload()));
        assertEquals(payload2, new String(message5.getPayload()));
        assertEquals(payload3, new String(message6.getPayload()));
    } finally {
        String[] topics = new String[] { TOPIC };
        if (connection1 != null) {
            connection1.unsubscribe(topics);
            connection1.disconnect();
        }
        if (connection2 != null) {
            connection2.unsubscribe(topics);
            connection2.disconnect();
        }
    }
}
Also used : Message(org.fusesource.mqtt.client.Message) WildcardConfiguration(org.apache.activemq.artemis.core.config.WildcardConfiguration) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 59 with BlockingConnection

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

the class MqttClusterWildcardTest method wildcardsWithBroker1Disconnected.

@Test
public void wildcardsWithBroker1Disconnected() throws Exception {
    BlockingConnection connection1 = null;
    BlockingConnection connection2 = null;
    BlockingConnection connection3 = null;
    final String TOPIC = "test/+/some/#";
    try {
        WildcardConfiguration wildcardConfiguration = new WildcardConfiguration();
        wildcardConfiguration.setAnyWords('#');
        wildcardConfiguration.setDelimiter('/');
        wildcardConfiguration.setRoutingEnabled(true);
        wildcardConfiguration.setSingleWord('+');
        setupServer(0, false, isNetty());
        servers[0].getConfiguration().setWildCardConfiguration(wildcardConfiguration);
        setupClusterConnection("cluster0", "", MessageLoadBalancingType.ON_DEMAND, 1, isNetty(), 0, 1);
        startServers(0);
        connection1 = retrieveMQTTConnection("tcp://localhost:61616");
        // Subscribe to topics
        Topic[] topics = { new Topic(TOPIC, QoS.AT_MOST_ONCE) };
        connection1.subscribe(topics);
        waitForBindings(0, TOPIC, 1, 1, true);
        waitForBindings(0, TOPIC, 0, 0, false);
        // 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);
        Message message1 = connection1.receive(5, TimeUnit.SECONDS);
        setupServer(1, false, isNetty());
        servers[1].getConfiguration().setWildCardConfiguration(wildcardConfiguration);
        setupClusterConnection("cluster1", "", MessageLoadBalancingType.ON_DEMAND, 1, isNetty(), 1, 0);
        startServers(1);
        connection2 = retrieveMQTTConnection("tcp://localhost:61617");
        connection3 = retrieveMQTTConnection("tcp://localhost:61617");
        connection2.subscribe(topics);
        connection3.subscribe(new Topic[] { new Topic("teste/1/some/1", QoS.AT_MOST_ONCE) });
        waitForBindings(1, TOPIC, 1, 1, false);
        waitForBindings(1, TOPIC, 1, 1, true);
        waitForBindings(0, TOPIC, 1, 1, true);
        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);
        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);
        assertEquals(payload1, new String(message1.getPayload()));
        assertEquals(payload2, new String(message2.getPayload()));
        assertEquals(payload3, new String(message3.getPayload()));
        assertEquals(payload1, new String(message4.getPayload()));
        assertEquals(payload2, new String(message5.getPayload()));
        assertEquals(payload3, new String(message6.getPayload()));
    } finally {
        String[] topics = new String[] { TOPIC };
        if (connection1 != null) {
            connection1.unsubscribe(topics);
            connection1.disconnect();
        }
        if (connection2 != null) {
            connection2.unsubscribe(topics);
            connection2.disconnect();
        }
        if (connection3 != null) {
            connection3.unsubscribe(new String[] { "teste/1/some/1" });
            connection3.disconnect();
        }
    }
}
Also used : Message(org.fusesource.mqtt.client.Message) WildcardConfiguration(org.apache.activemq.artemis.core.config.WildcardConfiguration) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 60 with BlockingConnection

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

the class MqttClusterRemoteSubscribeTest method unsubscribeRemoteQueueMultipleSubscriptions.

@Test
public void unsubscribeRemoteQueueMultipleSubscriptions() throws Exception {
    final String TOPIC = "test/1/some/la";
    final String TOPIC2 = "sample";
    setupServers(TOPIC);
    startServers(0, 1);
    BlockingConnection connection1 = null;
    BlockingConnection connection2 = null;
    try {
        connection1 = retrieveMQTTConnection("tcp://localhost:61616");
        connection2 = retrieveMQTTConnection("tcp://localhost:61617");
        // Subscribe to topics
        connection1.subscribe(new Topic[] { new Topic(TOPIC, QoS.AT_MOST_ONCE) });
        connection2.subscribe(new Topic[] { new Topic(TOPIC, QoS.AT_MOST_ONCE), new Topic(TOPIC2, QoS.AT_MOST_ONCE) });
        waitForBindings(0, TOPIC, 1, 1, true);
        waitForBindings(1, TOPIC, 1, 1, true);
        waitForBindings(0, TOPIC, 1, 1, false);
        waitForBindings(1, TOPIC, 1, 1, false);
        // Publish Messages
        String payload1 = "This is message 1";
        String payload2 = "This is message 2";
        String payload3 = "This is message 3";
        String payload4 = "This is message 4";
        connection1.publish(TOPIC, payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
        connection1.publish(TOPIC, payload2.getBytes(), QoS.AT_MOST_ONCE, false);
        connection1.publish(TOPIC, payload3.getBytes(), QoS.AT_MOST_ONCE, false);
        connection1.publish(TOPIC2, payload4.getBytes(), QoS.AT_MOST_ONCE, false);
        Message message1 = connection1.receive(5, TimeUnit.SECONDS);
        message1.ack();
        Message message2 = connection2.receive(5, TimeUnit.SECONDS);
        message2.ack();
        Message message3 = connection1.receive(5, TimeUnit.SECONDS);
        message3.ack();
        Message message4 = connection2.receive(5, TimeUnit.SECONDS);
        message4.ack();
        assertEquals(payload1, new String(message1.getPayload()));
        assertEquals(payload2, new String(message2.getPayload()));
        assertEquals(payload3, new String(message3.getPayload()));
        assertEquals(payload4, new String(message4.getPayload()));
        connection2.unsubscribe(new String[] { TOPIC });
        connection1.publish(TOPIC, payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
        connection1.publish(TOPIC, payload2.getBytes(), QoS.AT_MOST_ONCE, false);
        connection1.publish(TOPIC, payload3.getBytes(), QoS.AT_MOST_ONCE, false);
        connection1.publish(TOPIC2, payload4.getBytes(), QoS.AT_MOST_ONCE, false);
        Message message11 = connection1.receive(5, TimeUnit.SECONDS);
        message11.ack();
        Message message21 = connection1.receive(5, TimeUnit.SECONDS);
        message21.ack();
        Message message31 = connection1.receive(5, TimeUnit.SECONDS);
        message31.ack();
        Message message41 = connection2.receive(5, TimeUnit.SECONDS);
        message41.ack();
        String message11String = new String(message31.getPayload());
        String message21String = new String(message21.getPayload());
        String message31String = new String(message11.getPayload());
        assertTrue(payload1.equals(message11String) || payload1.equals(message21String) || payload1.equals(message31String));
        assertTrue(payload2.equals(message11String) || payload2.equals(message21String) || payload2.equals(message31String));
        assertTrue(payload3.equals(message11String) || payload3.equals(message21String) || payload3.equals(message31String));
        assertEquals(payload4, new String(message41.getPayload()));
    } finally {
        if (connection1 != null) {
            connection1.unsubscribe(new String[] { TOPIC });
            connection1.disconnect();
        }
        if (connection2 != null) {
            connection2.unsubscribe(new String[] { TOPIC, TOPIC2 });
            connection2.disconnect();
        }
    }
}
Also used : Message(org.fusesource.mqtt.client.Message) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) 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