Search in sources :

Example 46 with Topic

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

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

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

Example 49 with Topic

use of org.fusesource.mqtt.client.Topic in project beam by apache.

the class MqttIOTest method testWrite.

@Test
public void testWrite() throws Exception {
    final int numberOfTestMessages = 200;
    MQTT client = new MQTT();
    client.setHost("tcp://localhost:" + port);
    final BlockingConnection connection = client.blockingConnection();
    connection.connect();
    connection.subscribe(new Topic[] { new Topic(Buffer.utf8("WRITE_TOPIC"), QoS.EXACTLY_ONCE) });
    final Set<String> messages = new ConcurrentSkipListSet<>();
    Thread subscriber = new Thread(() -> {
        try {
            for (int i = 0; i < numberOfTestMessages; i++) {
                Message message = connection.receive();
                messages.add(new String(message.getPayload(), StandardCharsets.UTF_8));
                message.ack();
            }
        } catch (Exception e) {
            LOG.error("Can't receive message", e);
        }
    });
    subscriber.start();
    ArrayList<byte[]> data = new ArrayList<>();
    for (int i = 0; i < numberOfTestMessages; i++) {
        data.add(("Test " + i).getBytes(StandardCharsets.UTF_8));
    }
    pipeline.apply(Create.of(data)).apply(MqttIO.write().withConnectionConfiguration(MqttIO.ConnectionConfiguration.create("tcp://localhost:" + port, "WRITE_TOPIC")));
    pipeline.run();
    subscriber.join();
    connection.disconnect();
    assertEquals(numberOfTestMessages, messages.size());
    for (int i = 0; i < numberOfTestMessages; i++) {
        assertTrue(messages.contains("Test " + i));
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) Message(org.fusesource.mqtt.client.Message) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ArrayList(java.util.ArrayList) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 50 with Topic

use of org.fusesource.mqtt.client.Topic in project storm by apache.

the class StormMqttIntegrationTest method testMqttTopology.

@Test
public void testMqttTopology() throws Exception {
    MQTT client = new MQTT();
    client.setTracer(new MqttLogger());
    URI uri = URI.create("tcp://localhost:1883");
    client.setHost(uri);
    client.setClientId("MQTTSubscriber");
    client.setCleanSession(false);
    BlockingConnection connection = client.blockingConnection();
    connection.connect();
    Topic[] topics = { new Topic("/integration-result", QoS.AT_LEAST_ONCE) };
    byte[] qoses = connection.subscribe(topics);
    try (LocalCluster cluster = new LocalCluster();
        LocalTopology topo = cluster.submitTopology("test", new Config(), buildMqttTopology())) {
        LOG.info("topology started");
        while (!spoutActivated) {
            Thread.sleep(500);
        }
        // publish a retained message to the broker
        MqttOptions options = new MqttOptions();
        options.setCleanConnection(false);
        MqttPublisher publisher = new MqttPublisher(options, true);
        publisher.connectMqtt("MqttPublisher");
        publisher.publish(new MqttMessage(TEST_TOPIC, "test".getBytes()));
        LOG.info("published message");
        Message message = connection.receive();
        LOG.info("Message recieved on topic: {}", message.getTopic());
        LOG.info("Payload: {}", new String(message.getPayload()));
        message.ack();
        Assert.assertArrayEquals(message.getPayload(), RESULT_PAYLOAD.getBytes());
        Assert.assertEquals(message.getTopic(), RESULT_TOPIC);
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) LocalCluster(org.apache.storm.LocalCluster) MqttOptions(org.apache.storm.mqtt.common.MqttOptions) Message(org.fusesource.mqtt.client.Message) Config(org.apache.storm.Config) MqttPublisher(org.apache.storm.mqtt.common.MqttPublisher) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) URI(java.net.URI) LocalTopology(org.apache.storm.LocalCluster.LocalTopology) Topic(org.fusesource.mqtt.client.Topic) IntegrationTest(org.apache.storm.testing.IntegrationTest) Test(org.junit.jupiter.api.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