Java 示例
Introducing paho dependency
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.0</version>
</dependency>
Establishing an MQTT connection
When establishing a connection, you need to use AccessId, secret, topic, and clientId, which are all assigned by the system. AccessId corresponds to the client’s user, and secret corresponds to the client’s password.
String user = "u8vngl";//user
String password = "xxxxxx";//password(MD5-32)
String topic = "upload/{accessKey}/{deviceNum}/location";
//String topic = "upload/B176C0FF1DFF41D0126BFF7908C76E17/8041254836/location"; //Subscribe to the location data of this device
//String topic = "upload/B176C0FF1DFF41D0126BFF7908C76E17/+/location"; //Subscribe to the location data of this configuration device
//String topic = "upload/B176C0FF1DFF41D0126BFF7908C76E17/#"; //Subscribe to the location, alarm, event and other data of this configuration device
int qos = 1;//qos2 consumes more energy, please use 1 or 0
String broker = "mqtt://mqtt.assetscontrols.com:1883";//mqtt server address
String clientId = "u8vngl_consumer"; //User-defined client name
//When qos is 1 or 2, mqttclient uses
MemoryPersistence persistence = new MemoryPersistence();
try {
MqttClient client = new MqttClient(broker, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setUserName(user);
connOpts.setPassword(password.toCharArray());
//When cleanSession is false, the next time you log in with the same clientId, you will // be able to retrieve all stored messages
//If true, you will retrieve the last message marked as retained
//Set cleanSession to true during debugging and testing, and false during production
connOpts.setCleanSession(false);
connOpts.setAutomaticReconnect(true);//Set up automatic reconnection
client.setCallback(new MqttCallback());//Get subscription messages
client.connect(connOpts);
client.subscribe(topic, qos);//Subscribe the topic
} catch(MqttException me) {
me.printStackTrace();
}
Get subscription messages
Paho gets messages by implementing the MqttCallback interface, which gets messages through the messageArrived method
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
//Please put the processing of /message into other threads. If too much time is spent in this method, it will affect the response of qos 1 or 2, making the server think that the message is not successfully delivered.
System.out.println("topic:" + topic + " msg:" + message);
}
/**
* [Important Tips]
* Processing logic after connection loss
* 1、Reconnect
* 2、Resubscribe to Topic Data
* @param cause
*/
@Override
public void connectionLost(Throwable cause) {
cause.printStackTrace();
while (true) {
try {
//Reconnect
if (!client.isConnected()) {
client.reconnect();
}
//Resubscribe to Topic Data
if (client.isConnected()) {
client.subscribe(topic, qos);
System.out.println("has resubscribed");
break;
}
TimeUnit.SECONDS.sleep(100);
} catch (MqttException | InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("client not connect");
}
}
文档更新时间: 2025-03-15 15:07 作者:Jeson