elunez/eladmin · error · BadRequestException
定时任务执行失败
Error message
定时任务执行失败
What it means
QuartzManage.runJobNow implements 'execute immediately': it fetches the trigger (creating the job via addJob if the trigger is null), then scheduler.triggerJob(jobKey, dataMap) to fire it once outside the cron schedule. Any exception is logged and rethrown as BadRequestException '定时任务执行失败'. This does not affect the job's schedule.
Source
Thrown at eladmin-system/src/main/java/me/zhengjie/modules/quartz/utils/QuartzManage.java:160
/**
* 立即执行job
* @param quartzJob /
*/
public void runJobNow(QuartzJob quartzJob){
try {
TriggerKey triggerKey = TriggerKey.triggerKey(JOB_NAME + quartzJob.getId());
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
// 如果不存在则创建一个定时任务
if(trigger == null) {
addJob(quartzJob);
}
JobDataMap dataMap = new JobDataMap();
dataMap.put(QuartzJob.JOB_KEY, quartzJob);
JobKey jobKey = JobKey.jobKey(JOB_NAME + quartzJob.getId());
scheduler.triggerJob(jobKey,dataMap);
} catch (Exception e){
log.error("定时任务执行失败", e);
throw new BadRequestException("定时任务执行失败");
}
}
/**
* 暂停一个job
* @param quartzJob /
*/
public void pauseJob(QuartzJob quartzJob){
try {
JobKey jobKey = JobKey.jobKey(JOB_NAME + quartzJob.getId());
scheduler.pauseJob(jobKey);
} catch (Exception e){
log.error("定时任务暂停失败", e);
throw new BadRequestException("定时任务暂停失败");
}
}
}
View on GitHub (pinned to 55fbf70595)
Solutions
- Read the logged stack trace to find whether addJob or triggerJob failed and why.
- Recreate the job (delete + add) to rebuild both job detail and trigger atomically.
- Verify the job's bean still exists as a @Service bean (the runtime instantiation uses the Spring context via the job factory).
Defensive patterns
Strategy: try-catch
Validate before calling
// ensure job detail exists before manual fire
JobKey jk = JobKey.jobKey("JOB_NAME" + job.getId());
if (!scheduler.checkExists(jk)) quartzManage.addJob(job); // rebuilds detail+trigger
scheduler.triggerJob(jk, new JobDataMap(Map.of(QuartzJob.JOB_KEY, job))); Try / catch
try { quartzJobService.execution(job); } catch (BadRequestException e) { if ("定时任务执行失败".equals(e.getMessage())) { log.error("manual fire failed", e); return fail("执行失败:请检查任务Bean与调度器状态(日志有详细堆栈)"); } throw e; } Prevention
- Check the target bean still exists as @Service before manual execution.
- Smoke-test jobs with 执行 immediately after creation to catch instantiation problems early.
- Correlate the API failure with the server stack trace — the message alone says nothing.
When it happens
Trigger: Clicking 执行 on a quartz job in the UI (runJobNow endpoint) when the scheduler throws: missing job detail after a failed lazy addJob, scheduler standby, or the job class cannot be instantiated by Quartz at trigger time.
Common situations: JobDetail missing because the trigger existed but the job did not (partial state); target bean removed from Spring context so ExecutionException occurs; scheduler paused globally; concurrent-run policy rejecting the manual fire in some store states.
Related errors
AI-assisted analysis of elunez/eladmin@55fbf70595 (2026-08-14).
Data as JSON: /api/errors/f2f9b92d052d1bd2.
Report an issue: GitHub.