From 48abdcc0bb55bb7e46ed20704ec156da846d3eab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E8=9C=97=E7=89=9B?=
<1003271829@qq.com>
Date: Sun, 24 Dec 2023 13:25:52 +0800
Subject: [PATCH] =?UTF-8?q?=E6=8E=92=E9=98=9F=E5=AF=BC=E5=87=BA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../paiduiexport/paiduiexport/pom.xml | 77 +++++++++++++++
.../demo/DemoRequestScopeApplication.java | 14 +++
.../example/demo/config/ThreadPoolConfig.java | 30 ++++++
.../example/demo/export/AbstractExport.java | 93 +++++++++++++++++++
.../example/demo/export/ExportController.java | 40 ++++++++
.../com/example/demo/export/ExportImpl.java | 58 ++++++++++++
.../com/example/demo/export/ExportQueue.java | 60 ++++++++++++
.../com/example/demo/export/ExportUser.java | 8 ++
.../src/main/resources/application.properties | 3 +
9 files changed, 383 insertions(+)
create mode 100644 排队导出/paiduiexport/paiduiexport/pom.xml
create mode 100644 排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/DemoRequestScopeApplication.java
create mode 100644 排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/config/ThreadPoolConfig.java
create mode 100644 排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/AbstractExport.java
create mode 100644 排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportController.java
create mode 100644 排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportImpl.java
create mode 100644 排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportQueue.java
create mode 100644 排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportUser.java
create mode 100644 排队导出/paiduiexport/paiduiexport/src/main/resources/application.properties
diff --git a/排队导出/paiduiexport/paiduiexport/pom.xml b/排队导出/paiduiexport/paiduiexport/pom.xml
new file mode 100644
index 0000000..c820cb8
--- /dev/null
+++ b/排队导出/paiduiexport/paiduiexport/pom.xml
@@ -0,0 +1,77 @@
+
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.7.4
+
+
+ com.example
+ demo-request-scope
+ 0.0.1-SNAPSHOT
+ demo-request-scope
+ Demo project for Spring Boot
+
+ 8
+
+
+
+
+
+
+ com.google.guava
+ guava
+ 30.1-jre
+
+
+
+ org.springframework.boot
+ spring-boot-starter-aop
+
+
+ org.springframework.boot
+ spring-boot-starter-data-redis
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ cn.hutool
+ hutool-all
+ 5.8.16
+
+
+
+ com.alibaba
+ easyexcel
+ 3.3.2
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+ org.projectlombok
+ lombok
+ true
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
diff --git a/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/DemoRequestScopeApplication.java b/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/DemoRequestScopeApplication.java
new file mode 100644
index 0000000..0da2cb2
--- /dev/null
+++ b/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/DemoRequestScopeApplication.java
@@ -0,0 +1,14 @@
+package com.example.demo;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+
+@SpringBootApplication
+public class DemoRequestScopeApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(DemoRequestScopeApplication.class, args);
+ }
+
+}
diff --git a/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/config/ThreadPoolConfig.java b/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/config/ThreadPoolConfig.java
new file mode 100644
index 0000000..db07f79
--- /dev/null
+++ b/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/config/ThreadPoolConfig.java
@@ -0,0 +1,30 @@
+package com.example.demo.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+@Configuration
+public class ThreadPoolConfig {
+
+ private static final int corePoolSize = 10; // 核心线程数
+ private static final int maximumPoolSize = 20; // 最大线程数
+ private static final long keepAliveTime = 60L; // 空闲线程存活时间
+ private static final TimeUnit TIME_UNIT = TimeUnit.SECONDS;
+
+ @Bean
+ public ThreadPoolExecutor getThreadPoolExecutor() {
+ return new ThreadPoolExecutor(
+ corePoolSize,
+ maximumPoolSize,
+ keepAliveTime,
+ TIME_UNIT,
+ new ArrayBlockingQueue<>(2000),
+ new ThreadPoolExecutor.AbortPolicy()
+ );
+ }
+
+}
diff --git a/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/AbstractExport.java b/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/AbstractExport.java
new file mode 100644
index 0000000..1767d0b
--- /dev/null
+++ b/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/AbstractExport.java
@@ -0,0 +1,93 @@
+package com.example.demo.export;
+
+
+
+import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.util.PageUtil;
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.ExcelWriter;
+import com.alibaba.excel.write.metadata.WriteSheet;
+import lombok.extern.slf4j.Slf4j;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.util.List;
+
+@Slf4j
+public abstract class AbstractExport {
+
+
+ public abstract void export(ExportUser sysUser) throws InterruptedException;
+
+ /**
+ * 导出
+ * @param response 输出流
+ * @param pageSize 每页大小
+ * @param t 导出条件
+ * @param k Excel内容实体类
+ * @param fileName 文件名称
+ */
+ public void export(HttpServletResponse response, int pageSize, T t, Class k, String fileName) throws Exception {
+ ExcelWriter writer = null;
+ try {
+ writer = getExcelWriter(response, fileName);
+ //查询导出总条数
+ int total = this.countExport(t);
+ //页数
+ int loopCount = PageUtil.totalPage(total, pageSize);
+ BeanUtil.setProperty(t, "pageSize", pageSize);
+ for (int i = 0; i < loopCount; i++) {
+ //开始页
+ BeanUtil.setProperty(t, "pageNum", PageUtil.getStart(i + 1, pageSize));
+ //获取Excel导出信息
+ List kList = this.getExportDetail(t);
+ WriteSheet writeSheet = EasyExcel.writerSheet(fileName).head(k).build();
+ writer.write(kList, writeSheet);
+ }
+ } catch (Exception e) {
+ String msg = "导出" + fileName + "异常";
+ log.error(msg, e);
+ throw new Exception(msg + e);
+ } finally {
+ if (writer != null) {
+ writer.finish();
+ }
+ }
+ }
+
+ public ExcelWriter getExcelWriter(HttpServletResponse response, String fileName) throws IOException {
+ response.setContentType("application/vnd.ms-excel");
+ response.setCharacterEncoding("utf-8");
+ // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
+ String fileNameUtf = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
+ response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileNameUtf + ".xlsx");
+ return EasyExcel.write(response.getOutputStream()).build();
+ }
+
+
+ /**
+ * (模版导出)
+ *
+ * @param t
+ * @param fileName
+ * @param response
+ */
+ public abstract void complexFillWithTable(T t, String fileName, HttpServletResponse response);
+
+ /**
+ * 查询导出总条数
+ *
+ * @param t
+ * @return
+ */
+ public abstract int countExport(T t);
+
+ /**
+ * 查询导出数据
+ *
+ * @param t
+ * @return
+ */
+ public abstract List getExportDetail(T t);
+}
diff --git a/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportController.java b/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportController.java
new file mode 100644
index 0000000..51738e6
--- /dev/null
+++ b/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportController.java
@@ -0,0 +1,40 @@
+package com.example.demo.export;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.concurrent.ThreadPoolExecutor;
+
+@RestController
+@RequestMapping("/export")
+@Slf4j
+public class ExportController {
+
+ @Autowired
+ private ExportImpl export;
+
+ @Autowired
+ private ThreadPoolExecutor threadPoolExecutor;
+
+ /**
+ * 实现一个排队导出功能
+ */
+ @PostMapping("/exportFile")
+ public void exportFile() {
+ log.info("提交线程池任务");
+ threadPoolExecutor.execute(() -> {
+ ExportUser sysUser = new ExportUser();
+ sysUser.setUserName(Thread.currentThread().getName());
+ try {
+ export.export(sysUser);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ );
+ log.info("提交线程池任务结束");
+ }
+}
diff --git a/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportImpl.java b/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportImpl.java
new file mode 100644
index 0000000..2165b71
--- /dev/null
+++ b/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportImpl.java
@@ -0,0 +1,58 @@
+package com.example.demo.export;
+
+import com.alibaba.excel.ExcelWriter;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.LinkedList;
+import java.util.List;
+
+@Service
+@Slf4j
+public class ExportImpl extends AbstractExport {
+
+ @Autowired
+ private ExportQueue exportQueue;
+
+ @Override
+ public void export(ExportUser sysUser) throws InterruptedException {
+ //导出
+ log.info("导出文件方法执行~~~~~~~~~");
+ LinkedList queue = exportQueue.add(sysUser);
+ //可以考虑把队列进行持久化
+ log.info("导出队列:" + queue);
+ //export(response,pageSize,t,k,fileName);
+ //导出成功后移除当前导出用户
+ ExportUser nextSysUser = exportQueue.getNextSysUser();
+ log.info("移除后获取下一个排队的用户: " + nextSysUser.getUserName());
+ }
+
+ @Override
+ public void export(HttpServletResponse response, int pageSize, Object o, Class k, String fileName) throws Exception {
+ super.export(response, pageSize, o, k, fileName);
+ }
+
+ @Override
+ public ExcelWriter getExcelWriter(HttpServletResponse response, String fileName) throws IOException {
+ return super.getExcelWriter(response, fileName);
+ }
+
+ @Override
+ public void complexFillWithTable(Object o, String fileName, HttpServletResponse response) {
+
+ }
+
+ @Override
+ public int countExport(Object o) {
+ return 0;
+ }
+
+ @Override
+ public List getExportDetail(Object o) {
+ return null;
+ }
+}
+
diff --git a/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportQueue.java b/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportQueue.java
new file mode 100644
index 0000000..1c7cb4d
--- /dev/null
+++ b/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportQueue.java
@@ -0,0 +1,60 @@
+package com.example.demo.export;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import java.util.LinkedList;
+
+@Slf4j
+@Component
+public class ExportQueue {
+
+
+ private final int MAX_CAPACITY = 10; // 队列最大容量
+ private LinkedList queue; // 用户队列 或者阻塞队列 或者 直接消息队列
+
+ public ExportQueue(LinkedList queue) {
+ this.queue = new LinkedList<>();
+ }
+
+ /**
+ * 排队队列添加
+ *
+ * @param sysUser
+ */
+ public synchronized LinkedList add(ExportUser sysUser) {
+ while (queue.size() >= MAX_CAPACITY) {
+ try {
+ log.info("当前排队人已满,请等待");
+ wait();
+ } catch (InterruptedException e) {
+ e.getMessage();
+ }
+ }
+ queue.add(sysUser);
+ log.info("目前导出队列排队人数:" + queue.size());
+ notifyAll();
+ return queue;
+ }
+
+
+ /**
+ * 获取排队队列下一个人
+ *
+ * @return
+ */
+ public synchronized ExportUser getNextSysUser() {
+ while (queue.isEmpty()) {
+ try {
+ wait();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ ExportUser sysUser = queue.remove();
+ notifyAll(); //唤醒
+ return sysUser;
+ }
+}
+
+
diff --git a/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportUser.java b/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportUser.java
new file mode 100644
index 0000000..9bed106
--- /dev/null
+++ b/排队导出/paiduiexport/paiduiexport/src/main/java/com/example/demo/export/ExportUser.java
@@ -0,0 +1,8 @@
+package com.example.demo.export;
+
+import lombok.Data;
+
+@Data
+public class ExportUser {
+ private String userName;
+}
diff --git a/排队导出/paiduiexport/paiduiexport/src/main/resources/application.properties b/排队导出/paiduiexport/paiduiexport/src/main/resources/application.properties
new file mode 100644
index 0000000..1b0e4b6
--- /dev/null
+++ b/排队导出/paiduiexport/paiduiexport/src/main/resources/application.properties
@@ -0,0 +1,3 @@
+
+spring.redis.host=192.168.56.10
+spring.redis.password=123456
\ No newline at end of file