我正在使用MQTT Eclipse Paho Java Library(JAR)来连接到MQTT代理。
但我希望使用相同的库连接两个代理。我已经实现了代码连接两个代理,它确实连接但问题是在某个时候连接后随机断开(连接中的任何一个)。
那么使用一个MQTT客户端库连接两个MQTT Broker的最佳方法是什么。
更新
我的连接代码如下所示:
import org.eclipse.paho.client.mqttv3.MqttClient;
Class com.test.A
{
MqttClient mMqttClient;
A()
{
mMqttClient = new MqttClient("broker_url_1", "Client1", persistence);
// Create MQTT connection options
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true); // Create new clear session
mMqttClient.connect(connOpts);
}
}
import org.eclipse.paho.client.mqttv3.MqttClient;
Class com.test.B
{
MqttClient mMqttClient;
B()
{
mMqttClient = new MqttClient("broker_url_2", "Client2", persistence);
// Create MQTT connection options
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true); // Create new clear session
mMqttClient.connect(connOpts);
}
}
连接批次connectionLost: cause: Connection lost
答案 0 :(得分:1)
要解决丢失连接问题,请尝试启用自动重新连接功能:
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true); // Create new clear session
connOpts.setAutomaticReconnect(true); // add this line
要获取详细的错误消息,请创建jsr47min.properties文件并:
mMqttClient = new MqttClient("broker_url_1", "Client1", persistence);
Debug debug = mMqttClient.getDebug();
debug.dumpClientDebug(); // call at different points in your code?