Search in sources :

Example 16 with Topic

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

the class MQTTTest method testLinkRouteAmqpReceiveMQTT.

@Test(timeout = 60 * 1000)
public void testLinkRouteAmqpReceiveMQTT() throws Exception {
    MQTT mqtt = createMQTTConnection();
    mqtt.setClientId("TestClient");
    BlockingConnection blockingConnection = mqtt.blockingConnection();
    blockingConnection.connect();
    Topic t = new Topic("test", QoS.AT_LEAST_ONCE);
    blockingConnection.subscribe(new Topic[] { t });
    AmqpClient client = new AmqpClient(new URI(AMQP_URI), null, null);
    AmqpConnection connection = client.connect();
    try {
        AmqpSession session = connection.createSession();
        AmqpSender sender = session.createSender("test", true);
        AmqpMessage message = new AmqpMessage();
        message.setText("Test-Message");
        sender.send(message);
        sender.close();
    } finally {
        connection.close();
    }
    try {
        blockingConnection.subscribe(new Topic[] { t });
        assertNotNull(blockingConnection.receive(5, TimeUnit.SECONDS));
    } finally {
        blockingConnection.kill();
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) URI(java.net.URI) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Test(org.junit.Test)

Example 17 with Topic

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

the class MQTTTest method testPacketIdGeneratorNonCleanSession.

@Test(timeout = 90 * 1000)
public void testPacketIdGeneratorNonCleanSession() throws Exception {
    final MQTT mqtt = createMQTTConnection("nonclean-packetid", false);
    mqtt.setKeepAlive((short) 15);
    final Map<Short, PUBLISH> publishMap = new ConcurrentHashMap<>();
    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);
                    LOG.info("PUBLISH " + publish);
                } catch (ProtocolException e) {
                    fail("Error decoding publish " + e.getMessage());
                }
                if (publishMap.get(publish.messageId()) != null) {
                    assertTrue(publish.dup());
                }
                publishMap.put(publish.messageId(), publish);
            }
        }

        @Override
        public void onSend(MQTTFrame frame) {
            LOG.info("Client sent:\n" + frame);
        }
    });
    BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    final String TOPIC = "TopicA/";
    connection.subscribe(new Topic[] { new Topic(TOPIC, QoS.EXACTLY_ONCE) });
    // publish non-retained messages
    final int TOTAL_MESSAGES = 10;
    for (int i = 0; i < TOTAL_MESSAGES; i++) {
        connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
    }
    // receive half the messages in this session
    for (int i = 0; i < TOTAL_MESSAGES / 2; i++) {
        final Message msg = connection.receive(1000, TimeUnit.MILLISECONDS);
        assertNotNull(msg);
        assertEquals(TOPIC, new String(msg.getPayload()));
        msg.ack();
    }
    connection.disconnect();
    // resume session
    connection = mqtt.blockingConnection();
    connection.connect();
    // receive rest of the messages
    Message msg = null;
    do {
        msg = connection.receive(1000, TimeUnit.MILLISECONDS);
        if (msg != null) {
            assertEquals(TOPIC, new String(msg.getPayload()));
            msg.ack();
        }
    } while (msg != null);
    // make sure we received all message ids
    for (short id = 1; id <= TOTAL_MESSAGES; id++) {
        assertNotNull("No message for id " + id, publishMap.get(id));
    }
    connection.unsubscribe(new String[] { TOPIC });
    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) 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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 18 with Topic

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

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

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

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