From eea14955bc786f431d9521ca59529d35578eabc3 Mon Sep 17 00:00:00 2001
From: rouchen <3133657697@qq.com>
Date: Wed, 29 May 2024 22:44:56 +0800
Subject: [PATCH] =?UTF-8?q?feat=20=E5=AE=A2=E6=88=B7=E7=AB=AF=E5=88=9B?=
=?UTF-8?q?=E5=BB=BAMQTT=20=E4=B8=BB=E9=A2=98=20=E5=92=8C=E6=B6=88?=
=?UTF-8?q?=E6=81=AF=20=E5=AE=9E=E7=8E=B0=E6=B6=88=E6=81=AF=E7=9A=84?=
=?UTF-8?q?=E5=8F=91=E9=80=81=E5=92=8C=E6=8E=A5=E5=8F=97=20=20=E6=B5=8B?=
=?UTF-8?q?=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 33 +++++++
pom.xml | 94 +++++++++++++++++++
src/main/java/com/muyu/MqttApplication.java | 13 +++
.../com/muyu/demos/web/BasicController.java | 67 +++++++++++++
.../demos/web/PathVariableController.java | 44 +++++++++
src/main/java/com/muyu/demos/web/User.java | 43 +++++++++
.../com/muyu/mqtt/MessageCallbackService.java | 34 +++++++
src/main/java/com/muyu/mqtt/MqttFactory.java | 47 ++++++++++
.../java/com/muyu/mqtt/MqttProperties.java | 42 +++++++++
src/main/java/com/muyu/mqtt/MsgHandler.java | 33 +++++++
src/main/java/com/muyu/mqtt/RabbitConfig.java | 37 ++++++++
.../java/com/muyu/mqtt/SubscribeSample.java | 55 +++++++++++
src/main/resources/application.yml | 8 ++
src/main/resources/static/index.html | 6 ++
.../java/com/muyu/MqttApplicationTests.java | 13 +++
15 files changed, 569 insertions(+)
create mode 100644 .gitignore
create mode 100644 pom.xml
create mode 100644 src/main/java/com/muyu/MqttApplication.java
create mode 100644 src/main/java/com/muyu/demos/web/BasicController.java
create mode 100644 src/main/java/com/muyu/demos/web/PathVariableController.java
create mode 100644 src/main/java/com/muyu/demos/web/User.java
create mode 100644 src/main/java/com/muyu/mqtt/MessageCallbackService.java
create mode 100644 src/main/java/com/muyu/mqtt/MqttFactory.java
create mode 100644 src/main/java/com/muyu/mqtt/MqttProperties.java
create mode 100644 src/main/java/com/muyu/mqtt/MsgHandler.java
create mode 100644 src/main/java/com/muyu/mqtt/RabbitConfig.java
create mode 100644 src/main/java/com/muyu/mqtt/SubscribeSample.java
create mode 100644 src/main/resources/application.yml
create mode 100644 src/main/resources/static/index.html
create mode 100644 src/test/java/com/muyu/MqttApplicationTests.java
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..549e00a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,33 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..114bd1a
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,94 @@
+
+
+ 4.0.0
+ com.muyu
+ mqtt
+ 0.0.1-SNAPSHOT
+ mqtt
+ Demo project for Spring Boot
+
+ 17
+ UTF-8
+ UTF-8
+ 2.6.13
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ org.projectlombok
+ lombok
+ 1.18.32
+ provided
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.eclipse.paho
+ org.eclipse.paho.client.mqttv3
+ 1.2.5
+
+
+ org.apache.commons
+ commons-lang3
+ 3.12.0
+
+
+
+ org.springframework.boot
+ spring-boot-starter-amqp
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-dependencies
+ ${spring-boot.version}
+ pom
+ import
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.8.1
+
+ 17
+ 17
+ UTF-8
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+ ${spring-boot.version}
+
+ com.muyu.MqttApplication
+ true
+
+
+
+ repackage
+
+ repackage
+
+
+
+
+
+
+
+
diff --git a/src/main/java/com/muyu/MqttApplication.java b/src/main/java/com/muyu/MqttApplication.java
new file mode 100644
index 0000000..cca93cf
--- /dev/null
+++ b/src/main/java/com/muyu/MqttApplication.java
@@ -0,0 +1,13 @@
+package com.muyu;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class MqttApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(MqttApplication.class, args);
+ }
+
+}
diff --git a/src/main/java/com/muyu/demos/web/BasicController.java b/src/main/java/com/muyu/demos/web/BasicController.java
new file mode 100644
index 0000000..65e8908
--- /dev/null
+++ b/src/main/java/com/muyu/demos/web/BasicController.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2013-2018 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.muyu.demos.web;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+/**
+ * @author theonefx
+ */
+@Controller
+public class BasicController {
+
+ // http://127.0.0.1:8080/hello?name=lisi
+ @RequestMapping("/hello")
+ @ResponseBody
+ public String hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
+ return "Hello " + name;
+ }
+
+ // http://127.0.0.1:8080/user
+ @RequestMapping("/user")
+ @ResponseBody
+ public User user() {
+ User user = new User();
+ user.setName("theonefx");
+ user.setAge(666);
+ return user;
+ }
+
+ // http://127.0.0.1:8080/save_user?name=newName&age=11
+ @RequestMapping("/save_user")
+ @ResponseBody
+ public String saveUser(User u) {
+ return "user will save: name=" + u.getName() + ", age=" + u.getAge();
+ }
+
+ // http://127.0.0.1:8080/html
+ @RequestMapping("/html")
+ public String html(){
+ return "index.html";
+ }
+
+ @ModelAttribute
+ public void parseUser(@RequestParam(name = "name", defaultValue = "unknown user") String name
+ , @RequestParam(name = "age", defaultValue = "12") Integer age, User user) {
+ user.setName("zhangsan");
+ user.setAge(18);
+ }
+}
diff --git a/src/main/java/com/muyu/demos/web/PathVariableController.java b/src/main/java/com/muyu/demos/web/PathVariableController.java
new file mode 100644
index 0000000..e3024a9
--- /dev/null
+++ b/src/main/java/com/muyu/demos/web/PathVariableController.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2013-2018 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.muyu.demos.web;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+/**
+ * @author theonefx
+ */
+@Controller
+public class PathVariableController {
+
+ // http://127.0.0.1:8080/user/123/roles/222
+ @RequestMapping(value = "/user/{userId}/roles/{roleId}", method = RequestMethod.GET)
+ @ResponseBody
+ public String getLogin(@PathVariable("userId") String userId, @PathVariable("roleId") String roleId) {
+ return "User Id : " + userId + " Role Id : " + roleId;
+ }
+
+ // http://127.0.0.1:8080/javabeat/somewords
+ @RequestMapping(value = "/javabeat/{regexp1:[a-z-]+}", method = RequestMethod.GET)
+ @ResponseBody
+ public String getRegExp(@PathVariable("regexp1") String regexp1) {
+ return "URI Part : " + regexp1;
+ }
+}
diff --git a/src/main/java/com/muyu/demos/web/User.java b/src/main/java/com/muyu/demos/web/User.java
new file mode 100644
index 0000000..8e07afb
--- /dev/null
+++ b/src/main/java/com/muyu/demos/web/User.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2013-2018 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.muyu.demos.web;
+
+/**
+ * @author theonefx
+ */
+public class User {
+
+ private String name;
+
+ private Integer age;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public Integer getAge() {
+ return age;
+ }
+
+ public void setAge(Integer age) {
+ this.age = age;
+ }
+}
diff --git a/src/main/java/com/muyu/mqtt/MessageCallbackService.java b/src/main/java/com/muyu/mqtt/MessageCallbackService.java
new file mode 100644
index 0000000..3fa3fa3
--- /dev/null
+++ b/src/main/java/com/muyu/mqtt/MessageCallbackService.java
@@ -0,0 +1,34 @@
+package com.muyu.mqtt;
+
+import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
+import org.eclipse.paho.client.mqttv3.MqttCallback;
+import org.eclipse.paho.client.mqttv3.MqttMessage;
+import org.springframework.stereotype.Service;
+
+/**
+ * 回执消息类 MessageCallbackService
+ *
+ * @author Yangle
+ * Date 2024/5/29 20:24
+ */
+@Service
+public class MessageCallbackService implements MqttCallback {
+
+ @Override
+ public void connectionLost(Throwable cause) {
+ System.out.println("connectionLost: " + cause.getMessage());
+ }
+ @Override
+ 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()));
+
+ }
+
+ @Override
+ public void deliveryComplete(IMqttDeliveryToken token) {
+ System.out.println("deliveryComplete---------" + token.isComplete());
+ }
+
+}
diff --git a/src/main/java/com/muyu/mqtt/MqttFactory.java b/src/main/java/com/muyu/mqtt/MqttFactory.java
new file mode 100644
index 0000000..e706010
--- /dev/null
+++ b/src/main/java/com/muyu/mqtt/MqttFactory.java
@@ -0,0 +1,47 @@
+package com.muyu.mqtt;
+
+import lombok.AllArgsConstructor;
+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.persist.MemoryPersistence;
+import org.springframework.stereotype.Service;
+
+/**
+ * mqtt配置 MqttConfig
+ *
+ * @author Yangle
+ * Date 2024/5/29 20:26
+ */
+@Service
+@AllArgsConstructor
+public class MqttFactory {
+
+ private final MessageCallbackService messageCallbackService;
+ public MqttClient creatClient(MqttProperties mqttProperties) {
+ MqttClient client = null;
+ try {
+ client = new MqttClient(mqttProperties.getBroker(), mqttProperties.getClientid(), new MemoryPersistence());
+ MqttConnectOptions options = new MqttConnectOptions();
+
+ // 连接参数
+ if (mqttProperties.isLong())
+ {
+ options.setUserName(mqttProperties.getUsername());
+ options.setPassword(mqttProperties.getPassword().toCharArray());
+ }
+
+
+ options.setConnectionTimeout(60);
+ options.setKeepAliveInterval(60);
+ client.connect(options);
+ client.setCallback(messageCallbackService);
+ client.subscribe(mqttProperties.getTopic(),0);
+
+ } catch (MqttException e) {
+ throw new RuntimeException(e);
+ }
+
+ return client;
+ }
+}
diff --git a/src/main/java/com/muyu/mqtt/MqttProperties.java b/src/main/java/com/muyu/mqtt/MqttProperties.java
new file mode 100644
index 0000000..8e2eabc
--- /dev/null
+++ b/src/main/java/com/muyu/mqtt/MqttProperties.java
@@ -0,0 +1,42 @@
+package com.muyu.mqtt;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.experimental.SuperBuilder;
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.UUID;
+
+/**
+ * 配置文件 MqttProperties
+ *
+ * @author Yangle
+ * Date 2024/5/29 20:06
+ */
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@SuperBuilder
+public class MqttProperties {
+
+ private String broker;
+ private String topic ;
+ private String username;
+ private String password;
+ private String clientid;
+
+ public static MqttProperties configBuild(String ip,String topic){
+ return MqttProperties.builder()
+ .broker("tcp://"+ip+":1883")
+ .topic(topic)
+ .clientid("protocol-parsing")
+ .build();
+ }
+
+ public boolean isLong(){
+ return !StringUtils.isBlank(this.username) && !StringUtils.isBlank(this.password);
+ }
+
+
+}
diff --git a/src/main/java/com/muyu/mqtt/MsgHandler.java b/src/main/java/com/muyu/mqtt/MsgHandler.java
new file mode 100644
index 0000000..25a101f
--- /dev/null
+++ b/src/main/java/com/muyu/mqtt/MsgHandler.java
@@ -0,0 +1,33 @@
+package com.muyu.mqtt;
+
+import lombok.extern.log4j.Log4j2;
+import org.eclipse.paho.client.mqttv3.MqttClient;
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * 消息处理器 MsgHandler
+ *
+ * @author Yangle
+ * Date 2024/5/29 20:44
+ */
+@Log4j2
+@Component
+public class MsgHandler {
+
+ @Autowired
+ private MqttFactory mqttFactory;
+ @RabbitListener(queues = "create.topic")
+ public void msg(String msg){
+ System.out.println("接收到消息:" + msg);
+ MqttProperties mqttProperties = MqttProperties.configBuild(
+ "47.102.133.88",
+ "mqtt/test"
+ );
+ log.error("接收到消息初始化信息:{}",mqttProperties);
+ MqttClient mqttClient = mqttFactory.creatClient(mqttProperties);
+ log.error("client创建成功:{}",mqttClient.getClientId());
+
+ }
+}
diff --git a/src/main/java/com/muyu/mqtt/RabbitConfig.java b/src/main/java/com/muyu/mqtt/RabbitConfig.java
new file mode 100644
index 0000000..8b96256
--- /dev/null
+++ b/src/main/java/com/muyu/mqtt/RabbitConfig.java
@@ -0,0 +1,37 @@
+package com.muyu.mqtt;
+
+import org.springframework.amqp.core.Binding;
+import org.springframework.amqp.core.BindingBuilder;
+import org.springframework.amqp.core.DirectExchange;
+import org.springframework.amqp.core.Queue;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * RabbitConfig
+ *
+ * @author Yangle
+ * Date 2024/5/29 20:44
+ */
+@Configuration
+public class RabbitConfig {
+
+
+ @Bean
+ public Queue autoDeleteQueue1() {
+ return new Queue("create.topic", true);
+ }
+
+ @Bean
+ public DirectExchange directExchange() {
+ return new DirectExchange("topic.direct");
+ }
+
+ @Bean
+ public Binding binding(DirectExchange directExchange,
+ Queue autoDeleteQueue1 ) {
+ return BindingBuilder.bind(autoDeleteQueue1)
+ .to(directExchange)
+ .with("protocol-parsing");
+ }
+}
\ No newline at end of file
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..6a56545
--- /dev/null
+++ b/src/main/java/com/muyu/mqtt/SubscribeSample.java
@@ -0,0 +1,55 @@
+package com.muyu.mqtt;
+
+import org.eclipse.paho.client.mqttv3.*;
+import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class SubscribeSample {
+ public static void main(String[] args) {
+
+ String broker = "tcp://47.102.133.88:1883";
+ String topic = "mqtt/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() {
+
+ @Override
+ public void connectionLost(Throwable cause) {
+ System.out.println("connectionLost: " + cause.getMessage());
+ }
+ @Override
+ 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()));
+
+ }
+
+ @Override
+ public void deliveryComplete(IMqttDeliveryToken token) {
+ System.out.println("deliveryComplete---------" + token.isComplete());
+ }
+
+ });
+ client.connect(options);
+ client.subscribe(topic, qos);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ SpringApplication.run(SubscribeSample.class, args);
+ }
+}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
new file mode 100644
index 0000000..a97104b
--- /dev/null
+++ b/src/main/resources/application.yml
@@ -0,0 +1,8 @@
+# 应用服务 WEB 访问端口4
+server: 808
+
+spring:
+ application:
+ name: protocol-parsing
+ rabbitmq:
+ host: 115.159.211.196
diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html
new file mode 100644
index 0000000..e2d94a2
--- /dev/null
+++ b/src/main/resources/static/index.html
@@ -0,0 +1,6 @@
+
+
+ hello word!!!
+ this is a html page
+
+
\ No newline at end of file
diff --git a/src/test/java/com/muyu/MqttApplicationTests.java b/src/test/java/com/muyu/MqttApplicationTests.java
new file mode 100644
index 0000000..762c9d5
--- /dev/null
+++ b/src/test/java/com/muyu/MqttApplicationTests.java
@@ -0,0 +1,13 @@
+package com.muyu;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+class MqttApplicationTests {
+
+ @Test
+ void contextLoads() {
+ }
+
+}