我们在项目中使用mosquitto将IoT设备连接到云。
但是,当只有30台设备(服务器具有3.1 CPU和8G RAM)时,总是会出现“正在进行的发布太多”错误。
我们尝试将qos设置为0,1,2。但是,没有一个有效。
任何人都可以提出一些建议来解决它吗?
发布消息
public static void publishMessage(MqttPubMsg config) {
String clientId = MqttClient.generateClientId();
MemoryPersistence persistence = new MemoryPersistence();
MqttClient sampleClient = new MqttClient(config.getBroker(), clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
connOpts.setUserName(config.getUsername());
connOpts.setPassword(config.getPassword().toCharArray());
connOpts.setConnectionTimeout(mqttConnectTimeout);
connOpts.setKeepAliveInterval(mqttKeppAliveInterval);
sampleClient.connect(connOpts);
MqttMessage message = new MqttMessage(config.getContent());
message.setQos(0);
sampleClient.publish(config.getTopic(), message);
System.out.println("Message published");
}
mosquitto.conf
max_inflight_messages 0
max_queued_messages 0
max_connections -1
订阅消息
public static void subscribeMessage(MqttSubMsg config) {
System.out.println(config.getBroker());
String clientId = MqttClient.generateClientId();
MemoryPersistence persistence = new MemoryPersistence();
MqttClient sampleClient = new MqttClient(config.getBroker(), clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
connOpts.setUserName(config.getUsername());
connOpts.setPassword(config.getPassword().toCharArray());
connOpts.setConnectionTimeout(mqttConnectTimeout);
connOpts.setKeepAliveInterval(mqttKeppAliveInterval);
System.out.println("run receive...");
sampleClient.setCallback(new controllers.PushCallback());
MqttTopic mtopic = sampleClient.getTopic(config.getTopic());
connOpts.setWill(mtopic, "close".getBytes(), 0, true);
sampleClient.connect(connOpts);
int[] Qos = {0};
String[] topic1 = {config.getTopic()};
sampleClient.subscribe(topic1, Qos);
}
答案 0 :(得分:1)
这与mosquitto无关,它是您正在使用的客户端。查看此答案:Send many publish message: Too many publishes in progress Error
答案 1 :(得分:1)
让我展示如何自己解决此类问题-
打开Paho客户端的Github网页并搜索“正在进行的发布太多”:
这将导致您找到“ 32202”字符串,然后再次搜索。
这会导致您(跳过一些本地化文件之后)到达Java源代码文件MqttException.java中的常量:
public static final short REASON_CODE_MAX_INFLIGHT = 32202;
再次搜索“ REASON_CODE_MAX_INFLIGHT”-最终将您带到文件ClientState.java:
if (actualInFlight >= this.maxInflight) {
//@TRACE 613= sending {0} msgs at max inflight window
log.fine(CLASS_NAME, methodName, "613", new Object[]{new Integer(actualInFlight)});
throw new MqttException(MqttException.REASON_CODE_MAX_INFLIGHT);
}
因此,您需要调整maxInflight
属性。
再没有其他搜索,您发现可以通过对传递给Paho客户程序的setMaxInflight(int maxInflight)
方法的MqttConnectionOptions对象调用connect
方法来设置搜索结果。>