Search in sources :

Example 16 with BlockingConnection

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

the class MQTTTest method testDuplicateSubscriptions.

@Test(timeout = 60 * 1000)
public void testDuplicateSubscriptions() throws Exception {
    MQTT mqtt = createMQTTConnection();
    mqtt.setClientId("foo");
    mqtt.setKeepAlive((short) 20);
    final int[] actualQoS = { -1 };
    mqtt.setTracer(new Tracer() {

        @Override
        public void onReceive(MQTTFrame frame) {
            // validate the QoS
            if (frame.messageType() == PUBLISH.TYPE) {
                actualQoS[0] = frame.qos().ordinal();
            }
        }
    });
    final BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    final String RETAIN = "RETAIN";
    connection.publish("TopicA", RETAIN.getBytes(), QoS.EXACTLY_ONCE, true);
    QoS[] qoss = { QoS.AT_MOST_ONCE, QoS.AT_MOST_ONCE, QoS.AT_LEAST_ONCE, QoS.EXACTLY_ONCE };
    for (QoS qos : qoss) {
        connection.subscribe(new Topic[] { new Topic("TopicA", qos) });
        final Message msg = connection.receive(5000, TimeUnit.MILLISECONDS);
        assertNotNull("No message for " + qos, msg);
        assertEquals(RETAIN, new String(msg.getPayload()));
        msg.ack();
        int waitCount = 0;
        while (actualQoS[0] == -1 && waitCount < 10) {
            Thread.sleep(1000);
            waitCount++;
        }
        assertEquals(qos.ordinal(), actualQoS[0]);
        actualQoS[0] = -1;
    }
    connection.unsubscribe(new String[] { "TopicA" });
    connection.disconnect();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) QoS(org.fusesource.mqtt.client.QoS) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Message(org.fusesource.mqtt.client.Message) BytesMessage(javax.jms.BytesMessage) Tracer(org.fusesource.mqtt.client.Tracer) 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 17 with BlockingConnection

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

the class MQTTTest method testAnycastPrefixWorksWithMQTT.

@Test(timeout = 60 * 1000)
public void testAnycastPrefixWorksWithMQTT() throws Exception {
    String clientId = "testMqtt";
    String anycastAddress = "anycast:foo/bar";
    String sendAddress = "foo/bar";
    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(sendAddress, message1.getBytes(), QoS.AT_LEAST_ONCE, false);
    connection2.publish(sendAddress, 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) Test(org.junit.Test)

Example 18 with BlockingConnection

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

the class MQTTTest method testBrokerRestartAfterSubHashWithConfigurationQueues.

@Test(timeout = 60 * 1000)
public void testBrokerRestartAfterSubHashWithConfigurationQueues() throws Exception {
    // Add some pre configured queues
    CoreQueueConfiguration coreQueueConfiguration = new CoreQueueConfiguration();
    coreQueueConfiguration.setName("DLQ");
    coreQueueConfiguration.setRoutingType(RoutingType.ANYCAST);
    coreQueueConfiguration.setAddress("DLA");
    CoreAddressConfiguration coreAddressConfiguration = new CoreAddressConfiguration();
    coreAddressConfiguration.setName("DLA");
    coreAddressConfiguration.addRoutingType(RoutingType.ANYCAST);
    coreAddressConfiguration.addQueueConfiguration(coreQueueConfiguration);
    getServer().getConfiguration().getAddressConfigurations().add(coreAddressConfiguration);
    getServer().stop();
    getServer().start();
    getServer().waitForActivation(10, TimeUnit.SECONDS);
    for (int i = 0; i < 2; i++) {
        MQTT mqtt = createMQTTConnection("myClient", false);
        BlockingConnection connection = mqtt.blockingConnection();
        connection.connect();
        connection.subscribe(new Topic[] { new Topic("#", QoS.AT_MOST_ONCE) });
        connection.disconnect();
        getServer().stop();
        getServer().start();
        getServer().waitForActivation(10, TimeUnit.SECONDS);
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) CoreAddressConfiguration(org.apache.activemq.artemis.core.config.CoreAddressConfiguration) CoreQueueConfiguration(org.apache.activemq.artemis.core.config.CoreQueueConfiguration) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 19 with BlockingConnection

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

the class MQTTTest method testMQTT311Connection.

@Test(timeout = 60 * 1000)
public void testMQTT311Connection() throws Exception {
    MQTT mqtt = createMQTTConnection();
    mqtt.setClientId("foo");
    mqtt.setVersion("3.1.1");
    final BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    connection.disconnect();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Test(org.junit.Test)

Example 20 with BlockingConnection

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

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