未在 Java Paho 库中使用 MqttAsyncClient 接收消息
Not receiving messages with MqttAsyncClient in Java Paho library
我有一个创建 MqttAsyncClient 实例并连接到远程服务器的新线程。连接后,客户端订阅特定主题。如果我使用 MqttClient 而不是 MqttAsyncClient,我会收到消息,但如果我使用 MqttAsyncClient,则不会收到任何消息。以下是我的代码,有人可以花点时间看看我是否遗漏或不正确吗?
public class MqttEventReceiver implements Runnable {
private static final String CLIENT_ID = UUID.randomUUID().toString();
private IMqttAsyncClient client = null;
public MqttEventReceiver(String apiStreamingUri, String
connectionAccessToken) {
}
private MqttCallback mqttCallback = new MqttCallback() {
public void messageArrived(String topic, MqttMessage message) throws Exception {
String incomingMsg = new String(message.getPayload());
LOG.info("Message: ", new String(payload));
}
public void deliveryComplete(IMqttDeliveryToken arg0) {
// TODO Auto-generated method stub
}
public void connectionLost(Throwable arg0) {
// TODO Auto-generated method stub
}
};
@Override
public void run() {
String tmpDir = System.getProperty("java.io.tmpdir");
MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir);
//make the connect request. this request establishes a permanent connection
MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true);
options.setCleanSession(true);
options.setConnectionTimeout(10);
options.setUserName("authorization");
options.setPassword(connectionAccessToken.toCharArray());
Long threadId = successfullyConnected();
client = new MqttAsyncClient(apiStreamingUri, CLIENT_ID, dataStore);
client.setCallback(mqttCallback);
client.connect(options).waitForCompletion();
client.subscribe("topic", 1).waitForCompletion();
}
}
原来是QoS设置导致消息传递缓慢。我将 QoS 设置为 0,消息被及时传递。
我有一个创建 MqttAsyncClient 实例并连接到远程服务器的新线程。连接后,客户端订阅特定主题。如果我使用 MqttClient 而不是 MqttAsyncClient,我会收到消息,但如果我使用 MqttAsyncClient,则不会收到任何消息。以下是我的代码,有人可以花点时间看看我是否遗漏或不正确吗?
public class MqttEventReceiver implements Runnable {
private static final String CLIENT_ID = UUID.randomUUID().toString();
private IMqttAsyncClient client = null;
public MqttEventReceiver(String apiStreamingUri, String
connectionAccessToken) {
}
private MqttCallback mqttCallback = new MqttCallback() {
public void messageArrived(String topic, MqttMessage message) throws Exception {
String incomingMsg = new String(message.getPayload());
LOG.info("Message: ", new String(payload));
}
public void deliveryComplete(IMqttDeliveryToken arg0) {
// TODO Auto-generated method stub
}
public void connectionLost(Throwable arg0) {
// TODO Auto-generated method stub
}
};
@Override
public void run() {
String tmpDir = System.getProperty("java.io.tmpdir");
MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir);
//make the connect request. this request establishes a permanent connection
MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true);
options.setCleanSession(true);
options.setConnectionTimeout(10);
options.setUserName("authorization");
options.setPassword(connectionAccessToken.toCharArray());
Long threadId = successfullyConnected();
client = new MqttAsyncClient(apiStreamingUri, CLIENT_ID, dataStore);
client.setCallback(mqttCallback);
client.connect(options).waitForCompletion();
client.subscribe("topic", 1).waitForCompletion();
}
}
原来是QoS设置导致消息传递缓慢。我将 QoS 设置为 0,消息被及时传递。