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