2 回答

TA贡献1860条经验 获得超8个赞
最简单的方法是通过扩展SpringBeanJobFactory和createJobInstance方法来进行一些配置。然后你需要定义 SchedulerFactoryBean,最后定义你的 Scheduler:@Override
@Configuration
public class SchedulerConfiguration {
public class AutowireCapableBeanJobFactory extends SpringBeanJobFactory {
private final AutowireCapableBeanFactory beanFactory;
@Autowired
public AutowireCapableBeanJobFactory(AutowireCapableBeanFactory beanFactory) {
Assert.notNull(beanFactory, "Bean factory must not be null");
this.beanFactory = beanFactory;
}
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object jobInstance = super.createJobInstance(bundle);
this.beanFactory.autowireBean(jobInstance);
this.beanFactory.initializeBean(jobInstance, null);
return jobInstance;
}
}
@Bean
public SchedulerFactoryBean schedulerFactory(ApplicationContext applicationContext) {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setJobFactory(new AutowireCapableBeanJobFactory(applicationContext.getAutowireCapableBeanFactory()));
return schedulerFactoryBean;
}
@Bean
public Scheduler scheduler(ApplicationContext applicationContext) throws SchedulerException {
Scheduler scheduler = schedulerFactory(applicationContext).getScheduler();
scheduler.start();
return scheduler;
}
}
然后,在应用程序中的任意位置(例如在 RestController 中),您可以访问调度程序并计划新作业:
@RestController
public class ScheduleController {
@Autowired
private Scheduler scheduler;
@GetMapping(value = "/schedule/{detail}/{desc}")
public String scheduleJob(@PathVariable(value = "detail") String detail, @PathVariable(value = "desc") String desc) throws SchedulerException {
JobDetail job = newJob(detail, desc);
return scheduler.scheduleJob(job, trigger(job)).toString();
}
private JobDetail newJob(String identity, String description) {
return JobBuilder.newJob().ofType(SimpleJob.class).storeDurably()
.withIdentity(JobKey.jobKey(identity))
.withDescription(description)
.build();
}
private SimpleTrigger trigger(JobDetail jobDetail) {
return TriggerBuilder.newTrigger().forJob(jobDetail)
.withIdentity(jobDetail.getKey().getName(), jobDetail.getKey().getGroup())
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(1))
.build();
}
}
您可以从您的 - 查看文档控制所有计划(暂停,停止,重新启动,删除等)Scheduler

TA贡献1777条经验 获得超3个赞
这就是 JobDataMap 参数的用途。您可以使用这些参数将任意参数传递给作业和触发器。通常建议使用 String 参数值以避免各种序列化问题。JobDataMap API 提供了辅助方法,您可以使用这些方法将字符串值的 JobDataMap 参数值转换为各种基本 Java 对象(整数、长整型、双精度型、布尔值等)。
请注意,在作业详细信息级别上指定的 JobDataMap 参数可以在触发器级别被覆盖。在 JobDetail 级别,通常指定应用于所有作业执行的常用参数和/或默认值,并覆盖这些默认值和/或在触发器级别添加新参数。
有关详细信息,请参阅Quartz Javadoc:
TriggerBuilder.html#usingJobData
添加回答
举报