-1

I use library paho for connectivity with mqtt broker, sending messages and almost everything work fine, but i have problem with setTimeToWait() method. it does not metter how many milliseconds i put into method setTimeToWait(2000) or setTimeToWait(10). I always get messages from publiusher.

Why can i get messages within the hours? If i set waiting time 2000 milliseconds. I thought after 2 secs absence of messages from publisher my subscriber cannot get messages from publiser and control will be returned.

What Am i doing wrong?

Publisher code:

public class MqttPublishSample {

    public static void main(String[] args) throws MqttException {

        String messageString = "{\"device_status\": \"ready\"}";

        if (
                args.length == 2 ) {
            messageString = args[1];
        }


        System.out.println("== START PUBLISHER ==");
        MqttClient client = new MqttClient("tcp://localhost:1883" , MqttClient.generateClientId());
        client.connect();
        MqttMessage message = new MqttMessage();
        message.setPayload(messageString.getBytes());
        message.setQos(1);
        client.publish("/catalog", message);

        System.out.println("\tMessage '"+ messageString +"' to 'iot_data'");
        client.disconnect();

        System.out.println("== END PUBLISHER ==");

    }
}

Subscriber code:

public class MqttSuscribeSample {

    public static void main(String[] args) {

        System.out.println("== START SUBSCRIBER ==");

        try{
            MqttClient client=new MqttClient("tcp://localhost:1883", MqttClient.generateClientId());
            client.setTimeToWait(2000);
            client.setCallback( new SimpleMqttCallback() );
            client.connect();
            client.subscribe( "/catalog");
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

}

SimpleMqttCallback code

public class SimpleMqttCallback implements MqttCallback {

    public void connectionLost(Throwable throwable) {
        System.out.println("Connection to MQTT broker lost!");
    }

    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        System.out.println("Message received:\t"+ new String(mqttMessage.getPayload()) );

    }

    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
    }
}
hardillb
  • 54,545
  • 11
  • 67
  • 105
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – sɐunıɔןɐqɐp Jun 09 '18 at 13:26

1 Answers1

0

The setTimeToWait() method is how long the the client should wait for a response from the broker when carrying out specific actions e.g.

  • publishing a message
  • making a subscription
  • connecting or disconnecting from the broker

It is not how long the client should wait or a message to be delivered for an existing subscription.

If you want this sort of behaviour you need to run your own timer and either unsubscribe from the topic or disconnect from the broker when it times out.

hardillb
  • 54,545
  • 11
  • 67
  • 105