Search in sources :

Example 66 with BlockingConnection

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

the class MqttIOTest method testRead.

@Test(timeout = 30 * 1000)
@Ignore("https://issues.apache.org/jira/browse/BEAM-5150 Flake Non-deterministic output.")
public void testRead() throws Exception {
    PCollection<byte[]> output = pipeline.apply(MqttIO.read().withConnectionConfiguration(MqttIO.ConnectionConfiguration.create("tcp://localhost:" + port, "READ_TOPIC").withClientId("READ_PIPELINE")).withMaxReadTime(Duration.standardSeconds(3)));
    PAssert.that(output).containsInAnyOrder("This is test 0".getBytes(StandardCharsets.UTF_8), "This is test 1".getBytes(StandardCharsets.UTF_8), "This is test 2".getBytes(StandardCharsets.UTF_8), "This is test 3".getBytes(StandardCharsets.UTF_8), "This is test 4".getBytes(StandardCharsets.UTF_8), "This is test 5".getBytes(StandardCharsets.UTF_8), "This is test 6".getBytes(StandardCharsets.UTF_8), "This is test 7".getBytes(StandardCharsets.UTF_8), "This is test 8".getBytes(StandardCharsets.UTF_8), "This is test 9".getBytes(StandardCharsets.UTF_8));
    // 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(() -> {
        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(StandardCharsets.UTF_8), QoS.EXACTLY_ONCE, false);
            }
        } catch (Exception e) {
        // nothing to do
        }
    });
    publisherThread.start();
    pipeline.run();
    publisherThread.join();
    publishConnection.disconnect();
}
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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 67 with BlockingConnection

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

the class MqttIOTest method testReadNoClientId.

@Test(timeout = 60 * 1000)
@Ignore("https://issues.apache.org/jira/browse/BEAM-3604 Test timeout failure.")
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(StandardCharsets.UTF_8), "This is test 1".getBytes(StandardCharsets.UTF_8), "This is test 2".getBytes(StandardCharsets.UTF_8), "This is test 3".getBytes(StandardCharsets.UTF_8), "This is test 4".getBytes(StandardCharsets.UTF_8), "This is test 5".getBytes(StandardCharsets.UTF_8), "This is test 6".getBytes(StandardCharsets.UTF_8), "This is test 7".getBytes(StandardCharsets.UTF_8), "This is test 8".getBytes(StandardCharsets.UTF_8), "This is test 9".getBytes(StandardCharsets.UTF_8));
    // 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(() -> {
        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(StandardCharsets.UTF_8), QoS.EXACTLY_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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 68 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 {
    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 69 with BlockingConnection

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

Example 70 with BlockingConnection

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

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