java 如何设置 MQTT 客户端的超时时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5236524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:08:19  来源:igfitidea点击:

How to set the timeout for a MQTT client?

javamqtt

提问by Cristian

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:

我正在使用MQTTIA92 Java 实现,它允许我连接到 MQTT 代理。为了建立连接,我正在做这样的事情:

// 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.

我需要做的是手动设置超时,而不是让 mqtt 客户端决定。文档说:还有设置MQ遥测传输连接属性的方法,例如超时和重试。

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.

但是,老实说,我还没有发现任何关于它的信息。我查看了整个 javadoc 参考,没有超时配置的证据。我看不到源代码,因为它不是开源的。

So how can I set the timeout for the Mqtt connection?

那么如何设置 Mqtt 连接的超时时间呢?

回答by Yanish Pradhananga

If you have confusion you can go to MqttConnectionOptions for detail.

如果您有任何困惑,可以前往 MqttConnectionOptions 了解详细信息。

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

回答by ralight

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?

我对 ia92 一无所知,但我认为 connect() 调用中的 666 是您试图将超时设置为的内容?

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.

文档所指的超时可能是 keepalive 超时。这是在服务器和客户端之间没有通信的情况下可以经过的最大秒数(由客户端选择)。我想这是你最感兴趣的。

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.

另一方面,重试最有可能指的是在发送 QoS>0 的消息时重试似乎误入歧途的消息。不过,这将由客户端库代码处理,而不是由代理处理。这是只有在您连接后才能发挥作用的东西,所以我非常怀疑这是您的问题。

To be sure that the keepalive timeout is being set correctly, I'd try pointing your client at a modified mosquittobroker. 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!

为了确保 keepalive 超时设置正确,我会尝试将您的客户端指向一个修改过的mosquitto代理。您可以修改 src/read_handle_server.c 中的 mqtt3_handle_connect() 以打印出连接时的 keepalive 值。这将确保它按照您的想法行事,但恐怕无助于解决实际问题!

回答by Alpay

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

你用什么经纪人?Really Small Message Broker V1.1 Alpha,Mosquitto,IBM WebSphere 自带的broker?您需要在服务器配置中设置此超时值。因为系统就是这样工作的。您在代理中设置了一个保持活动值,并在该间隔到期之前从客户端发送 ping,以免代理关闭客户端-服务器连接,然后进程重新启动。实际上,即使该间隔到期,服务器仍不会关闭连接,直到“宽限期”结束。请参阅http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#connect