public static void main(String[] args) {
try {
/** a. Create an instance of MQTT client */
MqttClient client = new MqttClient(Example.TCPAddress, Example.clientId);
/** b. Prepare connection options */
// Callback callback = new Callback(Example.clientId);
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
cause.printStackTrace();
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println("Topic=" + topic + ", msg=" + message);
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
});
MqttConnectOptions conOptions = new MqttConnectOptions();
conOptions.setCleanSession(Example.cleanSession);
conOptions.setKeepAliveInterval(Example.keepAliveInterval);
/** c. Connect to broker with the connection options */
client.connect();
System.out.println("Subscribing to topic \"" + Example.subTopicString + "\" for client instance \""
+ client.getClientId() + "\" using QoS " + Example.QoS + ". Clean session is "
+ Example.cleanSession);
/** d. subscribe interested topics */
client.subscribe(Example.subTopicString, Example.QoS);
System.out.format("Subscribing to topic %s \n for client %s using QoS %d \n\n"
+ "Press Q<Enter> to quit \n\n", Example.subTopicString, Example.clientId, Example.QoS);
Scanner scanner = new Scanner(System.in);
for (String input = ""; !input.equalsIgnoreCase("q"); input = scanner.nextLine())
;
// e. Disconnect to broker
client.disconnect();
System.out.println("Finished");
}
catch (Exception e) {
e.printStackTrace();
}
}