Reputation: 12018
I am using MQTT Eclipse Paho Java Library (JAR) for connection to a MQTT broker.
But I want to connect with two brokers using the same library. I have implemented code to connect with two brokers, it does connect but problem is that after sometime connection randomly disconnects (Either one of the connection).
So what is the best way to connect with two MQTT Brokers using one MQTT client library.
Updated
My code for connection looks like below:
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);
}
}
Error That I am getting after connection lots connectionLost: cause: Connection lost
Upvotes: 1
Views: 2026
Reputation: 22988
To workaround losing connections try enabling automatic reconnect feature:
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true); // Create new clear session
connOpts.setAutomaticReconnect(true); // add this line
To get verbose error messages create the jsr47min.properties file and:
mMqttClient = new MqttClient("broker_url_1", "Client1", persistence);
Debug debug = mMqttClient.getDebug();
debug.dumpClientDebug(); // call at different points in your code?
Upvotes: 1