elunez/eladmin · error · BadRequestException

创建定时任务失败

Error message

创建定时任务失败

What it means

QuartzManage.addJob wraps scheduler.scheduleJob(jobDetail, cronTrigger) in a blanket catch(Exception); only ObjectAlreadyExistsException is tolerated (job already present at startup restore). Any other scheduler failure — building the trigger, job registration, or the immediate pauseJob call — is logged and rethrown as BadRequestException '创建定时任务失败'. The underlying exception is in the server log; the API response intentionally hides it.

Source

Thrown at eladmin-system/src/main/java/me/zhengjie/modules/quartz/utils/QuartzManage.java:72

            cronTrigger.getJobDataMap().put(QuartzJob.JOB_KEY, quartzJob);

            //重置启动时间
            ((CronTriggerImpl)cronTrigger).setStartTime(new Date());

            //执行定时任务,如果是持久化的,这里会报错,捕获输出
            try {
                scheduler.scheduleJob(jobDetail,cronTrigger);
            } catch (ObjectAlreadyExistsException e) {
                log.warn("定时任务已存在,跳过加载");
            }

            // 暂停任务
            if (quartzJob.getIsPause()) {
                pauseJob(quartzJob);
            }
        } catch (Exception e){
            log.error("创建定时任务失败", e);
            throw new BadRequestException("创建定时任务失败");
        }
    }

    /**
     * 更新job cron表达式
     * @param quartzJob /
     */
    public void updateJobCron(QuartzJob quartzJob){
        try {
            TriggerKey triggerKey = TriggerKey.triggerKey(JOB_NAME + quartzJob.getId());
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            // 如果不存在则创建一个定时任务
            if(trigger == null){
                addJob(quartzJob);
                trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            }
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzJob.getCronExpression());
            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Read the full stack trace after the '创建定时任务失败' log line — it names the real exception; fix that root cause.
  2. Verify the quartz tables/schema exist and match your Quartz version if using JDBC job store.
  3. Ensure the job bean/class is resolvable in Spring context (see also error 26 validation) and its state is serializable.
  4. Confirm the Scheduler bean is not in standby/shutdown state (check spring.quartz.auto-startup and any manual scheduler.shutdown() calls).
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-flight: scheduler healthy and bean resolvable before create
if (!scheduler.isStarted()) throw new IllegalStateException("Quartz scheduler not started");
if (!SpringBeanHolder.getAllServiceBeanName().contains(job.getBeanName())) throw new IllegalArgumentException("bean missing");

Try / catch

try { quartzJobService.create(job); } catch (BadRequestException e) { // real cause is in the server log after "创建定时任务失败" log.error(e.getMessage(), e); return fail("创建失败,请查看服务端日志定位根因"); }

Prevention

When it happens

Trigger: Creating a job (create calls addJob after save) when the Scheduler is misconfigured or shut down, the JobDataMap references a bean missing from Spring context, or a ClassCastException/internal SchedulerException occurs. Since create() is @Transactional, the DB row is also rolled back.

Common situations: Quartz clustered mode with a broken QRTZ_ table schema or datasource; scheduler auto-startup disabled; the scheduled job class failing to instantiate; serializing non-serializable data into the JobDataMap.

Related errors


AI-assisted analysis of elunez/eladmin@55fbf70595 (2026-08-14). Data as JSON: /api/errors/588af5d8ef62f441. Report an issue: GitHub.