{"record":{"id":"959341cfdc1807e7","repo":"xkcoding/spring-boot-demo","slug":"error-959341","errorCode":null,"errorMessage":"【定时任务】创建失败！","messagePattern":"【定时任务】创建失败！","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"demo-task-quartz/src/main/java/com/xkcoding/task/quartz/service/impl/JobServiceImpl.java","lineNumber":62,"sourceCode":"    @Override\n    public void addJob(JobForm form) throws Exception {\n        // 启动调度器\n        scheduler.start();\n\n        // 构建Job信息\n        JobDetail jobDetail = JobBuilder.newJob(JobUtil.getClass(form.getJobClassName()).getClass()).withIdentity(form.getJobClassName(), form.getJobGroupName()).build();\n\n        // Cron表达式调度构建器(即任务执行的时间)\n        CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule(form.getCronExpression());\n\n        //根据Cron表达式构建一个Trigger\n        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(form.getJobClassName(), form.getJobGroupName()).withSchedule(cron).build();\n\n        try {\n            scheduler.scheduleJob(jobDetail, trigger);\n        } catch (SchedulerException e) {\n            log.error(\"【定时任务】创建失败！\", e);\n            throw new Exception(\"【定时任务】创建失败！\");\n        }\n\n    }\n\n    /**\n     * 删除定时任务\n     *\n     * @param form 表单参数 {@link JobForm}\n     * @throws SchedulerException 异常\n     */\n    @Override\n    public void deleteJob(JobForm form) throws SchedulerException {\n        scheduler.pauseTrigger(TriggerKey.triggerKey(form.getJobClassName(), form.getJobGroupName()));\n        scheduler.unscheduleJob(TriggerKey.triggerKey(form.getJobClassName(), form.getJobGroupName()));\n        scheduler.deleteJob(JobKey.jobKey(form.getJobClassName(), form.getJobGroupName()));\n    }\n\n    /**","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/xkcoding/spring-boot-demo/blob/87a142f9604c1a5365b4d24d22c2c11c26a9d5ab/demo-task-quartz/src/main/java/com/xkcoding/task/quartz/service/impl/JobServiceImpl.java#L44-L80","documentation":"JobServiceImpl.addJob wraps scheduler.scheduleJob(jobDetail, trigger) in try/catch(SchedulerException) and rethrows a generic Exception('【定时任务】创建失败！') at JobServiceImpl.java:62. Note the cron build (CronScheduleBuilder.cronSchedule at line 53) is OUTSIDE the try, so an invalid cron expression escapes uncaught.","triggerScenarios":"Adding a job whose class+group already exists (ObjectAlreadyExistsException, a SchedulerException); a job class not on the classpath or not implementing org.quartz.Job; a scheduler-store error during scheduleJob.","commonSituations":"Submitting the same jobClassName twice; a wrong class name in JobForm; clustered Quartz with a conflicting job identity.","solutions":["Check scheduler.checkExists(JobKey.jobKey(className, groupName)) before scheduleJob","Ensure the job class implements org.quartz.Job and is loadable by JobUtil.getClass","Validate the cron expression (CronExpression.isValidExpression) before building the schedule","Keep job class+group unique per submission"],"exampleFix":"// before\ntry {\n    scheduler.scheduleJob(jobDetail, trigger);\n} catch (SchedulerException e) {\n    throw new Exception('【定时任务】创建失败！');\n}\n// after\nJobKey jobKey = JobKey.jobKey(form.getJobClassName(), form.getJobGroupName());\nif (scheduler.checkExists(jobKey)) {\n    throw new IllegalStateException('任务已存在: ' + jobKey);\n}\ntry {\n    scheduler.scheduleJob(jobDetail, trigger);\n} catch (SchedulerException e) {\n    throw new Exception('【定时任务】创建失败！', e);\n}","handlingStrategy":"validation","validationCode":"JobKey jobKey = JobKey.jobKey(form.getJobClassName(), form.getJobGroupName());\nif (scheduler.checkExists(jobKey)) {\n    throw new IllegalStateException('job already exists: ' + jobKey);\n}\nif (!CronExpression.isValidExpression(form.getCronExpression())) {\n    throw new IllegalArgumentException('invalid cron: ' + form.getCronExpression());\n}","typeGuard":null,"tryCatchPattern":"try { jobService.addJob(form); }\ncatch (Exception e) { if ('【定时任务】创建失败！'.equals(e.getMessage())) { /* surface scheduling failure */ } }","preventionTips":["Check checkExists before scheduleJob to avoid ObjectAlreadyExistsException","Ensure the job class implements org.quartz.Job","Validate the cron expression before CronScheduleBuilder.cronSchedule"],"tags":["quartz","scheduler","duplicate-job"],"backgroundTag":null,"analyzedSha":"87a142f9604c1a5365b4d24d22c2c11c26a9d5ab","analyzedAt":"2026-08-14T01:16:58.217Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}