我有一个控制器,我试图立即触发新的工作:
@GetMapping("/job/{tripId}")
public void handle(@PathVariable("tripId") Integer fundId) {
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
try {
Scheduler sched = schedFact.getScheduler();
//Create a new Job
JobKey jobKey = JobKey.jobKey("Trip Calculations", "Trip Calculations Group");
JobDetail job = JobBuilder.newJob(TripCalculationsJob.class).withIdentity(jobKey).storeDurably().build();
job.getJobDataMap().put("tripId", fundId);
//Register this job to the scheduler
sched.addJob(job, true);
sched.triggerJob(jobKey);
} catch (SchedulerException e) {
log.error("Error when execting job");
e.printStackTrace();
}
}
job是实现job的简单类:
@Slf4j
public class TripCalculationsJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
log.info( "blah blah blah");
}
作业没有被触发,下面是maven的依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
</dependency>
我认为调度作业的全部意义在于不要手动触发它们。为什么不将作业的操作提取到另一个处理程序类中,并从作业和控制器中调用该处理程序?