Написал программу, которая передает сообщения от клиента брокеру, но мне нужно написать программу, которая будет получать сообщения от брокера.
Пример отправки сообщения ниже:
public class MqttPublishSample {
private String topic = "Lesson_MQTT_server";
private String content = "Lesson message";
private String clientId = "random id";
private String broker = "tcp://m15.cloudmqtt.com:18594";
private char [] password = "******".toCharArray();
private String userName = "*****";
private int qos = 2; // Max quality
private MemoryPersistence persistence;// wtf???
private void MqttMessage (String message) {
System.out.println (message);
}
public MqttPublishSample () {
try {
persistence = new MemoryPersistence ();
MqttMessage ("Create client");
MqttClient client = new MqttClient (broker, clientId, persistence);
MqttMessage ("Create setting of connection");
MqttConnectOptions options = new MqttConnectOptions ();
options.setCleanSession(true);
options.setUserName(userName);
options.setPassword(password);
MqttMessage ("Connect to server");
client.connect(options);
if (client.isConnected())
MqttMessage ("Connect is succeful!");
else
MqttMessage ("Connect falls");
MqttMessage("Publish message");
MqttMessage message = new MqttMessage (content.getBytes());
message.setQos(qos);
client.publish(topic, message);
MqttMessage("Message was published!");
client.disconnect();
MqttMessage("Disconnect!");
} catch (MqttException ex) {
System.out.println("reason: " + ex.getReasonCode());
System.out.println("msg: " + ex.getMessage());
System.out.println("loc : " + ex.getLocalizedMessage());
System.out.println("cause: " + ex.getCause());
}
}
public static void main (String...strings) {
new MqttPublishSample ();
}
}