paho.mqtt.java
paho.mqtt.java copied to clipboard
v3 subscribers can receive offline messages, but v5 cannot
1: Start the subscribers implemented by v3 and v5 respectively
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.mqttv5.client</artifactId>
<version>1.2.5</version>
</dependency>
v5
import org.eclipse.paho.mqttv5.*
public class MqttSubscriberv5 {
public static void main(String[] args) {
String topic = "/test/topic0716";
String broker = "*****";
String clientId = "JavaSubSample0716v5";
try {
MqttConnectionOptions options = new MqttConnectionOptions();
options.setCleanStart(false);
options.setSessionExpiryInterval(0L);
options.setSocketFactory(SSLSocketBuilder.getSSLSocketFactory());
MqttClient mqttClient = new MqttClient(broker, clientId);
mqttClient.setCallback(new MqttCallback() {
@Override
public void disconnected(MqttDisconnectResponse disconnectResponse) {
System.out.println("==========disconnected");
}
@Override
public void mqttErrorOccurred(MqttException exception) {
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println(topic + ": --------" + message);
}
@Override
public void deliveryComplete(IMqttToken token) {
}
@Override
public void connectComplete(boolean reconnect, String serverURI) {
}
@Override
public void authPacketArrived(int reasonCode, MqttProperties properties) {
}
});
mqttClient.connect(options);
mqttClient.subscribe(topic, 1);
} catch (MqttException e) {
throw new RuntimeException(e);
}
}
}
v3
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;
public class SubscribeSamplev3 {
public static void main(String[] args) {
String topic = "/test/topic0716";
String broker = "********";
String clientId = "JavaSubSample0716v3";
try {
MqttClient client = new MqttClient(broker, clientId);
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(false);
options.setSocketFactory(SSLSocketBuilder.getSSLSocketFactory());
client.setCallback(new MqttCallback() {
public void connectionLost(Throwable cause) {
System.out.println("connectionLost subscribe: " + cause.getMessage());
}
public void messageArrived(String topic, MqttMessage message) {
System.out.println("topic subscribe: " + topic);
System.out.println("Qos subscribe: " + message.getQos());
System.out.println("message content subscribe: " + new String(message.getPayload()));
}
public void deliveryComplete(IMqttDeliveryToken token) {
System.out.println("deliveryComplete subscribe---------" + token.isComplete());
}
});
client.connect(options);
client.subscribe(topic, 1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2: Push a message.At this time, both v3 and v5 can receive the "hello world" message.
mosquitto_pub -h test.ota.autox.clu -p 38883 \
--cafile Root_CA.crt \
--cert client.crt \
--key client.key \
-t '*******' \
-q 1 \
-i testpub0716 \
-m "Hello World"
3: Stop the subscribers implemented by v3 and v5. And push a message again.
mosquitto_pub -h test.ota.autox.clu -p 38883 \
--cafile Root_CA.crt \
--cert client.crt \
--key client.key \
-t '*******' \
-q 1 \
-i testpub0716 \
-m "Hello World My God "
4: Start the subscribers implemented by v3 and v5 again. v3 can receive "Hello World My God " message sent in step 3, but v5 cannot