Search in sources :

Example 6 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 7 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 8 with Message

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

the class MqttIOTest method testWrite.

@Test
public void testWrite() throws Exception {
    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.AT_LEAST_ONCE) });
    final Set<String> messages = new HashSet<>();
    Thread subscriber = new Thread() {

        public void run() {
            try {
                for (int i = 0; i < 200; i++) {
                    Message message = connection.receive();
                    messages.add(new String(message.getPayload()));
                    message.ack();
                }
            } catch (Exception e) {
                LOG.error("Can't receive message", e);
            }
        }
    };
    subscriber.start();
    ArrayList<byte[]> data = new ArrayList<>();
    for (int i = 0; i < 200; i++) {
        data.add(("Test " + i).getBytes());
    }
    pipeline.apply(Create.of(data)).apply(MqttIO.write().withConnectionConfiguration(MqttIO.ConnectionConfiguration.create("tcp://localhost:" + port, "WRITE_TOPIC")));
    pipeline.run();
    subscriber.join();
    connection.disconnect();
    assertEquals(200, messages.size());
    for (int i = 0; i < 200; i++) {
        assertTrue(messages.contains("Test " + i));
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) Message(org.fusesource.mqtt.client.Message) ArrayList(java.util.ArrayList) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Message (org.fusesource.mqtt.client.Message)8 MQTT (org.fusesource.mqtt.client.MQTT)7 BlockingConnection (org.fusesource.mqtt.client.BlockingConnection)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