# springboot-quartz **Repository Path**: happergit/springboot-quartz ## Basic Information - **Project Name**: springboot-quartz - **Description**: springboot-quartz定时任务 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2018-08-10 - **Last Updated**: 2023-10-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # springboot-quartz #### 项目介绍 springboot-quartz定时任务 1)动态修改时间任务,cron表达式,反射调用服务实现多任务调度 2)统一的异常处理,枚举优雅处理; 3)文件zip压缩下载; ###### cron表达式每3小时执行、每6小时执行、每12小时执行 18 31 14/12 * * ? 应该为18 31 2/12 * * ? 秒 分钟 hour%小时频次/小时频次 * * ? 指定时间,按照这个频次执行通用:秒\分钟\小时%频次,时间不能大于频次否则会出现执行时间不对。 每7天执行一次(每周一10:15:0执行) 0 15 10 ? * 2 每月2日执行一次 0 15 10 2 * ? 如果指定是31日,如果当前月份没有31日则不会执行 ``` public class CronCache { private static final Map cronCache=new HashMap<>(); private CronCache(){} static { cronCache.put("1", "/1 * * ?"); cronCache.put("3", "/3 * * ?"); cronCache.put("6", "/6 * * ?"); cronCache.put("12", " * * ?"); cronCache.put("24", " * * ?");//每1天 cronCache.put("724", " ? * ");//每7天 cronCache.put("3024", "/30 * ?");//每30天 } public static String getKey(String key,Date startTime) { String temCron = cronCache.get(key); int hour = startTime.getHours(); int minute = startTime.getMinutes(); int sec = startTime.getSeconds(); int date=startTime.getDate(); if (Integer.valueOf(key) == 724) { Calendar c = Calendar.getInstance(); c.setTime(startTime); int week=c.get(Calendar.DAY_OF_WEEK); return sec+" "+minute+" "+hour+temCron+week; }else if (Integer.valueOf(key)==3024){ if(date==30){ return sec+" "+minute+" "+hour+" "+date+temCron ; }else { return sec + " " + minute + " " + hour + " " + date % 30 + temCron; } }else if (Integer.valueOf(key)==24){ return sec+" "+minute+" "+hour+temCron ; }else if(Integer.valueOf(key)==12){ if(hour>=12){ return sec+" "+minute+" "+(hour-12)+","+hour+temCron; } return sec+" "+minute+" "+hour+","+(hour+12)+temCron; }else{ return sec+" "+minute+" "+hour%Integer.valueOf(key)+temCron; } } public static Set getCode(){ return cronCache.keySet(); } } ``` ## 统一异常处理 Validator只解决了参数自身的数据校验,解决不了参数和业务数据之间校验; 异常按阶段进行分类,大体可以分成:进入Controller前的异常 和 Service 层异常, https://blog.csdn.net/lovoo/article/details/129977635 https://blog.csdn.net/xiyang_1990/article/details/130280018