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