【发布时间】:2018-04-02 08:46:45
【问题描述】:
我正在 thingstream 中处理 mqtt 客户端。当我尝试使用 qos = 0 将消息发布到主题时,消息已发送,但程序仅挂在那里。然后我尝试使用 qos = 1 发送消息。消息发送成功,程序也没有挂起。但是在发送另一条消息时,我收到中断异常。谁能帮我吗。
下面是我用来发布消息的sn-p。
MqttMessage message = new MqttMessage(command);
message.setPayload(command);
message.setQos(1);
mqttClient.publish(topic, message);
System.out.println("Message published");
使用的库 - org.eclipse.paho.client.mqttv3-1.2.0
mqtt客户端初始化代码
public void connect() {
try {
mqttClient = new MqttClient(serverUri, clientId);
//mqttClient.setTimeToWait(10000);
} catch (MqttException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mqttClient.setCallback(new MqttCallbackExtended() {
@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
// TODO Auto-generated method stub
System.out.println("messageArrived: " + topic.toString());
System.out.println(mqttMessage.toString());
System.out.println(mqttMessage.getPayload());
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
// TODO Auto-generated method stub
System.out.println("deliveryComplete: " + arg0.getMessageId());
}
@Override
public void connectionLost(Throwable arg0) {
// TODO Auto-generated method stub
System.out.println("---Connection lost1");
// Toast.makeText(App.getContext(), "Connection
// lost",Toast.LENGTH_SHORT).show();
}
@Override
public void connectComplete(boolean arg0, String arg1) {
// TODO Auto-generated method stub
System.out.println("connectComplete");
try {
mqttClient.subscribe("device/+/publish");
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
commands.sendCommand(Commands.GET_STATUS, null, "device/identity:85111741-5789-3010-85c9-be4a7204e5d3");
} catch (MqttException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setAutomaticReconnect(true);
mqttConnectOptions.setCleanSession(true);
mqttConnectOptions.setUserName(username);
mqttConnectOptions.setPassword(password.toCharArray());
mqttConnectOptions.setKeepAliveInterval(30);
mqttConnectOptions.setConnectionTimeout(60);
mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
try {
mqttClient.connect(mqttConnectOptions);
} catch (MqttSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
【问题讨论】: