Search in sources :

Example 26 with BlockingConnection

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

the class MQTTTest method testDoubleBroker.

@Test
public void testDoubleBroker() throws Exception {
    /*
       * Start two embedded server instances for MQTT and connect to them
       * with the same MQTT client id. As those are two different instances
       * connecting to them with the same client ID must succeed.
       */
    final int port1 = 1884;
    final int port2 = 1885;
    final Configuration cfg1 = createDefaultConfig(1, false);
    cfg1.addAcceptorConfiguration("mqtt1", "tcp://localhost:" + port1 + "?protocols=MQTT");
    final Configuration cfg2 = createDefaultConfig(2, false);
    cfg2.addAcceptorConfiguration("mqtt2", "tcp://localhost:" + port2 + "?protocols=MQTT");
    final ActiveMQServer server1 = createServer(cfg1);
    server1.start();
    final ActiveMQServer server2 = createServer(cfg2);
    server2.start();
    final String clientId = "client1";
    final MQTT mqtt1 = createMQTTConnection(clientId, true);
    final MQTT mqtt2 = createMQTTConnection(clientId, true);
    mqtt1.setHost("localhost", port1);
    mqtt2.setHost("localhost", port2);
    final BlockingConnection connection1 = mqtt1.blockingConnection();
    final BlockingConnection connection2 = mqtt2.blockingConnection();
    try {
        connection1.connect();
        connection2.connect();
    } catch (Exception e) {
        fail("Connections should have worked.");
    } finally {
        if (connection1.isConnected())
            connection1.disconnect();
        if (connection2.isConnected())
            connection2.disconnect();
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) Configuration(org.apache.activemq.artemis.core.config.Configuration) CoreAddressConfiguration(org.apache.activemq.artemis.core.config.CoreAddressConfiguration) CoreQueueConfiguration(org.apache.activemq.artemis.core.config.CoreQueueConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) EOFException(java.io.EOFException) ProtocolException(java.net.ProtocolException) Test(org.junit.Test)

Example 27 with BlockingConnection

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

the class MQTTTest method testResendMessageId.

@Test(timeout = 60 * 1000)
public void testResendMessageId() throws Exception {
    final MQTT mqtt = createMQTTConnection("resend", false);
    mqtt.setKeepAlive((short) 5);
    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);
        }
    });
    BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    final String TOPIC = "TopicA/";
    final String[] topics = new String[] { TOPIC, "TopicA/+" };
    connection.subscribe(new Topic[] { new Topic(topics[0], QoS.AT_LEAST_ONCE), new Topic(topics[1], QoS.EXACTLY_ONCE) });
    // publish non-retained message
    connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
    assertTrue(Wait.waitFor(() -> publishList.size() == 2, 5000));
    connection.disconnect();
    connection = mqtt.blockingConnection();
    connection.connect();
    assertTrue(Wait.waitFor(() -> publishList.size() == 4, 5000));
    // TODO Investigate if receiving the same ID for overlapping subscriptions is actually spec compliant.
    // In Artemis we send a new ID for every copy of the message.
    // make sure we received duplicate message ids
    // assertTrue(publishList.get(0).messageId() == publishList.get(2).messageId() || publishList.get(0).messageId() == publishList.get(3).messageId());
    // assertTrue(publishList.get(1).messageId() == publishList.get(3).messageId() || publishList.get(1).messageId() == publishList.get(2).messageId());
    // assertTrue(publishList.get(2).dup() && publishList.get(3).dup());
    connection.unsubscribe(topics);
    connection.disconnect();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) ProtocolException(java.net.ProtocolException) 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) Topic(org.fusesource.mqtt.client.Topic) MQTTFrame(org.fusesource.mqtt.codec.MQTTFrame) Test(org.junit.Test)

Example 28 with BlockingConnection

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

the class MQTTTest method testPublishDollarTopics.

@Ignore
@Test(timeout = 60 * 1000)
public // TODO Make dollar topics configurable in code base.
void testPublishDollarTopics() throws Exception {
    MQTT mqtt = createMQTTConnection();
    final String clientId = "publishDollar";
    mqtt.setClientId(clientId);
    mqtt.setKeepAlive((short) 2);
    BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    final String DOLLAR_TOPIC = "$TopicA";
    connection.subscribe(new Topic[] { new Topic(DOLLAR_TOPIC, QoS.EXACTLY_ONCE) });
    connection.publish(DOLLAR_TOPIC, DOLLAR_TOPIC.getBytes(), QoS.EXACTLY_ONCE, true);
    Message message = connection.receive(10, TimeUnit.SECONDS);
    assertNull("Publish enabled for $ Topics by default", message);
    connection.disconnect();
    stopBroker();
    protocolConfig = "transport.publishDollarTopics=true";
    startBroker();
    mqtt = createMQTTConnection();
    mqtt.setClientId(clientId);
    mqtt.setKeepAlive((short) 2);
    connection = mqtt.blockingConnection();
    connection.connect();
    connection.subscribe(new Topic[] { new Topic(DOLLAR_TOPIC, QoS.EXACTLY_ONCE) });
    connection.publish(DOLLAR_TOPIC, DOLLAR_TOPIC.getBytes(), QoS.EXACTLY_ONCE, true);
    message = connection.receive(10, TimeUnit.SECONDS);
    assertNotNull(message);
    message.ack();
    assertEquals("Message body", DOLLAR_TOPIC, new String(message.getPayload()));
    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)

Example 29 with BlockingConnection

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

the class MQTTTest method testClientDisconnectedOnMaxConsumerLimitReached.

@Test(timeout = 60 * 1000)
public void testClientDisconnectedOnMaxConsumerLimitReached() throws Exception {
    Exception peerDisconnectedException = null;
    try {
        String clientId = "test.client";
        SimpleString coreAddress = new SimpleString("foo.bar");
        Topic[] mqttSubscription = new Topic[] { new Topic("foo/bar", QoS.AT_LEAST_ONCE) };
        getServer().createQueue(coreAddress, RoutingType.MULTICAST, new SimpleString(clientId + "." + coreAddress), null, false, true, 0, false, true);
        MQTT mqtt = createMQTTConnection();
        mqtt.setClientId(clientId);
        mqtt.setKeepAlive((short) 2);
        final BlockingConnection connection = mqtt.blockingConnection();
        connection.connect();
        connection.subscribe(mqttSubscription);
    } catch (EOFException e) {
        peerDisconnectedException = e;
    }
    assertNotNull(peerDisconnectedException);
    assertTrue(peerDisconnectedException.getMessage().contains("Peer disconnected"));
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) EOFException(java.io.EOFException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Topic(org.fusesource.mqtt.client.Topic) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) EOFException(java.io.EOFException) ProtocolException(java.net.ProtocolException) Test(org.junit.Test)

Example 30 with BlockingConnection

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

the class MQTTTest method testAmbiguousRoutingWithMQTT.

@Test(timeout = 60 * 1000)
public void testAmbiguousRoutingWithMQTT() throws Exception {
    String anycastAddress = "foo/bar";
    EnumSet<RoutingType> routingTypeSet = EnumSet.of(RoutingType.ANYCAST, RoutingType.MULTICAST);
    getServer().addAddressInfo(new AddressInfo(SimpleString.toSimpleString("foo.bar"), routingTypeSet));
    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));
    assertNotNull(connection1.receive(1000, TimeUnit.MILLISECONDS));
    assertNotNull(connection2.receive(1000, TimeUnit.MILLISECONDS));
    assertNotNull(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) RoutingType(org.apache.activemq.artemis.api.core.RoutingType) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo) 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