From 3fc61eb9d89a3a58eed2b70a7be2f86955f19c3c Mon Sep 17 00:00:00 2001
From: lijiayao <13831655+xiao-yao-charge-forward@user.noreply.gitee.com>
Date: Sun, 24 Mar 2024 16:53:55 +0800
Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 33 ++++++
pom.xml | 106 ++++++++++++++++++
.../java/com/example/DemoApplication.java | 13 +++
.../example/demos/web/BasicController.java | 67 +++++++++++
.../example/demos/web/MQTT/PublishSample.java | 47 ++++++++
.../com/example/demos/web/MQTT/SSLUtils.java | 74 ++++++++++++
.../demos/web/MQTT/SubscribeSample.java | 51 +++++++++
.../demos/web/PathVariableController.java | 44 ++++++++
.../java/com/example/demos/web/Service.java | 78 +++++++++++++
src/main/java/com/example/demos/web/Test.java | 44 ++++++++
src/main/java/com/example/demos/web/User.java | 43 +++++++
.../com/example/demos/web/UserService.java | 44 ++++++++
src/main/resources/application.properties | 3 +
src/main/resources/static/index.html | 6 +
.../com/example/DemoApplicationTests.java | 13 +++
15 files changed, 666 insertions(+)
create mode 100644 .gitignore
create mode 100644 pom.xml
create mode 100644 src/main/java/com/example/DemoApplication.java
create mode 100644 src/main/java/com/example/demos/web/BasicController.java
create mode 100644 src/main/java/com/example/demos/web/MQTT/PublishSample.java
create mode 100644 src/main/java/com/example/demos/web/MQTT/SSLUtils.java
create mode 100644 src/main/java/com/example/demos/web/MQTT/SubscribeSample.java
create mode 100644 src/main/java/com/example/demos/web/PathVariableController.java
create mode 100644 src/main/java/com/example/demos/web/Service.java
create mode 100644 src/main/java/com/example/demos/web/Test.java
create mode 100644 src/main/java/com/example/demos/web/User.java
create mode 100644 src/main/java/com/example/demos/web/UserService.java
create mode 100644 src/main/resources/application.properties
create mode 100644 src/main/resources/static/index.html
create mode 100644 src/test/java/com/example/DemoApplicationTests.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..f01ff37
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,106 @@
+
+
+ 4.0.0
+ com.example
+ demo
+ 0.0.1-SNAPSHOT
+ demo
+ Demo project for Spring Boot
+
+ 11
+ UTF-8
+ UTF-8
+ 2.3.12.RELEASE
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.junit.vintage
+ junit-vintage-engine
+
+
+
+
+ javax.servlet
+ javax.servlet-api
+ 3.1.0
+
+
+ org.springframework.cloud
+ spring-cloud-starter-bootstrap
+ 3.1.7
+
+
+
+ org.projectlombok
+ lombok
+ 1.18.22
+
+
+ org.eclipse.paho
+ org.eclipse.paho.client.mqttv3
+ 1.2.5
+
+
+
+ org.bouncycastle
+ bcpkix-jdk15on
+ 1.70
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-dependencies
+ ${spring-boot.version}
+ pom
+ import
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.8.1
+
+ 11
+ 11
+ UTF-8
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+ ${spring-boot.version}
+
+ com.example.DemoApplication
+ true
+
+
+
+ repackage
+
+ repackage
+
+
+
+
+
+
+
+
diff --git a/src/main/java/com/example/DemoApplication.java b/src/main/java/com/example/DemoApplication.java
new file mode 100644
index 0000000..909a1ec
--- /dev/null
+++ b/src/main/java/com/example/DemoApplication.java
@@ -0,0 +1,13 @@
+package com.example;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class DemoApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(DemoApplication.class, args);
+ }
+
+}
diff --git a/src/main/java/com/example/demos/web/BasicController.java b/src/main/java/com/example/demos/web/BasicController.java
new file mode 100644
index 0000000..7a927b8
--- /dev/null
+++ b/src/main/java/com/example/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.example.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/example/demos/web/MQTT/PublishSample.java b/src/main/java/com/example/demos/web/MQTT/PublishSample.java
new file mode 100644
index 0000000..91171c2
--- /dev/null
+++ b/src/main/java/com/example/demos/web/MQTT/PublishSample.java
@@ -0,0 +1,47 @@
+package com.example.demos.web.MQTT;
+
+import lombok.extern.log4j.Log4j2;
+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;
+
+/**
+ * @Author: LiJiaYao
+ * @Date: 2024/3/20
+ */
+@Log4j2
+public class PublishSample {
+ public static void main(String[] args) throws Exception {
+ String broker = "tcp://broker.emqx.io:1883";
+ String topic = "mqtt/test";
+ String username = "emqx";
+ String password = "public";
+ String clientid = "publish_client";
+ String content = "Hello MQTT";
+ int qos = 0;
+
+ 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);
+ //创建消息并设置Qos
+ MqttMessage message = new MqttMessage(content.getBytes());
+ message.setQos(qos);
+ client.publish(topic,message);
+ log.info("Message published");
+ System.out.println("topic "+topic);
+ System.out.println("message content "+content);
+ client.disconnect();
+ client.close();
+ }
+}
diff --git a/src/main/java/com/example/demos/web/MQTT/SSLUtils.java b/src/main/java/com/example/demos/web/MQTT/SSLUtils.java
new file mode 100644
index 0000000..7ee18f6
--- /dev/null
+++ b/src/main/java/com/example/demos/web/MQTT/SSLUtils.java
@@ -0,0 +1,74 @@
+package com.example.demos.web.MQTT;
+
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.openssl.PEMKeyPair;
+import org.bouncycastle.openssl.PEMParser;
+import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
+
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSocketFactory;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.*;
+import java.security.*;
+import java.security.cert.CertificateException;
+import java.security.cert.CertificateFactory;
+import java.security.cert.X509Certificate;
+
+/**
+ * @Author: LiJiaYao
+ * @Date: 2024/3/20
+ */
+public class SSLUtils {
+
+ public static SSLSocketFactory getSocketFactory(final String caCrtFile, final String crtFile, final String keyFile) throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyManagementException {
+
+ String broker = "tcp://broker.emqx.io:1883";
+ // TLS/SSL
+// String broker = "ssl://broker.emqx.io:8883";
+ String username = "emqx";
+ String password = "public";
+ String clientid = "publish_client";
+
+ Security.addProvider(new BouncyCastleProvider());
+ //load CA certificate
+
+ X509Certificate caCert = null;
+ FileInputStream fis = new FileInputStream(crtFile);
+ BufferedInputStream bis = new BufferedInputStream(fis);
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+
+ while (bis.available() > 0) {
+ caCert = (X509Certificate) cf.generateCertificate(bis);
+ }
+ bis = new BufferedInputStream(new FileInputStream(crtFile));
+ X509Certificate cert = null;
+ while (bis.available() > 0) {
+ cert = (X509Certificate) cf.generateCertificate(bis);
+ }
+
+ PEMParser pemParser = new PEMParser(new FileReader(keyFile));
+ Object object = pemParser.readObject();
+ JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
+ KeyPair keyPair = converter.getKeyPair((PEMKeyPair) object);
+ pemParser.close();
+
+ KeyStore caKs = KeyStore.getInstance(KeyStore.getDefaultType());
+
+ caKs.load(null, null);
+ caKs.setCertificateEntry("ca-certificate", caCert);
+ TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
+ tmf.init(caKs);
+
+ KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
+ ks.load(null, null);
+ ks.setCertificateEntry("certificate", cert);
+ ks.setKeyEntry("private-key", keyPair.getPrivate(), password.toCharArray(), new java.security.cert.Certificate[]{cert});
+ KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+ kmf.init(ks, password.toCharArray());
+ SSLContext context = SSLContext.getInstance("TLSv1.2");
+ context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
+ return context.getSocketFactory();
+ }
+
+}
diff --git a/src/main/java/com/example/demos/web/MQTT/SubscribeSample.java b/src/main/java/com/example/demos/web/MQTT/SubscribeSample.java
new file mode 100644
index 0000000..9bdcf20
--- /dev/null
+++ b/src/main/java/com/example/demos/web/MQTT/SubscribeSample.java
@@ -0,0 +1,51 @@
+package com.example.demos.web.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://broker.emqx.io: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();
+ }
+ }
+}
diff --git a/src/main/java/com/example/demos/web/PathVariableController.java b/src/main/java/com/example/demos/web/PathVariableController.java
new file mode 100644
index 0000000..09cf9f7
--- /dev/null
+++ b/src/main/java/com/example/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.example.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/example/demos/web/Service.java b/src/main/java/com/example/demos/web/Service.java
new file mode 100644
index 0000000..7b425fc
--- /dev/null
+++ b/src/main/java/com/example/demos/web/Service.java
@@ -0,0 +1,78 @@
+package com.example.demos.web;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.*;
+
+/**
+ * @author DongXiaoDong
+ * @version 1.0
+ * @date 2024/3/20 9:47
+ * @description
+ */
+public class Service {
+ /**
+ * 线程池优化
+ */
+ private final ExecutorService executorService;
+
+ /**
+ * 构造函数,初始化线程池
+ */
+ public Service() {
+ this.executorService = Executors.newFixedThreadPool(1);
+ }
+
+ /**
+ * 优化后的线程池
+ * @param userIds
+ * @return
+ */
+ public Map get(List userIds) {
+ Map resultMap = new ConcurrentHashMap<>();
+ List> batches = splitIntoBatches(userIds, 1000);
+
+
+ List>> tasks = new ArrayList<>();
+ for (List batch : batches) {
+ tasks.add(() -> UserService.getUserMap(batch));
+ }
+
+ try {
+ List>> futures = executorService.invokeAll(tasks);
+
+ for (Future