Search in sources :

Example 11 with BlockingConnection

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

Example 12 with BlockingConnection

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

the class MqttIOTest method testReadNoClientId.

@Test(timeout = 60 * 1000)
public void testReadNoClientId() throws Exception {
    final String topicName = "READ_TOPIC_NO_CLIENT_ID";
    Read mqttReader = MqttIO.read().withConnectionConfiguration(MqttIO.ConnectionConfiguration.create("tcp://localhost:" + port, topicName)).withMaxNumRecords(10);
    PCollection<byte[]> output = pipeline.apply(mqttReader);
    PAssert.that(output).containsInAnyOrder("This is test 0".getBytes(), "This is test 1".getBytes(), "This is test 2".getBytes(), "This is test 3".getBytes(), "This is test 4".getBytes(), "This is test 5".getBytes(), "This is test 6".getBytes(), "This is test 7".getBytes(), "This is test 8".getBytes(), "This is test 9".getBytes());
    // produce messages on the brokerService in another thread
    // This thread prevents to block the pipeline waiting for new messages
    MQTT client = new MQTT();
    client.setHost("tcp://localhost:" + port);
    final BlockingConnection publishConnection = client.blockingConnection();
    publishConnection.connect();
    Thread publisherThread = new Thread() {

        public void run() {
            try {
                LOG.info("Waiting pipeline connected to the MQTT broker before sending " + "messages ...");
                boolean pipelineConnected = false;
                while (!pipelineConnected) {
                    Thread.sleep(1000);
                    for (Connection connection : brokerService.getBroker().getClients()) {
                        if (!connection.getConnectionId().isEmpty()) {
                            pipelineConnected = true;
                        }
                    }
                }
                for (int i = 0; i < 10; i++) {
                    publishConnection.publish(topicName, ("This is test " + i).getBytes(), QoS.AT_LEAST_ONCE, false);
                }
            } catch (Exception e) {
            // nothing to do
            }
        }
    };
    publisherThread.start();
    pipeline.run();
    publishConnection.disconnect();
    publisherThread.join();
}
Also used : Read(org.apache.beam.sdk.io.mqtt.MqttIO.Read) MQTT(org.fusesource.mqtt.client.MQTT) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Connection(org.apache.activemq.broker.Connection) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Test(org.junit.Test)

Example 13 with BlockingConnection

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

the class MqttIOTest method testRead.

@Test(timeout = 60 * 1000)
public void testRead() throws Exception {
    PCollection<byte[]> output = pipeline.apply(MqttIO.read().withConnectionConfiguration(MqttIO.ConnectionConfiguration.create("tcp://localhost:" + port, "READ_TOPIC", "READ_PIPELINE")).withMaxNumRecords(10));
    PAssert.that(output).containsInAnyOrder("This is test 0".getBytes(), "This is test 1".getBytes(), "This is test 2".getBytes(), "This is test 3".getBytes(), "This is test 4".getBytes(), "This is test 5".getBytes(), "This is test 6".getBytes(), "This is test 7".getBytes(), "This is test 8".getBytes(), "This is test 9".getBytes());
    // produce messages on the brokerService in another thread
    // This thread prevents to block the pipeline waiting for new messages
    MQTT client = new MQTT();
    client.setHost("tcp://localhost:" + port);
    final BlockingConnection publishConnection = client.blockingConnection();
    publishConnection.connect();
    Thread publisherThread = new Thread() {

        public void run() {
            try {
                LOG.info("Waiting pipeline connected to the MQTT broker before sending " + "messages ...");
                boolean pipelineConnected = false;
                while (!pipelineConnected) {
                    Thread.sleep(1000);
                    for (Connection connection : brokerService.getBroker().getClients()) {
                        if (connection.getConnectionId().startsWith("READ_PIPELINE")) {
                            pipelineConnected = true;
                        }
                    }
                }
                for (int i = 0; i < 10; i++) {
                    publishConnection.publish("READ_TOPIC", ("This is test " + i).getBytes(), QoS.AT_LEAST_ONCE, false);
                }
            } catch (Exception e) {
            // nothing to do
            }
        }
    };
    publisherThread.start();
    pipeline.run();
    publishConnection.disconnect();
    publisherThread.join();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Connection(org.apache.activemq.broker.Connection) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Test(org.junit.Test)

Aggregations

BlockingConnection (org.fusesource.mqtt.client.BlockingConnection)13 MQTT (org.fusesource.mqtt.client.MQTT)13 Test (org.junit.Test)11 Topic (org.fusesource.mqtt.client.Topic)9 Message (org.fusesource.mqtt.client.Message)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 Connection (org.apache.activemq.broker.Connection)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 Read (org.apache.beam.sdk.io.mqtt.MqttIO.Read)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