当前位置:网站首页>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 software cannot be uninstalled. Please wait for the current program to complete the uninstallation or change the solution
- 一文带你深入浅出C字符串函数和内存函数
- EMQX 简单使用
- How can I open and view the bin file? Diagram of reading method of bin file backed up by router
- 力扣146题:LRU缓存
- 链接预测中训练集、验证集以及测试集的划分(以PyG的RandomLinkSplit为例)
- The difference between statement and Preparedstatement and how to use placeholders
- Chapter VI more supervision training
- What if the notepad file is too large to open? Introduction to the solution to the phenomenon that txt files are too large to be opened
- Esp32:arduino tutorial summary
猜你喜欢

Web3 product manager's Guide: how to face the encryption world
![[cornerstone of high concurrency] multithreading, daemon thread, thread safety, thread synchronization, mutual exclusion](/img/24/16cfb44dde056f4b91cdb1de2e9566.png)
[cornerstone of high concurrency] multithreading, daemon thread, thread safety, thread synchronization, mutual exclusion

Activation functions and the 10 most commonly used activation functions

The second chapter is encog's data acquisition

MapReduce concept

Event extraction and documentation (2019)
![Rlib learning - [4] - algorithmconfig detailed introduction [pytoch version]](/img/1e/95078ad64a17686463547e8ba87cb1.png)
Rlib learning - [4] - algorithmconfig detailed introduction [pytoch version]

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

Common cross domain problems

Journey of little black leetcode: 341. Flattening nested list iterator
随机推荐
The difference between statement and Preparedstatement and how to use placeholders
How to make yourself look young in how old robot? How old do I look? Younger method skills
What if IPv4 has no internet access? Solutions to IPv4 without internet access rights (detailed explanation of pictures and texts)
【ARC127F】±AB
Recruitment | embedded software (MCU) Engineer
Sword finger offer special assault edition day 7
LabVIEW主VI冻结挂起
Question 146: LRU cache
本,降低线上要度是一样的。发现异常实例cp操
Introduction to MapReduce
C. Recover an RBS (parenthesis sequence, thinking)
pycharm 调试功能介绍与使用
微信朋友圈的高性能架构设计
Nautilus 3.19.2 adds momentum to Gnome
Illustration and text demonstrate the movable range of the applet movable view
Two methods of modifying configuration files in PHP
智能指针、左值引用右值引用、lambda表达式
招聘| 嵌入式軟件(单片机)工程师
[postgraduate entrance examination vocabulary training camp] day 10 - capital, expand, force, adapt, depand
MapReduce介绍