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();
}
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();
}
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));
}
}
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);
}
}
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();
}
}
Aggregations