In this section's code sample, a message is published to the server at QoS 2, which means it is ensured to be delivered once and only once.
To achieve this delivery, the message needs to be persisted by the client. A default implementation of persistence is provided by MqttDefaultFilePersistence
. This implementation is used by the client unless you supply an alternative.
// Create a client to communicate with a broker at the specified address
MqttClient client = new MqttClient("tcp://localhost:1883");
// Connect to the broker
// Create message and set quality of service to "2" (assured delivery)
MqttMessage message = new MqttMessage("Hello world");
// Publish the message to the broker
MqttTopic topic = client.getTopic("foo/bar");
MqttDeliveryToken token = topic.publish(message);
// Block until the message is safely with the broker
// Disconnect from the broker
Parent topic: Using the MQTT Java client: XPD622