@ResponseBody @RequestMapping(value = "/startJob", method = RequestMethod.POST) public void startJob(@RequestParam Map<String, Object> map) throws Exception { String selectid = (String) map.get("selectid"); AssertHelper.notEmpty_assert(selectid, "执行的任务id不能为空"); JobDefinition jobDefinition = (JobDefinition) jobDefinitionService.get(JobDefinition.class, selectid); // JobDefinition job=(JobDefinition)jobDefinitionService.get(JobDefinition.class, selectid); jobDefinition.setState(JobState.running); jobDefinition.setStartDate(new Date()); jobDefinitionService.update(jobDefinition); try { JobHistory jobhistory = new JobHistory(); jobhistory.setStartDate(jobDefinition.getStartDate()); jobhistory.setId(IdGenerator.getUUID()); jobhistory.setJobId(selectid); jobhistory.setSuccessful(true); jobDefinitionService.save(jobhistory); } catch (Exception e) { e.printStackTrace(); JobHistory jobhistory = new JobHistory(); jobhistory.setEndDate(jobDefinition.getEndDate()); jobhistory.setStartDate(jobDefinition.getStartDate()); jobhistory.setId(IdGenerator.getUUID()); jobhistory.setJobId(selectid); jobhistory.setSuccessful(false); jobDefinitionService.save(jobhistory); } }
/** * 立即执行 * * @param map * @throws Exception */ @ResponseBody @RequestMapping(value = "/exeJob", method = RequestMethod.POST) public void exeJob(@RequestParam Map<String, Object> map, HttpServletResponse response) throws Exception { String selectid = (String) map.get("selectid"); AssertHelper.notEmpty_assert(selectid, "执行的任务id不能为空"); JobDefinition jobDefinition = (JobDefinition) jobDefinitionService.get(JobDefinition.class, selectid); AssertHelper.notEmpty_assert(jobDefinition, "id为" + selectid + "的job定义没有找到"); String beanId = jobDefinition.getBeanId(); AssertHelper.notEmpty_assert(jobDefinition, "id为" + selectid + "的job没有定义beanId"); Object job = SpringUtil.getBean(beanId); AssertHelper.notEmpty_assert(job, "beanId为" + beanId + "的bean没有找到"); if (!((job instanceof Job) && (job instanceof JobTest))) { throw new BusinessException("beanId为" + beanId + "的bean必须实现的是org.quartz.Job和JobTest接口"); } JobTest jobTest = (JobTest) job; JSONObject object = new JSONObject(); object.put("success", "true"); try { jobTest.execute(); JobHistory jobhistory = new JobHistory(); jobhistory.setEndDate(jobDefinition.getEndDate()); jobhistory.setStartDate(jobDefinition.getStartDate()); jobhistory.setId(IdGenerator.getUUID()); jobhistory.setJobId(beanId); jobhistory.setSuccessful(true); jobDefinitionService.save(jobhistory); retJson(response, object); } catch (Exception e) { e.printStackTrace(); JobHistory jobhistory = new JobHistory(); jobhistory.setEndDate(jobDefinition.getEndDate()); jobhistory.setStartDate(jobDefinition.getStartDate()); jobhistory.setId(IdGenerator.getUUID()); jobhistory.setJobId(beanId); jobhistory.setSuccessful(false); jobDefinitionService.save(jobhistory); object.put("errorMsg", e.getMessage()); object.put("success", "false"); retJson(response, object); } }