我使用弹簧实现石英作业。我的调度程序工作正常,工作正在完美执行。 我的问题是如何使这个调度程序进入待机状态?因此,一旦我决定将调度程序置于待机模式,就不会触发任何作业。 以下是工作类
public class MyJobClass extends QuartzJobBean {
//my job logic
}
来自applicationContext_Scheduler.xml的片段
<bean name="myJobBean"
class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="MyJobClass" />
</bean>
<bean id="rsHourlyJobCronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="myJobBean" />
<property name="cronExpression" value="00 0/1 * * * ?" />
</bean>
答案 0 :(得分:0)
而不是在xml中创建调度程序bean。我按照以下方式编程:
@Configuration
public class QuartzSchedulerConfiguration {
@Autowired
private ApplicationContext applicationContext;
@Bean
public JobFactory jobFactory() {
ApplicationContextHolder jobFactory = new ApplicationContextHolder();
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}
@Bean
public SchedulerFactoryBean schedulerFactory() {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setAutoStartup(true);
factory.setSchedulerName("Quartz Scheduler");
factory.setOverwriteExistingJobs(true);
factory.setJobFactory(jobFactory());
return factory;
}
}
@Component
public final class ApplicationContextHolder extends SpringBeanJobFactory implements ApplicationContextAware {
private static ApplicationContext context;
private transient AutowireCapableBeanFactory beanFactory;
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
beanFactory = ctx.getAutowireCapableBeanFactory();
context = ctx;
}
@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
final Object job = super.createJobInstance(bundle);
beanFactory.autowireBean(job);
return job;
}
public static ApplicationContext getContext() {
return context;
}
}
这将创建一个应用程序上下文感知的quartz调度程序bean,允许您将弹簧对象自动装配到您的石英作业中。
然后创建一些Scheduling服务,如:
@Service
public class SchedulerService {
@Autowired
private SchedulerFactoryBean schedulerFactory;
private Scheduler scheduler;
@PostConstruct
private void init() {
scheduler = schedulerFactory.getScheduler();
}
public void standBy() throws Exception {
if (scheduler != null && !scheduler.isInStandbyMode()) {
scheduler.standby();
}
}
}
然后添加调度所需的其他方法。