一起聊聊基于隊(duì)列實(shí)現(xiàn)多人同時(shí)導(dǎo)出 Excel
作者:一安
其余的還未實(shí)現(xiàn),導(dǎo)出文件的表的設(shè)計(jì)、oss文件上傳、用戶導(dǎo)出文件下載,還有高并發(fā)的場(chǎng)景下會(huì)不會(huì)出現(xiàn)什么問(wèn)題,這些都還沒有太考慮進(jìn)去; 實(shí)現(xiàn)的方式應(yīng)該挺多的,Redis的隊(duì)列應(yīng)該也是可以的,這里僅僅提供一個(gè)實(shí)現(xiàn)思路。
前言
業(yè)務(wù)關(guān)系定義
分別是用戶、導(dǎo)出隊(duì)列、導(dǎo)出執(zhí)行方法
- ExportQueue: 維護(hù)一條定長(zhǎng)隊(duì)列,可以獲取隊(duì)列里前后排隊(duì)的用戶,提供查詢,隊(duì)列如果已經(jīng)滿了,其余的人需要進(jìn)行等待
- User信息: 排隊(duì)執(zhí)行導(dǎo)出方法對(duì)應(yīng)用戶;
- Export類: 定義導(dǎo)出方法,異步執(zhí)行,用戶可以通過(guò)導(dǎo)出頁(yè)面查看、下載,導(dǎo)出的文件;
圖片
具體代碼實(shí)現(xiàn)
ExportQueue隊(duì)列
package com.example.system.config;
import com.example.system.api.domain.ExportUser;
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; // 隊(duì)列最大容量
private LinkedList<ExportUser> queue; // 用戶隊(duì)列
public ExportQueue(LinkedList<ExportUser> queue) {
this.queue = new LinkedList<>();
}
/**
* 排隊(duì)隊(duì)列添加
* @param sysUser
*/
public synchronized LinkedList<ExportUser> add(ExportUser sysUser) {
while (queue.size() >= MAX_CAPACITY) {
try {
log.info("當(dāng)前排隊(duì)人已滿,請(qǐng)等待");
wait();
} catch (InterruptedException e) {
e.getMessage();
}
}
queue.add(sysUser);
log.info("目前導(dǎo)出隊(duì)列排隊(duì)人數(shù):" + queue.size());
notifyAll();
return queue;
}
/**
* 獲取排隊(duì)隊(duì)列下一個(gè)人
* @return
*/
public synchronized ExportUser getNextSysUser() {
while (queue.isEmpty()) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
ExportUser sysUser = queue.remove();
notifyAll(); //喚醒
return sysUser;
}
}
AbstractExport導(dǎo)出類
引入EasyExcel百萬(wàn)級(jí)別的導(dǎo)出功能
package com.example.system.config;
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 com.example.system.api.domain.ExportUser;
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<T, K> {
public abstract void export(ExportUser sysUser) throws InterruptedException;
/**
* 導(dǎo)出
*
* @param response 輸出流
* @param pageSize 每頁(yè)大小
* @param t 導(dǎo)出條件
* @param k Excel內(nèi)容實(shí)體類
* @param fileName 文件名稱
*/
public void export(HttpServletResponse response, int pageSize, T t, Class<K> k, String fileName) throws Exception {
ExcelWriter writer = null;
try {
writer = getExcelWriter(response, fileName);
//查詢導(dǎo)出總條數(shù)
int total = this.countExport(t);
//頁(yè)數(shù)
int loopCount = PageUtil.totalPage(total, pageSize);
BeanUtil.setProperty(t, "pageSize", pageSize);
for (int i = 0; i < loopCount; i++) {
//開始頁(yè)
BeanUtil.setProperty(t, "pageNum", PageUtil.getStart(i + 1, pageSize));
//獲取Excel導(dǎo)出信息
List<K> kList = this.getExportDetail(t);
WriteSheet writeSheet = EasyExcel.writerSheet(fileName).head(k).build();
writer.write(kList, writeSheet);
}
} catch (Exception e) {
String msg = "導(dǎo)出" + fileName + "異常";
log.error(msg, e);
throw new Exception(msg + e);
} finally {
if (writer != null) {
writer.finish();
}
}
}
public com.alibaba.excel.ExcelWriter getExcelWriter(HttpServletResponse response, String fileName) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 這里URLEncoder.encode可以防止中文亂碼 當(dāng)然和easyexcel沒有關(guān)系
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();
}
/**
* (模版導(dǎo)出)
*
* @param t
* @param fileName
* @param response
*/
public abstract void complexFillWithTable(T t, String fileName, HttpServletResponse response);
/**
* 查詢導(dǎo)出總條數(shù)
*
* @param t
* @return
*/
public abstract int countExport(T t);
/**
* 查詢導(dǎo)出數(shù)據(jù)
*
* @param t
* @return
*/
public abstract List<K> getExportDetail(T t);
}
ExportImpl導(dǎo)出實(shí)現(xiàn)方法
package com.example.system.service.impl;
import com.alibaba.excel.ExcelWriter;
import com.example.system.api.domain.ExportUser;
import com.example.system.config.AbstractExport;
import com.example.system.config.ExportQueue;
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 {
//導(dǎo)出
log.info("導(dǎo)出文件方法執(zhí)行~~~~~~~~~");
// export(response,pageSize,t,k,fileName);
LinkedList<ExportUser> queue = exportQueue.add(sysUser);
log.info("導(dǎo)出隊(duì)列:" + queue);
//休眠時(shí)間稍微設(shè)置大點(diǎn),模擬導(dǎo)出處理時(shí)間
Thread.sleep(20000);
//導(dǎo)出成功后移除當(dāng)前導(dǎo)出用戶
ExportUser nextSysUser = exportQueue.getNextSysUser();
log.info("移除后獲取下一個(gè)排隊(duì)的用戶: " + 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;
}
}
測(cè)試controller
package com.example.system.controller;
import com.example.system.api.domain.ExportUser;
import com.example.system.api.domain.SysUser;
import com.example.system.service.impl.ExportImpl;
import lombok.SneakyThrows;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/export")
@Slf4j
public class ExportController {
@Autowired
private ExportImpl export;
@PostMapping("/exportFile")
public void exportFile() {
new Thread(new Runnable() {
@SneakyThrows
@Override
public void run() {
Thread thread1 = Thread.currentThread();
ExportUser sysUser =new ExportUser();
sysUser.setUserName(thread1.getName());
export.export(sysUser);
}
}).start();
}
}
測(cè)試結(jié)果
通過(guò)請(qǐng)求測(cè)試方法,限制了我們導(dǎo)出隊(duì)列最大限制10次,隊(duì)列場(chǎng)長(zhǎng)度超過(guò)10次則無(wú)法進(jìn)行繼續(xù)提交;
圖片
第一次請(qǐng)求和第二次請(qǐng)求,間隔10秒,第一個(gè)用戶導(dǎo)出完成后出列,下一個(gè)排隊(duì)用戶在隊(duì)列首位,再進(jìn)行導(dǎo)出請(qǐng)求排在上一個(gè)用戶后面;
圖片
總結(jié)
責(zé)任編輯:武曉燕
來(lái)源:
一安未來(lái)