Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[paho-dev] Issue while publishing QOS 0 messages on Automatic re-connect

Hi ,

When Connect/Automatic  reconnect is successful  I am publishing the messages/resume publishing.
If  QOS level is set to 0, messages are not getting published.Same code work fine with QOS set to 1 and 2.

Note:I am not using disconnected publishing as my queue requirement is different.

Attaching the code - 
public class MqttPublishQosIssue {
static MqttClient sampleClient = null;
private static final String topic = "MQTT Examples";
private static final String content = "Message from MqttPublishSample-";
private static final int qos = 0;
private static final String broker = "tcp://localhost:1883";
private static final String clientId = "JavaSample";
public static void main(String[] args) throws InterruptedException {
try {
sampleClient = new MqttClient(broker, clientId, new MemoryPersistence());
final MqttConnectOptions connOpts = new MqttConnectOptions();
sampleClient.setCallback(new MqttCallbackExtended() {
public void connectComplete(boolean reconnect, String serverURI) {
System.out.println("<< Connected to MQTT broker >> reconnect FLag" + reconnect);
try {
//Process Messages when connect is true/or automatic reconnect is true.
} catch (MqttException e) {
public void connectionLost(Throwable cause) {
System.out.println("<< Connection to MQTT broker lost >>" + cause.getMessage());

public void deliveryComplete(IMqttDeliveryToken token) {
System.out.println("<< Message was successfully delivered to MQTT>>");

public void messageArrived(String topic, MqttMessage message) throws Exception {
// Nothing to do

//No disconnect call as messages should be sent in a while loop

} catch (MqttException me) {
System.out.println("excep " + me);

private static void processMessages() throws MqttException, MqttPersistenceException {
System.out.println("[[Start processing of messages]]");
//Take incoming new + pending messages from user defined Q
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qos);//QOS level is 0 message publish wont happen,Thread gets stuck !!.No callback's as well.
sampleClient.publish(topic, message);
System.out.println("[[End processing of messages]]");


Back to the top