Search in sources :

Example 6 with Tracer

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

Aggregations

SimpleString (org.apache.activemq.artemis.api.core.SimpleString)6 BlockingConnection (org.fusesource.mqtt.client.BlockingConnection)6 MQTT (org.fusesource.mqtt.client.MQTT)6 Topic (org.fusesource.mqtt.client.Topic)6 Tracer (org.fusesource.mqtt.client.Tracer)6 MQTTFrame (org.fusesource.mqtt.codec.MQTTFrame)6 Test (org.junit.Test)6 BytesMessage (javax.jms.BytesMessage)5 AmqpMessage (org.apache.activemq.transport.amqp.client.AmqpMessage)5 Message (org.fusesource.mqtt.client.Message)5 ProtocolException (java.net.ProtocolException)4 PUBLISH (org.fusesource.mqtt.codec.PUBLISH)4 ArrayList (java.util.ArrayList)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 QoS (org.fusesource.mqtt.client.QoS)2 Random (java.util.Random)1 Ignore (org.junit.Ignore)1