feat 客户端创建MQTT 主题 和消息 实现消息的发送和接受 测试
commit
eea14955bc
|
@ -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/
|
|
@ -0,0 +1,94 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>mqtt</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>mqtt</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<spring-boot.version>2.6.13</spring-boot.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!-- MQTT-->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.32</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
<version>1.2.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.12.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>17</source>
|
||||
<target>17</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<configuration>
|
||||
<mainClass>com.muyu.MqttApplication</mainClass>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>repackage</id>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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 <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
|
||||
*/
|
||||
@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);
|
||||
}
|
||||
}
|
|
@ -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 <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
}
|
|
@ -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 <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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());
|
||||
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
# 应用服务 WEB 访问端口4
|
||||
server: 808
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: protocol-parsing
|
||||
rabbitmq:
|
||||
host: 115.159.211.196
|
|
@ -0,0 +1,6 @@
|
|||
<html>
|
||||
<body>
|
||||
<h1>hello word!!!</h1>
|
||||
<p>this is a html page</p>
|
||||
</body>
|
||||
</html>
|
|
@ -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() {
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue