开发者

How to set the timeout for a MQTT client?

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 al开发者_运维问答so 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?


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);


I don't know anything about ia92, but I'd imagine that the 666 in the connect() call is what you're trying to set the timeout to?

The timeout the documentation is referring to is probably the keepalive timeout. This is the maximum number of seconds (chosen by the client) that can elapse without communication between the server and client. I think this is what you're most interested in.

Retries on the other hand are most likely to refer to the retrying of messages that seem to have gone astray when sending messages with QoS>0. This will be something handled by the client library code though, rather than the broker. This is something that comes into play only after you've connected though, so I very much doubt it's your problem.

To be sure that the keepalive timeout is being set correctly, I'd try pointing your client at a modified mosquitto broker. You can modify mqtt3_handle_connect() in src/read_handle_server.c to print out the keepalive value when you connect. This will ensure it's doing what you think, but won't help with the actual problem I'm afraid!


What broker do you use? Really Small Message Broker V1.1 Alpha, Mosquitto, the broker that comes with IBM WebSphere? You need to set this timeout value in your server configuration. Because the system works that way. You set a keep alive value in your broker and send a ping from the client before that interval expires, in order not for the broker to close the client-server connection, and the process restarts. Actually, even if that interval expires, server will still not close the connection until the 'grace period' ends. See http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#connect

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜