当前位置:网站首页>Emqx simple to use
Emqx simple to use
2022-07-24 05:00:00 【Star age Cao Botao】
Import MAVEN rely on
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.2</version>
</dependency>
Write the message publisher
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();
}
}
}
Edit message subscriber
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();
}
}
}
边栏推荐
- The difference between run and start in thread class
- Quick reference manual for the strongest collation of common regular expressions (glory Collection Edition)
- Ben, reducing online importance is the same. Abnormal instance CP operation found
- OWA dynamic password SMS authentication scheme solves the problem of outlook email two factor authentication
- greatest common divisor
- Black one-stop operation and maintenance housekeeper 10 records ro
- How to make the words on the screen larger (setting method to make the text more comfortable on the large screen)
- Format problem handling
- 排序——QuickSort
- 连接数%的准确率。现在拟需求。企业在数足以
猜你喜欢

Esp32 tutorial (I): vscode+platform and vscade+esp-idf

Event extraction and documentation (2019)

MapReduce介绍

Event extraction and documentation (2020-2021)

Array force buckle (continuously updated)

Sort - quicksort

What programmer is still being grabbed by the company at the age of 35? Breaking the "middle-aged crisis" of programmers

MapReduce concept

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

Airiot Q & A issue 5 | how to use low code business flow engine?
随机推荐
Threejs+shader drawing commonly used graphics
几种常见的排序
How to play the Microsoft twin tool twinsonot? Introduction to twin test tool twinornot
How can I open and view the bin file? Diagram of reading method of bin file backed up by router
PHP修改配置文件的两种方法
Two methods of modifying configuration files in PHP
472-82 (22, 165, 39, sword finger offer II 078, 48. Rotate image)
Event extraction and documentation (2020-2021)
招聘| 嵌入式軟件(单片机)工程师
C. Recover an RBS(括号序列,思维)
All sections need to be able to have a problem system port, the first subscript. Many machines become
yum 查看某个命令由哪个安装包提供
Array force buckle (continuously updated)
How can e-commerce projects solve the over issuance of online coupons (troubleshooting + Solutions) (glory Collection)
What is the sandbox technology in the data anti disclosure scheme?
C. Recover an RBS (parenthesis sequence, thinking)
激活函数和最常用的10个激活函数
Little black gnawing leetcode:589. Preorder traversal of n-ary tree
Yolov7 -- brief introduction of the paper
How to download vscode using domestic image seconds