Search in sources :

Example 1 with Message

use of org.fusesource.mqtt.client.Message 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) Test(org.junit.Test) IntegrationTest(org.apache.storm.testing.IntegrationTest)

Example 2 with Message

use of org.fusesource.mqtt.client.Message in project quickstarts by jboss-switchyard.

the class CamelMQTTBindingTest method testReferenceBinding.

@Ignore("Disable for now due to https://issues.jboss.org/browse/SWITCHYARD-2221")
@Test
public void testReferenceBinding() throws Exception {
    MQTT mqtt = new MQTT();
    Topic outputTopic = new Topic(TOPIC_OUTPUT, QoS.AT_LEAST_ONCE);
    BlockingConnection connection = mqtt.blockingConnection();
    try {
        connection.connect();
        connection.subscribe(new Topic[] { outputTopic });
        _greet.sendInOnly(MESSAGE_INPUT);
        Message message = connection.receive(1000, TimeUnit.MILLISECONDS);
        Assert.assertNotNull("No output message from " + TOPIC_OUTPUT, message);
        Assert.assertEquals(MESSAGE_OUTPUT, new String(message.getPayload()));
        Assert.assertNull("More than one message received from " + TOPIC_OUTPUT, connection.receive(1000, TimeUnit.MILLISECONDS));
    } finally {
        connection.disconnect();
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) Message(org.fusesource.mqtt.client.Message) Topic(org.fusesource.mqtt.client.Topic) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with Message

use of org.fusesource.mqtt.client.Message in project quickstarts by jboss-switchyard.

the class MQTTClient method main.

/**
     * Only execution point for this application.
     * @param args command line args.
     * @throws Exception if something goes wrong.
     */
public static void main(final String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(MESSAGE_PAYLOAD)));
    String payload = reader.readLine();
    reader.close();
    BlockingConnection publishConnection = null;
    BlockingConnection subscribeConnection = null;
    try {
        Topic outputTopic = new Topic(TOPIC_OUTPUT, QoS.AT_LEAST_ONCE);
        MQTT mqtt = new MQTT();
        mqtt.setUserName(USER_NAME);
        mqtt.setPassword(PASSWORD);
        subscribeConnection = mqtt.blockingConnection();
        subscribeConnection.connect();
        subscribeConnection.subscribe(new Topic[] { outputTopic });
        publishConnection = mqtt.blockingConnection();
        publishConnection.connect();
        publishConnection.publish(TOPIC_INPUT, payload.getBytes(), QoS.AT_LEAST_ONCE, false);
        System.out.println("Published a message to " + TOPIC_INPUT + ": " + payload);
        Message msg = subscribeConnection.receive(60000, TimeUnit.MILLISECONDS);
        if (msg != null) {
            System.out.println("Received a message from " + TOPIC_OUTPUT + ": " + new String(msg.getPayload()));
        } else {
            System.out.println("No message was received from " + TOPIC_OUTPUT);
        }
    } finally {
        if (publishConnection != null && publishConnection.isConnected()) {
            publishConnection.disconnect();
        }
        if (subscribeConnection != null && subscribeConnection.isConnected()) {
            subscribeConnection.disconnect();
        }
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) InputStreamReader(java.io.InputStreamReader) Message(org.fusesource.mqtt.client.Message) BufferedReader(java.io.BufferedReader) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic)

Example 4 with Message

use of org.fusesource.mqtt.client.Message in project camel by apache.

the class MQTTProducerReconnectTest method testProduce.

@Test
public void testProduce() throws Exception {
    MQTT mqtt = new MQTT();
    mqtt.setHost(MQTTTestSupport.getHostForMQTTEndpoint());
    final BlockingConnection subscribeConnection = mqtt.blockingConnection();
    subscribeConnection.connect();
    Topic topic = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
    Topic[] topics = { topic };
    subscribeConnection.subscribe(topics);
    final CountDownLatch latch = new CountDownLatch(numberOfMessages);
    Thread thread = new Thread(new Runnable() {

        public void run() {
            for (int i = 0; i < numberOfMessages; i++) {
                try {
                    Message message = subscribeConnection.receive();
                    message.ack();
                    latch.countDown();
                } catch (Exception e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
    });
    thread.start();
    for (int i = 0; i < numberOfMessages; i++) {
        if (i == 5) {
            LOG.info("Stopping MQTT transport");
            brokerService.getTransportConnectorByScheme("mqtt").stop();
            Thread starter = new Thread(new Runnable() {

                public void run() {
                    try {
                        Thread.sleep(3000);
                        LOG.info("Starting MQTT transport again");
                        brokerService.getTransportConnectorByScheme("mqtt").start();
                    } catch (Exception e) {
                    // ignore
                    }
                }
            });
            starter.start();
        }
        try {
            template.sendBody("direct:foo", "test message " + i);
        } catch (Exception e) {
        // ignore
        }
    }
    latch.await(20, TimeUnit.SECONDS);
    assertTrue("Messages not consumed = " + latch.getCount(), latch.getCount() == 0);
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with Message

use of org.fusesource.mqtt.client.Message in project camel by apache.

the class MQTTProducerTest method testProduce.

@Test
public void testProduce() throws Exception {
    MQTT mqtt = new MQTT();
    mqtt.setHost(MQTTTestSupport.getHostForMQTTEndpoint());
    final BlockingConnection subscribeConnection = mqtt.blockingConnection();
    subscribeConnection.connect();
    Topic topic = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
    Topic[] topics = { topic };
    subscribeConnection.subscribe(topics);
    final CountDownLatch latch = new CountDownLatch(numberOfMessages);
    Thread thread = new Thread(new Runnable() {

        public void run() {
            for (int i = 0; i < numberOfMessages; i++) {
                try {
                    Message message = subscribeConnection.receive();
                    message.ack();
                    latch.countDown();
                } catch (Exception e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
    });
    thread.start();
    Producer producer = context.getEndpoint("direct:foo").createProducer();
    for (int i = 0; i < numberOfMessages; i++) {
        Exchange exchange = producer.createExchange();
        exchange.getIn().setBody("test message " + i);
        producer.process(exchange);
    }
    latch.await(20, TimeUnit.SECONDS);
    assertTrue("Messages not consumed = " + latch.getCount(), latch.getCount() == 0);
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) Exchange(org.apache.camel.Exchange) Message(org.fusesource.mqtt.client.Message) Producer(org.apache.camel.Producer) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

BlockingConnection (org.fusesource.mqtt.client.BlockingConnection)6 MQTT (org.fusesource.mqtt.client.MQTT)6 Message (org.fusesource.mqtt.client.Message)6 Topic (org.fusesource.mqtt.client.Topic)6 Test (org.junit.Test)5 CountDownLatch (java.util.concurrent.CountDownLatch)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Exchange (org.apache.camel.Exchange)1 Producer (org.apache.camel.Producer)1 Config (org.apache.storm.Config)1 LocalCluster (org.apache.storm.LocalCluster)1 LocalTopology (org.apache.storm.LocalCluster.LocalTopology)1 MqttOptions (org.apache.storm.mqtt.common.MqttOptions)1 MqttPublisher (org.apache.storm.mqtt.common.MqttPublisher)1 IntegrationTest (org.apache.storm.testing.IntegrationTest)1 Ignore (org.junit.Ignore)1