//伪代码,省略了一些步骤 @Slf4j public abstract class AbstractUploadService<T> { public static ThreadFactory commonThreadFactory = new ThreadFactoryBuilder().setNameFormat("-upload-pool-%d") .setPriority(Thread.NORM_PRIORITY).build(); public static ExecutorService uploadExecuteService = new ThreadPoolExecutor(10, 20, 300L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1024), commonThreadFactory, new ThreadPoolExecutor.AbortPolicy()); protected abstract String upload(List<T> data); protected void execute(String userName, List<T> data) { // 生成一个唯一编号 String uuid = UUID.randomUUID().toString().replace("-", ""); uploadExecuteService.submit(() -> { // 记录日志 writeLogToDb(uuid, userName, updateTime, "导入中"); // 一个字符串,用于记录upload的校验信息 String errorLog = ""; //执行上传 try { errorLog = upload(data); writeSuccess(uuid, "导入中", updateTime); } catch (Exception e) { LOGGER.error("导入错误", e); //计入导入错误日志 writeFailToDb(uuid, "导入失败", e.getMessage(), updateTime); } /** * 检查一下upload是不是返回了错误日志,如果有,需要注意记录 * * 因为错误日志可能比较长, * 可以写入一个文件然后上传到公司的文件服务器, * 然后在查看结果的时候允许用户下载该文件, * 这里不展开只做示意 */ if (StringUtils.isNotEmpty(errorLog)) { writeFailToDb(uuid, "导入失败", errorLog, updateTime); } }); } }如上文所示,模板方法的方式虽然能够极大地减少重复代码,但是仍有下面两个问题:
2. 每个上传的 service 还是要继承一下这个抽象类,还是不够简便和优雅
public class FileUploadLog { private Integer id; // 唯一编码 private String batchNo; // 上传到文件服务器的文件key private String key; // 错误日志文件名 private String fileName; //上传状态 private Integer status; //上传人 private String createName; //上传类型 private String uploadType; //结束时间 private Date endTime; // 开始时间 private Date startTime; }然后定义一个上传的类型枚举,用于记录是哪里操作的:
public enum UploadType { 未知(1,"未知 公众 号Java精选,关注有好礼"), 类型2(2,"类型2"), 类型1(3,"类型1"); private int code; private String desc; private static Map<Integer, UploadType> map = new HashMap<>(); static { for (UploadType value : UploadType.values()) { map.put(value.code, value); } } UploadType(int code, String desc) { this.code = code; this.desc = desc; } public int getCode() { return code; } public String getDesc() { return desc; } public static UploadType getByCode(Integer code) { return map.get(code); } }最后,定义一个注解,用于标识切点:
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface Upload { // 记录上传类型 UploadType type() default UploadType.未知; }然后,编写切面:
@Component @Aspect @Slf4j public class UploadAspect { public static ThreadFactory commonThreadFactory = new ThreadFactoryBuilder().setNameFormat("upload-pool-%d") .setPriority(Thread.NORM_PRIORITY).build(); public static ExecutorService uploadExecuteService = new ThreadPoolExecutor(10, 20, 300L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1024), commonThreadFactory, new ThreadPoolExecutor.AbortPolicy()); @Pointcut("@annotation(com.aaa.bbb.Upload)") public void uploadPoint() {} @Around(value = "uploadPoint()") public Object uploadControl(ProceedingJoinPoint pjp) { // 获取方法上的注解,进而获取uploadType MethodSignature signature = (MethodSignature)pjp.getSignature(); Upload annotation = signature.getMethod().getAnnotation(Upload.class); UploadType type = annotation == null ? UploadType.未知 : annotation.type(); // 获取batchNo String batchNo = UUID.randomUUID().toString().replace("-", ""); // 初始化一条上传的日志,记录开始时间 writeLogToDB(batchNo, type, new Date) // 线程池启动异步线程,开始执行上传的逻辑,pjp.proceed()就是你实现的上传功能 uploadExecuteService.submit(() -> { try { String errorMessage = pjp.proceed(); // 没有异常直接成功 if (StringUtils.isEmpty(errorMessage)) { // 成功,写入数据库,具体不展开了 writeSuccessToDB(batchNo); } else { // 失败,因为返回了校验信息 fail(errorMessage, batchNo); } } catch (Throwable e) { LOGGER.error("导入失败:", e); // 失败,抛了异常,需要记录 fail(e.toString(), batchNo); } }); return new Object(); } private void fail(String message, String batchNo) { // 生成上传错误日志文件的文件key String s3Key = UUID.randomUUID().toString().replace("-", ""); // 生成文件名称 String fileName = "错误日志_" + DateUtil.dateToString(new Date(), "yyyy年MM月dd日HH时mm分ss秒") + ExportConstant.txtSuffix; String filePath = "/home/xxx/xxx/" + fileName; // 生成一个文件,写入错误数据 File file = new File(filePath); OutputStream outputStream = null; try { outputStream = new FileOutputStream(file); outputStream.write(message.getBytes()); } catch (Exception e) { LOGGER.error("写入文件错误", e); } finally { try { if (outputStream != null) outputStream.close(); } catch (Exception e) { LOGGER.error("关闭错误", e); } } // 上传错误日志文件到文件服务器,我们用的是s3 upFileToS3(file, s3Key); // 记录上传失败,同时记录错误日志文件地址到数据库,方便用户查看错误信息 writeFailToDB(batchNo, s3Key, fileName); // 删除文件,防止硬盘爆炸 deleteFile(file) } }至此整个异步上传功能就完成了,是不是很简单?(笑)
@Upload(type = UploadType.类型1) public String upload(List<ClassOne> items) { if (items == null || items.size() == 0) { return; } //校验 String error = uploadCheck(items); if (StringUtils.isNotEmpty) { return error; } //删除旧的 deleteAll(); //插入新的 batchInsert(items); }