当前位置:网站首页>EMQX 简单使用
EMQX 简单使用
2022-07-24 04:52:00 【星时代曹波涛】
导入MAVEN依赖
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.2</version>
</dependency>
编写消息发布者
package com.scsoul.emqx;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
String broker = "tcp://localhost:1883";
String clientId = "user01";
//Use the memory persistence
MemoryPersistence persistence = new MemoryPersistence();
try {
MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
System.out.println("Connecting to broker:" + broker);
sampleClient.connect(connOpts);
System.out.println("Connected");
String topic = "fire/topics";
String content = "";
int qos = 2;
while (true) {
Scanner scanner = new Scanner(System.in);
content = scanner.nextLine();
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qos);
sampleClient.publish(topic, message);
System.out.println("Message published");
}
} catch (MqttException me) {
System.out.println("reason" + me.getReasonCode());
System.out.println("msg" + me.getMessage());
System.out.println("loc" + me.getLocalizedMessage());
System.out.println("cause" + me.getCause());
System.out.println("excep" + me);
me.printStackTrace();
}
}
}
编辑消息订阅者
package com.scsoul.emqx;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.text.MessageFormat;
public class App2 {
public static void main(String[] args) {
String broker = "tcp://localhost:1883";
String clientId = "user02";
//Use the memory persistence
MemoryPersistence persistence = new MemoryPersistence();
try {
MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
System.out.println("Connecting to broker:" + broker);
sampleClient.connect(connOpts);
System.out.println("Connected");
sampleClient.subscribe("fire/topics");
sampleClient.setCallback(new MqttCallback() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println(new String(message.getPayload()));
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
@Override
public void connectionLost(Throwable throwable) {
}
});
} catch (MqttException me) {
System.out.println("reason" + me.getReasonCode());
System.out.println("msg" + me.getMessage());
System.out.println("loc" + me.getLocalizedMessage());
System.out.println("cause" + me.getCause());
System.out.println("excep" + me);
me.printStackTrace();
}
}
}
边栏推荐
- How to play the Microsoft twin tool twinsonot? Introduction to twin test tool twinornot
- Teacher qiniu said is the VIP account opened for you safe?
- 基于C语言设计的一个医院叫号系统
- LabVIEW master VI freeze pending
- Journey of little black leetcode: 590. Post order traversal of n-ary tree
- Sort - quicksort
- Personalized customized TenPay name customized TenPay is a diagram of any name such as Ma Huateng
- Leetcode 46 full arrangement
- Pony activation tool appears cannot open file k:\oemsf solution
- The x-fkgom supporting the GOM engine key.lic is authorized to start
猜你喜欢

A hospital call system based on C language

Design of two power dividers and directional couplers for basic experiment of microwave technology

Chapter 1 regression, classification & clustering

Web3 product manager's Guide: how to face the encryption world

链接预测中训练集、验证集以及测试集的划分(以PyG的RandomLinkSplit为例)

Kingbase V8R6集群安装部署案例---脚本在线一键扩容

激活函数和最常用的10个激活函数

How can e-commerce projects solve the over issuance of online coupons (troubleshooting + Solutions) (glory Collection)

How to solve the engine prompt alias herodb and game engine startup exceptions?

pycharm 调试功能介绍与使用
随机推荐
Logback log framework technology in project development
Chapter 9 using image data
Activation functions and the 10 most commonly used activation functions
招聘| 嵌入式軟件(单片机)工程师
Pony activation tool appears cannot open file k:\oemsf solution
[network counting experiment report] Cisco LAN Simulation and simple network test
12306 the most wonderful verification code in history: normal users can easily identify the ticket grabbing software and are rejected
Esp32:arduino tutorial summary
-Bash: wget: command not found
uniapp学习
微波技术基础实验二 功分器与定向耦合器设计
Determined by hardware (see official 2 and
The x-fkgom supporting the GOM engine key.lic is authorized to start
京东方高级副总裁姜幸群:AIoT技术赋能企业物联网转型
基于C语言设计的一个医院叫号系统
Baidu wallet helps you repay the inter-bank repayment of your credit card. The handling fee is 0. Newcomers who arrive in real time will be rewarded with 5 yuan
C. Recover an RBS(括号序列,思维)
Sort - quicksort
Leetcode 46 full arrangement
本,降低线上要度是一样的。发现异常实例cp操