Java MQTT訂閱端,可訂閱萬用字元(/#)
Java實現MQTT訂閱端,可訂閱萬用字元(/#)
在做MQTT訂閱端時報錯:
The topic name MUST NOT contain any wildcard characters (#+)
提示Topic中不能包含萬用字元
- 主函式
PublishSubscribe.java
package Test;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.util.Map;
import java.util.UUID;
/**
* mqtt的釋出和訂閱
*
* @author wzq
*/
public class PublishSubscribe {
private static String serviceURI = "tcp://192.168.32.160:1883";
private static String clientID = UUID.randomUUID().toString();
private static MqttClientPersistence persistence = new MemoryPersistence();
//如果mqtt服務配置了匿名訪問,則不需要使用使用者名稱和密碼就可以實現訊息的訂閱和釋出
private static String username = "admin";
private static String password = "123456";
private static String topic = "capability/#";
/*
訊息服務質量,一共有三個:
0:盡力而為。訊息可能會丟,但絕不會重複傳輸
1:訊息絕不會丟,但可能會重複傳輸
2:恰好一次。每條訊息肯定會被傳輸一次且僅傳輸一次
*/
private static int qos = 0;
/**
* 訊息釋出
*
* @author wzq
**/
public static void publish() {
try {
MqttClient client = new MqttClient(serviceURI, clientID, persistence);
MqttConnectOptions connectOptions = new MqttConnectOptions();
// connectOptions.setUserName(username);
// connectOptions.setPassword(password.toCharArray());
connectOptions.setCleanSession(false);
//釋出者連線服務
client.connect(connectOptions);
System.out.println("釋出者連線狀態: " + client.isConnected());
MqttTopic mqttTopic = client.getTopic(topic);
//MqttMessage mqttMessage = new MqttMessage(message.getBytes());
MqttMessage mqttMessage = new MqttMessage();
mqttMessage.setQos(qos);
int i = 1;
String message = "hello,world-";
while (true) {
String payLoad = message + i++;
mqttMessage.setPayload(payLoad.getBytes());
MqttDeliveryToken deliveryToken = mqttTopic.publish(mqttMessage);
if (!deliveryToken.isComplete()) {
System.out.println("釋出者釋出訊息: " + payLoad + " 失敗");
deliveryToken.waitForCompletion();
} else {
System.out.println("釋出者釋出訊息: " + payLoad + " 成功");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 訊息訂閱
*
* @author wzq
**/
static Map<String, Object> map1;
public static Map<String, Object> subscribe() {
try {
MqttClient client = new MqttClient(serviceURI, clientID, persistence);
client.setCallback(new PushCallback());
MqttConnectOptions connectOptions = new MqttConnectOptions();
connectOptions.setUserName(username);
connectOptions.setPassword(password.toCharArray());
connectOptions.setCleanSession(false);
//訂閱者連線訂閱主題
client.connect(connectOptions);
client.subscribe(topic, qos);
System.out.println("訂閱者連線狀態: " + client.isConnected());
} catch (MqttException e) {
e.printStackTrace();
}
return map1;
}
public static void anay(String topic,String message){
System.out.println("topic:"+topic);
System.out.println("message:"+message);
String topic_content=topic;
String message_content=message;
}
}
- 回撥函式
PushCallback.java
package Test;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class PushCallback implements MqttCallback {
static int num=0;
@Override
public void connectionLost(Throwable arg0) {
// TODO Auto-generated method stub
System.out.println("訂閱者連線丟失...");
System.out.println(arg0.getMessage());
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
// TODO Auto-generated method stub
}
@Override
public void messageArrived(String arg0, MqttMessage arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("訂閱者接收到主題: " + arg0.toString());
System.out.println("訂閱者接收到訊息: " + arg1.toString());
anay(arg0.toString(), arg1.toString());
}
public static void anay(String topic,String message){
num++;
if(num>1){
JsonParser parser = new JsonParser() ;
JsonObject object=(JsonObject) parser.parse(message);
object.addProperty("topic", topic);
Client.array.add(object);
System.out.println(Client.array);
}
}
}
- 測試
client.java
package Test;
import java.util.Map;
import com.google.gson.JsonArray;
public class Client {
static JsonArray array = new JsonArray();
public static void main(String[] args) {
PublishSubscribe ps=new PublishSubscribe();
Map<String, Object> map=ps.subscribe();
}
}
相關文章
- mqtt訂閱和釋出MQQT
- MQTT 釋出/訂閱模式介紹MQQT模式
- MQTT-釋出與訂閱的報文MQQT
- openGauss 訂閱
- 如何使用Java訂閱RSS Feed?Java
- SQLServer 2012複製訂閱資料訂閱過程SQLServer
- 小心訂閱模式模式
- 訂閱制正酣?解析Google Play Pass、Apple Arcade 訂閱服務GoAPP
- 釋出訂閱EventEmitterMIT
- RabbitMQ-訂閱模式MQ模式
- 釋出訂閱模式模式
- openGauss 釋出訂閱
- Ocer訂閱定價
- iOS訂閱詳解iOS
- Redis釋出訂閱Redis
- php生成rss訂閱PHP
- 使用Java接入小程式訂閱訊息!Java
- 設計模式之釋出訂閱模式(2) Redis 釋出/訂閱模式設計模式Redis
- JS訂閱釋出模式JS模式
- 釋出訂閱管道化
- 微信小程式訂閱微信小程式
- openGauss-釋出訂閱
- Follow訂閱源認證
- LightDB訂閱和釋出
- Windows 10即將“被訂閱”:關於訂閱制的痛並快樂Windows
- HarmonyOS遠端狀態訂閱開發例項
- MQTT 客戶端出現連線訂閱等問題時如何排查?MQQT客戶端
- JMeter MQTT 在訂閱與釋出測試場景中的使用JMeterMQQT
- laravel 監聽mqtt 實現釋出 訂閱 哪個包穩定?LaravelMQQT
- 谷歌音樂應用訂閱使用者已經突破1500萬谷歌
- Javascript(七)釋出-訂閱模式JavaScript模式
- 釋出訂閱模式學習模式
- 【譯】Angular自動取消訂閱Angular
- 聊聊rocketmq的訂閱關係MQ
- 淺談graphql的訂閱模式模式
- Rxjava2解析-訂閱流程RxJava
- Laravel Redis釋出與訂閱.LaravelRedis
- Redis 的訂閱與釋出Redis