commit b4afdf762ac733b787e9cf0e1c6981e0b4e4f635 Author: DongZeLiang <2746733890@qq.com> Date: Mon Nov 6 19:33:24 2023 +0800 初始化 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c72de14 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +/.idea/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..1094b8f --- /dev/null +++ b/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + com.muyu + mqtt + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + org.eclipse.paho + org.eclipse.paho.client.mqttv3 + 1.2.5 + + + + org.projectlombok + lombok + 1.18.28 + + + + diff --git a/src/main/java/com/muyu/mqtt/PublishSample.java b/src/main/java/com/muyu/mqtt/PublishSample.java new file mode 100644 index 0000000..99bdce6 --- /dev/null +++ b/src/main/java/com/muyu/mqtt/PublishSample.java @@ -0,0 +1,70 @@ +package com.muyu.mqtt; + +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.eclipse.paho.client.mqttv3.MqttMessage; +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.concurrent.CountDownLatch; + +public class PublishSample { + + private static CountDownLatch countDownLatch = new CountDownLatch(10); + + public static void main (String[] args) { + + String broker = "tcp://192.168.40.128:1883"; + String topic = "test"; + String username = "emqx"; + String password = "public"; + String clientId = "publish_client"; + int qos = 0; + + try { + MqttClient client = new MqttClient(broker, clientId, new MemoryPersistence()); + // 连接参数 + MqttConnectOptions options = new MqttConnectOptions(); + // 设置用户名和密码 +// options.setUserName(username); +// options.setPassword(password.toCharArray()); + options.setConnectionTimeout(60); + options.setKeepAliveInterval(60); + // 连接 + client.connect(options); + new Thread(() -> { + for (int i = 0 ; i < 10 ; i++) { + SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); + String content = sdf4.format(new Date()) + " - Hello MQTT"; + // 创建消息并设置 QoS + MqttMessage message = new MqttMessage(content.getBytes()); + message.setQos(qos); + // 发布消息 + try { + client.publish(topic, message); + } catch (MqttException e) { + throw new RuntimeException(e); + } + System.out.println("Message published"); + System.out.println("topic: " + topic); + System.out.println("message content: " + content); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + countDownLatch.countDown(); + } + }).start(); + countDownLatch.await(); + // 关闭连接 + client.disconnect(); + // 关闭客户端 + client.close(); + } catch (MqttException | InterruptedException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/com/muyu/mqtt/SubscribeSample.java b/src/main/java/com/muyu/mqtt/SubscribeSample.java new file mode 100644 index 0000000..9a31e2c --- /dev/null +++ b/src/main/java/com/muyu/mqtt/SubscribeSample.java @@ -0,0 +1,51 @@ +package com.muyu.mqtt; + +import org.eclipse.paho.client.mqttv3.*; +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; + +public class SubscribeSample { + public static void main (String[] args) { + String broker = "tcp://192.168.40.128:1883"; + String topic = "test"; + String username = "emqx"; + String password = "public"; + String clientid = "subscribe_client"; + int qos = 0; + + try { + MqttClient client = new MqttClient(broker, clientid, new MemoryPersistence()); + // 连接参数 + MqttConnectOptions options = new MqttConnectOptions(); +// options.setUserName(username); +// options.setPassword(password.toCharArray()); + options.setConnectionTimeout(60); + options.setKeepAliveInterval(60); + // 设置回调 + client.setCallback(new MqttCallback() { + + public void connectionLost (Throwable cause) { + System.out.println("connectionLost: " + cause.getMessage()); + } + + public void messageArrived (String topic, MqttMessage message) { + System.out.println("topic: " + topic); + System.out.println("Qos: " + message.getQos()); + System.out.println("message content: " + new String(message.getPayload())); + + } + + public void deliveryComplete (IMqttDeliveryToken token) { + System.out.println("deliveryComplete---------" + token.isComplete()); + } + + }); + client.connect(options); + client.subscribe(topic, qos); + Thread.sleep(10000); + client.disconnect(); + client.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/muyu/mqtt/Test1.java b/src/main/java/com/muyu/mqtt/Test1.java new file mode 100644 index 0000000..0e0907a --- /dev/null +++ b/src/main/java/com/muyu/mqtt/Test1.java @@ -0,0 +1,9 @@ +package com.muyu.mqtt; + +/** + * @author DongZl + * @description: 测试1 + * @Date 2023-11-6 下午 04:15 + */ +public class Test1 { +}