1.controller封装
@RestController @RequestMapping("/api") public class SchedulerJobController { private Logger logger = LoggerFactory.getLogger(SchedulerJobController.class); @Autowired private SchedulerJobService schedulerJobService; @PostMapping("/getAllJobs") public MapgetAllJobs(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){ logger.info("getAllJobs:Request{pageNum:" + pageNum + ",pageSize:" + pageSize + "}"); PageInfo jobList = schedulerJobService.getAllScheduleJob(pageNum,pageSize); Map resultMap = new HashMap (); resultMap.put("errCode",200); resultMap.put("errMsg","查询任务成功"); resultMap.put("jobList", jobList.getList()); resultMap.put("count", jobList.getTotal()); resultMap.put("begin", pageNum); logger.info("getAllJobs:Response" + JSON.toJSONString(resultMap)); return resultMap; } @PostMapping("/saveOrUpdate") public Map addOrUpdateJob(@RequestBody String scheduleJobStr) throws Exception { logger.info("addOrUpdateJob:Request" + scheduleJobStr); Map scheduleMap = JSON.parseObject(scheduleJobStr); String branchOrgInfo = scheduleMap.get("branchOrgInfo").toString(); String settlement = scheduleMap.get("settlement").toString(); Map paramMap = new HashMap<>(); paramMap.put("branchOrgCode",branchOrgInfo); paramMap.put("settlement",settlement); scheduleMap.put("params",JSON.toJSONString(paramMap)); scheduleJobStr = JSON.toJSONString(scheduleMap); ScheduleJob scheduleJob = JSON.parseObject(scheduleJobStr,ScheduleJob.class); int flag = schedulerJobService.saveOrUpdate(scheduleJob); Map resultMap = new HashMap (); if(flag == 1) { resultMap.put("errCode",200); resultMap.put("errMsg","添加任务成功"); } else { resultMap.put("errCode",200); resultMap.put("errMsg","更新任务成功"); } logger.info("addOrUpdateJob:success"); logger.info("addOrUpdateJob:Response" + JSON.toJSONString(resultMap)); return resultMap; } @PostMapping("/pausejob") public Map pauseJob(@RequestBody String scheduleJobStr) throws SchedulerException { logger.info("pausejob:Request" + scheduleJobStr); ScheduleJob scheduleJob = JSON.parseObject(scheduleJobStr,ScheduleJob.class); schedulerJobService.pauseJob(scheduleJob.getJobName(),scheduleJob.getJobGroup()); Map resultMap = new HashMap (); resultMap.put("errCode",200); resultMap.put("errMsg","暂停任务成功"); logger.info("pauseJob:Response" + JSON.toJSONString(resultMap)); return resultMap; } @PostMapping("/deleteJob") public Map deleteJob(@RequestBody String scheduleJobStr) throws SchedulerException { logger.info("deleteJob:Request" + scheduleJobStr); ScheduleJob scheduleJob = JSON.parseObject(scheduleJobStr,ScheduleJob.class); schedulerJobService.deleteJob(scheduleJob.getJobName(),scheduleJob.getJobGroup()); Map resultMap = new HashMap (); resultMap.put("errCode",200); resultMap.put("errMsg","删除任务成功"); logger.info("deleteJob:Response" + JSON.toJSONString(resultMap)); return resultMap; } @PostMapping("/runOneJob") public Map runJob(@RequestBody String scheduleJobStr) throws SchedulerException { logger.info("runOneJob:Request" + scheduleJobStr); ScheduleJob scheduleJob = JSON.parseObject(scheduleJobStr,ScheduleJob.class); schedulerJobService.runOneJob(scheduleJob.getJobName(),scheduleJob.getJobGroup()); Map resultMap = new HashMap (); resultMap.put("errCode",200); resultMap.put("errMsg","运行一次任务成功"); logger.info("runOneJob:Response" + JSON.toJSONString(resultMap)); return resultMap; } @PostMapping("/resumeJob") public Object resumeJob(@RequestBody String scheduleJobStr) throws SchedulerException { logger.info("resumeJob:Request" + scheduleJobStr); ScheduleJob scheduleJob = JSON.parseObject(scheduleJobStr,ScheduleJob.class); schedulerJobService.resumeJob(scheduleJob.getJobName(),scheduleJob.getJobGroup()); Map resultMap = new HashMap (); resultMap.put("errCode",200); resultMap.put("errMsg","重启任务成功"); logger.info("resumeJob:Response" + JSON.toJSONString(resultMap)); return resultMap; } }
2.service
public interface SchedulerJobService { PageInfogetAllScheduleJob(Integer pageNum, Integer pageSize); int saveOrUpdate(ScheduleJob scheduleJob) throws Exception; public void pauseJob(String jobName, String jobGroup) throws SchedulerException; public void deleteJob(String jobName,String jobGroup) throws SchedulerException; public void runOneJob(String jobName, String jobGroup) throws SchedulerException; public void resumeJob(String jobName, String jobGroup) throws SchedulerException; void checkNotNull(ScheduleJob scheduleJob); }
@Service @Transactional public class SchedulerJobServiceImpl implements SchedulerJobService { private Logger logger = LoggerFactory.getLogger(SchedulerJobServiceImpl.class); @Autowired private SchedulerJobInService schedulerJobInService; @Autowired private Scheduler scheduler; @Override public PageInfogetAllScheduleJob(Integer pageNum, Integer pageSize) { PageHelper.startPage(pageNum,pageSize); List scheduleJobs = schedulerJobInService.selectAllJobs(); PageInfo page = new PageInfo (scheduleJobs); return page; } @Override public int saveOrUpdate(ScheduleJob scheduleJob) throws Exception { int flag = 0; TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobGroup(),scheduleJob.getJobGroup()); CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey); if (cronTrigger==null){ logger.info("添加任务----------------/addJob"); addJob(scheduleJob); flag = 1; }else { logger.info("更新任务----------------/updateJob"); updateJobSchedule(scheduleJob); } return flag; } @Override public void pauseJob(String jobName, String jobGroup) throws SchedulerException { logger.info("暂停任务----------------/pausejob"); JobKey jobKey = JobKey.jobKey(jobGroup,jobGroup); ScheduleJob scheduleJob = schedulerJobInService.selectByJobNameAngJobGroup(jobName,jobGroup); scheduleJob.setJobStatus("0"); scheduler.pauseJob(jobKey); schedulerJobInService.updateByPrimaryKeySelective(scheduleJob); } @Override public void deleteJob(String jobName, String jobGroup) throws SchedulerException { logger.info("删除任务----------------/deleteJob"); ScheduleJob jobKey = new ScheduleJob(); jobKey.setJobName(jobName); jobKey.setJobGroup(jobGroup); TriggerKey triggerKey = TriggerKey.triggerKey(jobGroup, jobGroup); // 停止触发器 scheduler.pauseTrigger(triggerKey); // 移除触发器 scheduler.unscheduleJob(triggerKey); // 删除任务 scheduler.deleteJob(JobKey.jobKey(jobGroup, jobGroup)); schedulerJobInService.deleteByJobNameAndJobGroup(jobKey); logger.info("deleteJob:" + JobKey.jobKey(jobName)); } @Override public void runOneJob(String jobName, String jobGroup) throws SchedulerException { logger.info("运行一次任务----------------/runOneJob"); JobKey jobKey = JobKey.jobKey(jobGroup, jobGroup); ScheduleJob scheduleJob = schedulerJobInService.selectByJobNameAngJobGroup(jobName, jobGroup); scheduler.triggerJob(jobKey); schedulerJobInService.updateByPrimaryKeySelective(scheduleJob); } @Override public void resumeJob(String jobName, String jobGroup) throws SchedulerException { logger.info("重启任务----------------/resumeJob"); JobKey jobKey = JobKey.jobKey(jobGroup,jobGroup); ScheduleJob scheduleJob = schedulerJobInService.selectByJobNameAngJobGroup(jobName,jobGroup); scheduleJob.setJobStatus("1"); scheduler.resumeJob(jobKey); schedulerJobInService.updateByPrimaryKeySelective(scheduleJob); } private void addJob(ScheduleJob scheduleJob) throws Exception{ //校验参数 checkNotNull(scheduleJob); String cronexpression = ""; if (StringUtils.isBlank(scheduleJob.getNextSchedulTime())) { cronexpression = scheduleJob.getCronexpression(); } else { String nextSchedulTime = scheduleJob.getNextSchedulTime(); logger.info("调度时间----------------->" + nextSchedulTime); String now = DateUtil.formatDate(new Date()); logger.info("当前时间----------------->" + now); int index = DateUtil.compareDate(now, nextSchedulTime); if (index == 1) { logger.info("----------------->调度时间不能小于当前时间"); throw new BusinessException(9999,"调度时间不能小于当前时间"); } cronexpression = DateUtil.getCron(nextSchedulTime); } logger.info("cron表达式:" + cronexpression); if (StringUtils.isBlank(cronexpression)){ throw new BusinessException("Cronexpression不能为空"); } scheduleJob.setCronexpression(cronexpression); scheduleJob.setTriggerName("trigger" + scheduleJob.getJobName()); scheduleJob.setTriggerGroup(scheduleJob.getJobGroup()); scheduleJob.setJobStatus("1"); scheduleJob.setJobRunState("0"); //构建job信息 JobDetail jobDetail = JobBuilder.newJob(getCls(scheduleJob.getJobClassName()).getClass()) .withIdentity(scheduleJob.getJobGroup(),scheduleJob.getJobGroup()) .withDescription(scheduleJob.getRemark()) .build(); jobDetail.getJobDataMap().put("scheduleJob",scheduleJob); //表达式调度构建器(即任务执行的时间) CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronexpression); //按新的cronexpression表达式构建一个新的trigger CronTrigger cronTrigger = TriggerBuilder.newTrigger() .withIdentity(scheduleJob.getJobGroup(),scheduleJob.getJobGroup()) .withSchedule(cronScheduleBuilder).build(); //传递的参数 jobDetail.getJobDataMap().put("jobTaskType", "0"); jobDetail.getJobDataMap().put("jobType", scheduleJob.getJobType()); jobDetail.getJobDataMap().put("apiUrl", scheduleJob.getApiUrl()); jobDetail.getJobDataMap().put("params", scheduleJob.getParams()); //交由Scheduler安排触发 scheduler.scheduleJob(jobDetail,cronTrigger); schedulerJobInService.insertSelective(scheduleJob); if("1".equals(scheduleJob.getIsNowRun())) { // 如果是立即运行则首先触发一次任务 JobKey key = new JobKey(scheduleJob.getJobGroup(), scheduleJob.getJobGroup()); scheduler.triggerJob(key, jobDetail.getJobDataMap()); } } private void updateJobSchedule(ScheduleJob scheduleJob) throws Exception{ //校验参数 checkNotNull(scheduleJob); String cronexpression = ""; if (StringUtils.isBlank(scheduleJob.getNextSchedulTime())) { cronexpression = scheduleJob.getCronexpression(); } else { String nextSchedulTime = scheduleJob.getNextSchedulTime(); logger.info("调度时间----------------->" + nextSchedulTime); String now = DateUtil.formatDate(new Date()); logger.info("当前时间----------------->" + now); int index = DateUtil.compareDate(now, nextSchedulTime); if (index == 1) { logger.info("------------------>调度时间不能小于当前时间"); throw new BusinessException(9999,"调度时间不能小于当前时间"); } cronexpression = DateUtil.getCron(nextSchedulTime); } logger.info("cron表达式:" + cronexpression); if (StringUtils.isBlank(cronexpression)){ throw new BusinessException("Cronexpression不能为空"); } scheduleJob.setCronexpression(cronexpression); String jobName = scheduleJob.getJobName(); String jobGroup = scheduleJob.getJobGroup(); ScheduleJob job = schedulerJobInService.selectByJobNameAngJobGroup(jobName,jobGroup); if(job == null || job.equals("")) { throw new BusinessException("null Job, please check it"); } scheduleJob.setJobId(job.getJobId()); //移除任务 TriggerKey triggerKey = TriggerKey.triggerKey(jobGroup, jobGroup); // 停止触发器 scheduler.pauseTrigger(triggerKey); // 移除触发器 scheduler.unscheduleJob(triggerKey); // 删除任务 scheduler.deleteJob(JobKey.jobKey(jobGroup, jobGroup)); //构建job信息 JobDetail jobDetail = JobBuilder.newJob(getCls(scheduleJob.getJobClassName()).getClass()) .withIdentity(jobGroup,jobGroup) .withDescription(scheduleJob.getRemark()) .build(); jobDetail.getJobDataMap().put("scheduleJob",scheduleJob); //表达式调度构建器(即任务执行的时间) CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronexpression); //按新的cronexpression表达式构建一个新的trigger CronTrigger cronTrigger = TriggerBuilder.newTrigger() .withIdentity(jobGroup,jobGroup) .withSchedule(cronScheduleBuilder).build(); //传递的参数 jobDetail.getJobDataMap().put("jobTaskType", "0"); jobDetail.getJobDataMap().put("jobType", scheduleJob.getJobType()); jobDetail.getJobDataMap().put("apiUrl", scheduleJob.getApiUrl()); jobDetail.getJobDataMap().put("params", scheduleJob.getParams()); //交由Scheduler安排触发 scheduler.scheduleJob(jobDetail,cronTrigger); scheduleJob.setJobStatus("1"); scheduleJob.setJobRunState("0"); schedulerJobInService.updateByPrimaryKeySelective(scheduleJob); if("1".equals(scheduleJob.getIsNowRun())) { // 如果是立即运行则首先触发一次任务 JobKey key = new JobKey(jobGroup, jobGroup); scheduler.triggerJob(key, jobDetail.getJobDataMap()); } } @Override public void checkNotNull(ScheduleJob scheduleJob) { if (scheduleJob==null){ throw new BusinessException("scheduleJob is null,Please check it"); } if (scheduleJob.getJobName()==null || scheduleJob.getJobName().equals("")){ throw new BusinessException("the jobName of scheduleJob is null,Please check it"); } if (scheduleJob.getJobGroup()==null || scheduleJob.getJobGroup().equals("")){ throw new BusinessException("the jobGroup of scheduleJob is null,Please check it"); } } public static baseJob getCls(String classname) throws Exception { Class> class1 = Class.forName(classname); return (baseJob)class1.newInstance(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)