@Test
public void testSendAndReceiveMQTT() throws Exception {
addMQTTConnector(brokerService);
brokerService.start();
MQTT mqtt = new MQTT();
final BlockingConnection subscribeConnection = mqtt.blockingConnection();
subscribeConnection.connect();
Topic topic = new Topic("foo/bah",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();
BlockingConnection publisherConnection = mqtt.blockingConnection();
publisherConnection.connect();
for (int i = 0; i < numberOfMessages; i++){
String payload = "Message " + i;
publisherConnection.publish(topic.name().toString(),payload.getBytes(),QoS.AT_LEAST_ONCE,false);
}