How to set the timeout for a MQTT client?
If you have confusion you can go to MqttConnectionOptions for detail.
String userName="Ohelig";
String password="Pojke";
MqttClient client = new MqttClient("tcp://192.168.1.4:1883","Sending");
MqttConnectOptions authen = new MqttConnectOptions();
authen.setUserName(userName);
authen.setPassword(password.toCharArray());
authen.setKeepAliveInterval(30);
authen.setConnectionTimeout(300);
client.connect(authen);
Comments
-
Cristian about 2 years
I'm using the IA92 Java implementation for MQTT, which allows me to connect to a MQTT broker. In order to establish the connection, I'm doing something like this:
// Create connection spec String mqttConnSpec = "tcp://the_server@the_port"; // Create the client and connect mqttClient = MqttClient.createMqttClient(mqttConnSpec, null); mqttClient.connect("the_id", true, 666);
The problem is that sometimes the server takes too much time to send a response, and it throws a timeout exception:
org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocket(OSNetworkSystem.java:130) at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:246) at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533) at java.net.Socket.connect(Socket.java:1055) at com.ibm.mqtt.j2se.MqttJava14NetSocket.<init>((null):-1) at com.ibm.mqtt.j2se.MqttJavaNetSocket.setConnection((null):-1) at com.ibm.mqtt.Mqtt.tcpipConnect((null):-1) at com.ibm.mqtt.MqttBaseClient.doConnect((null):-1) at com.ibm.mqtt.MqttBaseClient.connect((null):-1) at com.ibm.mqtt.MqttClient.connect((null):-1) at com.ibm.mqtt.MqttClient.connect((null):-1)
What I need to do is setting a timeout manually, instead of letting the mqtt client decide that. The documentation says: There are also methods for setting attributes of the MQ Telemetry Transport connection, such as timeouts and retries.
But, honestly, I haven't found anything about it. I have taken a look at the whole javadoc reference and there's no evidence of timeout configuration. I can't see the source code since it's not open source.
So how can I set the timeout for the Mqtt connection?