我有一个Java MQTT订阅客户端,它使用订阅的消息作为输入来更新SQLite数据库中的2个表。表格,数据和查询都是轻量级且简单的UPDATE
和INSERT
操作。这就是我所看到的:
messageArrived
回调启动数据库操作。connectionLost
回调。当用System.out.println
替换数据库操作时,行为是正常的。我试图摆弄连接选项中的keepAlive
值,认为数据库操作可能会影响PINGREQ
和PINGRESP
(即使客户端处于'后台')。< / p>
你能否建议,为什么客户失去联系?
Java MQTT客户端:
public class wSync implements MqttCallback {
MqttClient w;
private String BROKER_URL = "";
private String PROTOCOL = "tcp://";
private String PORT = "1883";
private String TOPIC_ROOT_UNCNF = "/P/uncnf/";
private String TOPIC_ROOT_CNF = "/P/cnf/";
private int KEEP_ALIVE = 180;
private we WE = new WE();
public static void main(String[] args) {
wSync wsy = new wSync();
processTxns(args[0]);
}
public void setBrokerUrl() {
this.BROKER_URL = PROTOCOL + System.getenv("BROKER_DNS") + ":" + PORT;
}
public String getBrokerUrl() {
return this.BROKER_URL;
}
public void processTxns(String wName) {
this.setBrokerUrl();
String broker = this.getBrokerUrl();
String clientId = wName + "-SUB";
String topic = TOPIC_ROOT_CNF + wName + "/CR";
MemoryPersistence persistence = new MemoryPersistence();
try {
MqttConnectOptions c = new MqttConnectOptions();
c.setKeepAliveInterval(KEEP_ALIVE);
c.setCleanSession(false);
w = new MqttClient(broker, clientId, persistence);
w.connect(c);
w.setCallback(this);
w.subscribe(topic, 2);
System.out.println(w.getServerURI() + " " + w.getClientId() + " " + w.isConnected());
} catch (MqttException e) {
e.printStackTrace();
}
}
@Override
public void connectionLost(Throwable arg0) {
System.out.println("Connection lost at : " + new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss").format(new java.util.Date()));
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
// TODO Auto-generated method stub
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
String s = new String(message.getPayload());
we.addSeed(s);
}
}
更新
由于我不确定,为什么断线发生,我决定处理它。毕竟,现场部署中的网络错误并非闻所未闻。所以,我重新写了connectionLost
回调:
@Override
public void connectionLost(Throwable arg0) {
System.out.println("A " + w.isConnected() + " " + new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss").format(new java.util.Date()));
try {
w.connect(mqttConn);
w.setCallback(this);
w.subscribe("/P/cnf/wA/CR", 2);
} catch (MqttSecurityException e) {
e.printStackTrace();
} catch (MqttException e) {
e.printStackTrace();
}
System.out.println("B " + w.isConnected() + " " + new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss").format(new java.util.Date()));
}
这似乎可以处理在主题上发布的消息的断开和接收。但是,如果留给自己(意味着没有收到出版物),我会看到以下错误消息:
champ@champ:~/bin$ java -jar w-sync.jar pull wA
* true 2017-02-19.23:21:00
{
"meta" : {"tStamp" : "current_Date"},
"data" :
[
{"txn" : "thingy,thingamajig,whatchamacallit"}
]
}
A false 2017-02-19.23:21:29
B true 2017-02-19.23:21:29
A false 2017-02-19.23:22:29
A false 2017-02-19.23:22:29
B true 2017-02-19.23:22:29
MqttException (0) - java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:171)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.removeElementAt(Vector.java:558)
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:137)
... 1 more
B true 2017-02-19.23:22:29
A false 2017-02-19.23:24:29
MqttException (0) - java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:171)
at java.lang.Thread.run(Thread.java:745)
A false 2017-02-19.23:24:29
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.removeElementAt(Vector.java:558)
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:137)
... 1 more
B false 2017-02-19.23:24:29
MqttException (0) - java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:171)
at java.lang.Thread.run(Thread.java:745)
A false 2017-02-19.23:24:29
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.removeElementAt(Vector.java:558)
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:137)
... 1 more
B false 2017-02-19.23:24:29
Client is not connected (32104)
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:27)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:132)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.subscribe(MqttAsyncClient.java:657)
at org.eclipse.paho.client.mqttv3.MqttClient.subscribe(MqttClient.java:282)
at org.eclipse.paho.client.mqttv3.MqttClient.subscribe(MqttClient.java:275)A false 2017-02-19.23:24:29
at com.rpk.demo.sync.wSync.connectionLost(wSync.java:109)
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.connectionLost(CommsCallback.java:229)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.shutdownConnection(ClientComms.java:339)
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:171)
at java.lang.Thread.run(Thread.java:745)
B false 2017-02-19.23:24:29
B true 2017-02-19.23:24:29