Search in sources :

Example 61 with BlockingConnection

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

the class MqttClusterRemoteSubscribeTest method unsubscribeRemoteQueue.

@Test
public void unsubscribeRemoteQueue() throws Exception {
    final String TOPIC = "test/1/some/la";
    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
        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(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);
        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();
        assertEquals(payload1, new String(message1.getPayload()));
        assertEquals(payload2, new String(message2.getPayload()));
        assertEquals(payload3, new String(message3.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);
        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();
        String message11String = new String(message11.getPayload());
        String message21String = new String(message21.getPayload());
        String message31String = new String(message31.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));
    } 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) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 62 with BlockingConnection

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

the class MqttClusterRemoteSubscribeTest method unsubscribeRemoteQueueWildCard.

@Test
public void unsubscribeRemoteQueueWildCard() throws Exception {
    final String TOPIC = "test/+/some/#";
    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
        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);
        message1.ack();
        Message message2 = connection2.receive(5, TimeUnit.SECONDS);
        message2.ack();
        Message message3 = connection1.receive(5, TimeUnit.SECONDS);
        message3.ack();
        assertEquals(payload1, new String(message1.getPayload()));
        assertEquals(payload2, new String(message2.getPayload()));
        assertEquals(payload3, new String(message3.getPayload()));
        connection2.unsubscribe(new String[] { TOPIC });
        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 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();
        String message11String = new String(message11.getPayload());
        String message21String = new String(message21.getPayload());
        String message31String = new String(message31.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));
    } 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) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 63 with BlockingConnection

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

the class MqttClusterRemoteSubscribeTest method unsubscribeExistingQueue.

@Test
public void unsubscribeExistingQueue() throws Exception {
    final String TOPIC = "test/1/some/la";
    setupServers(TOPIC);
    startServers(0, 1);
    BlockingConnection connection1 = null;
    BlockingConnection connection2 = null;
    BlockingConnection connection3 = null;
    try {
        connection1 = retrieveMQTTConnection("tcp://localhost:61616");
        connection2 = retrieveMQTTConnection("tcp://localhost:61617");
        connection3 = retrieveMQTTConnection("tcp://localhost:61617");
        // Subscribe to topics
        Topic[] topics = { new Topic(TOPIC, QoS.AT_MOST_ONCE) };
        connection1.subscribe(topics);
        connection2.subscribe(topics);
        connection3.subscribe(topics);
        waitForBindings(0, TOPIC, 1, 1, true);
        waitForBindings(1, TOPIC, 1, 2, true);
        waitForBindings(0, TOPIC, 1, 2, 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(TOPIC, 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 = connection3.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);
        Message message11 = connection1.receive(5, TimeUnit.SECONDS);
        message11.ack();
        Message message21 = connection3.receive(5, TimeUnit.SECONDS);
        message21.ack();
        Message message31 = connection1.receive(5, TimeUnit.SECONDS);
        message31.ack();
        String message11String = new String(message11.getPayload());
        String message21String = new String(message21.getPayload());
        String message31String = new String(message31.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));
    } 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(topics);
            connection3.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)

Example 64 with BlockingConnection

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

the class InterceptorExample method main.

public static void main(final String[] args) throws Exception {
    System.out.println("Connecting to Artemis using MQTT");
    MQTT mqtt = new MQTT();
    mqtt.setHost("tcp://localhost:1883");
    BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    System.out.println("Connected to Artemis");
    // Subscribe to a topic
    Topic[] topics = { new Topic("mqtt/example/interceptor", QoS.EXACTLY_ONCE) };
    connection.subscribe(topics);
    System.out.println("Subscribed to topics.");
    // Publish message
    String payload1 = "This is message 1";
    connection.publish("mqtt/example/interceptor", payload1.getBytes(), QoS.EXACTLY_ONCE, false);
    System.out.println("Sent message");
    // Receive the sent message
    Message message1 = connection.receive(5, TimeUnit.SECONDS);
    String messagePayload = new String(message1.getPayload(), StandardCharsets.UTF_8);
    System.out.println("Received message: " + messagePayload);
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) Message(org.fusesource.mqtt.client.Message) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic)

Example 65 with BlockingConnection

use of org.fusesource.mqtt.client.BlockingConnection in project wildfly-camel by wildfly-extras.

the class MQTTIntegrationTest method testMQTTProducer.

@Test
public void testMQTTProducer() throws Exception {
    String conUrl = TestUtils.getResourceValue(getClass(), "/tcp-connection");
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").transform(body().prepend("Hello ")).to("mqtt:foo?publishTopicName=" + BrokerSetup.TEST_TOPIC + "&host=" + conUrl);
        }
    });
    camelctx.start();
    try {
        MQTT mqtt = new MQTT();
        mqtt.setHost(conUrl);
        BlockingConnection connection = mqtt.blockingConnection();
        connection.connect();
        try {
            Topic topic = new Topic(BrokerSetup.TEST_TOPIC, QoS.AT_MOST_ONCE);
            connection.subscribe(new Topic[] { topic });
            ProducerTemplate producer = camelctx.createProducerTemplate();
            producer.asyncSendBody("direct:start", "Kermit");
            Message message = connection.receive(10, TimeUnit.SECONDS);
            message.ack();
            String result = new String(message.getPayload());
            Assert.assertEquals("Hello Kermit", result);
        } finally {
            connection.disconnect();
        }
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) MQTT(org.fusesource.mqtt.client.MQTT) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) Message(org.fusesource.mqtt.client.Message) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) 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