当前位置:网站首页>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();
}
}
}
边栏推荐
- C language: selective sorting method
- pso和mfpso
- Esp32 tutorial (I): vscode+platform and vscade+esp-idf
- Yum to see which installation package provides a command
- C. Recover an RBS(括号序列,思维)
- LabVIEW master VI freeze pending
- Billiard simulator based on the integration of GL pipeline and ray tracing technology
- Nautilus 3.19.2为Gnome增添动力
- What if the computer desktop gets stuck? Introduction of solutions to computer crash and desktop jamming
- How can I open and view the bin file? Diagram of reading method of bin file backed up by router
猜你喜欢

Merge sort

Kingbase v8r6 cluster installation and deployment case - script online one click capacity reduction

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

Chapter 1 regression, classification & clustering

一次线上事故,我顿悟了异步的精髓

A hospital call system based on C language

OWA dynamic password SMS authentication scheme solves the problem of outlook email two factor authentication

Uniapp learning

Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life

Esp32 tutorial (I): vscode+platform and vscade+esp-idf
随机推荐
在一线城市上班的程序员,家庭一般是怎样的?
OWA dynamic password SMS authentication scheme solves the problem of outlook email two factor authentication
What if the computer desktop gets stuck? Introduction of solutions to computer crash and desktop jamming
How to register and apply for free for Apple Developer account in order to enjoy the upgrade experience at the first time
Excel cell formula - realize Ackerman function calculation
Array force buckle (continuously updated)
PSO and mfpso
口叫SC 或者 pb 文件为读写控制ensor为
Want to know how a C program is compiled—— Show you the compilation of the program
Design of high frequency small signal resonant amplifier course design Multisim Simulation
To 3mm; Provide safe and stable product execution according to the sender IID
格式问题处理
greatest common divisor
后 SQL 时代降临:EdgeDB 2.0 发布会预告
Problems and solutions of QT (online installation package) crash in win10 installation
最大公约数
Summary of common errors in wechat applet cloud development
黑色的的一站式运维管家 10条记录RO
Journey of little black leetcode: 341. Flattening nested list iterator
Little black leetcode journey: 100 same trees