Java 在数据库中持久化Quartz触发器的正确方法
我对Quartz很陌生,现在我需要在SpringWeb应用程序中安排一些工作 我知道Spring+Quartz集成(我使用的是SpringV3.1.1),但我想知道这是否是正确的方法 特别是我需要将计划的任务保存在数据库中,以便在应用程序重新启动时重新初始化它们 Spring调度包装器是否提供了一些实用程序来实现这一点?Java 在数据库中持久化Quartz触发器的正确方法,java,spring,jdbc,persistence,quartz-scheduler,Java,Spring,Jdbc,Persistence,Quartz Scheduler,我对Quartz很陌生,现在我需要在SpringWeb应用程序中安排一些工作 我知道Spring+Quartz集成(我使用的是SpringV3.1.1),但我想知道这是否是正确的方法 特别是我需要将计划的任务保存在数据库中,以便在应用程序重新启动时重新初始化它们 Spring调度包装器是否提供了一些实用程序来实现这一点? 您能为我推荐一些“众所周知”的方法吗?我想有相当多的文档可用于Spring和Quartz JDBC job store集成;例如: 如果您正在使用注释配置: 如果您使用的是X
您能为我推荐一些“众所周知”的方法吗?我想有相当多的文档可用于Spring和Quartz JDBC job store集成;例如:
- 如果您正在使用注释配置:
- 如果您使用的是XML配置:
- 石英作业商店:
- 石英弹簧3.x
- 这里是我处理这种情况的一种方法
首先,在Spring配置中,我指定了一个
SchedulerFactoryBean
,从中我可以将Scheduler
注入到其他bean中
<bean name="SchedulerFactory"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="applicationContextSchedulerContextKey">
<value>applicationContext</value>
</property>
</bean>
JobContext
I构建包含有关作业的详细信息,并最终传递给用于调度作业的实用程序。下面是实际调度作业的实用程序方法的代码。请注意,在我的服务中,我自动连接了JobScheduler
,并将其传递给JobContext
。还请注意,我使用存储库将作业存储在数据库中
/**
* Schedules a DATA_MINING_JOB for the client. The job will attempt to enter
* followers of the target into the database.
*/
@Override
public void schedule(JobContext context) {
Client client = context.getNetworkSociallyJob().getClient();
this.logScheduleAttempt(context, client);
JobDetail jobDetails = JobBuilder.newJob(this.getJobClass()).withIdentity(context.getQuartzName(), context.getQuartzGroup()).build();
jobDetails.getJobDataMap().put("job", context.getNetworkSociallyJob());
jobDetails.getJobDataMap().put("repositories", context.getRepositories());
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(context.getQuartzName() + "-trigger", context.getQuartzGroup())
.withSchedule(cronSchedule(this.getSchedule())).build();
try {
context.getScheduler().scheduleJob(jobDetails, trigger);
this.logSuccess(context, client);
} catch (SchedulerException e) {
this.logFailure(context, client);
e.printStackTrace();
}
}
因此,在所有这些代码执行之后,有两件事发生了,我的作业存储在数据库中,并且它是使用quartz调度器进行调度的。现在,如果应用程序重新启动,我想用调度器重新安排我的作业。为此,我注册了一个bean,它实现了ApplicationListener
,每次容器重新启动或启动时,Spring都会调用它
<bean id="jobInitializer" class="com.network.socially.web.jobs.JobInitializer"/>
作业初始值设定项.class
public class JobInitializer implements ApplicationListener<ContextRefreshedEvent> {
Logger logger = LoggerFactory.getLogger(JobInitializer.class);
@Autowired
DataMiningJobRepository repository;
@Autowired
ApplicationJobRepository jobRepository;
@Autowired
Scheduler scheduler;
@Autowired
JobSchedulerLocator locator;
@Autowired
ListableBeanFactory beanFactory;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
logger.info("Job Initilizer started.");
//TODO: Modify this call to only pull completed & enabled jobs
for (ApplicationJob applicationJob : jobRepository.findAll()) {
if (applicationJob.getIsEnabled() && (applicationJob.getIsCompleted() == null || !applicationJob.getIsCompleted())) {
JobSchedulerUtil.schedule(new JobContext(beanFactory, scheduler, locator, applicationJob));
}
}
}
}
公共类作业初始值设定项实现ApplicationListener{
Logger Logger=LoggerFactory.getLogger(JobInitializer.class);
@自动连线
数据挖掘作业存储库;
@自动连线
应用程序jobRepository jobRepository;
@自动连线
调度程序;
@自动连线
作业调度定位器;
@自动连线
ListableBeanFactory beanFactory;
@凌驾
ApplicationEvent(ContextRefreshedEvent事件)上的公共无效{
info(“作业初始化程序已启动”);
//TODO:修改此调用以仅提取已完成和已启用的作业
对于(ApplicationJob ApplicationJob:jobRepository.findAll()){
if(applicationJob.getIsEnabled()&&(applicationJob.getIsCompleted()==null | | |!applicationJob.getIsCompleted()){
JobSchedulerUtil.schedule(新的JobContext(beanFactory、调度器、定位器、应用程序作业));
}
}
}
}
此类自动连接调度程序和存储库,该存储库获取实现ApplicationJob
接口的每个我的作业的实例。使用这些数据库记录中的信息,我可以使用调度程序实用程序重建作业
因此,基本上我手动将作业存储在数据库中,并通过在适当的bean中注入
调度器的实例来手动调度它们。要重新调度它们,我查询我的数据库,然后使用ApplicationListener
对它们进行调度,以说明容器的重新启动和启动。是的,我的项目是一个web应用程序。我想您应该阅读属性,以检查任务是否配置为在启动时调用。因此,您在重新启动服务器时加载了上下文,请阅读此属性并基于它启动您的作业非常感谢,我也在考虑类似的事情。我将按照您的情景模拟我的日程安排服务。@davioooh抱歉,我花了一段时间才回复,我一直很忙,但我不久前看到了您的问题,我遇到了同样的挑战。如果您需要帮助,我的电子邮件在我的个人资料中的博客上。非常感谢:)
public class JobInitializer implements ApplicationListener<ContextRefreshedEvent> {
Logger logger = LoggerFactory.getLogger(JobInitializer.class);
@Autowired
DataMiningJobRepository repository;
@Autowired
ApplicationJobRepository jobRepository;
@Autowired
Scheduler scheduler;
@Autowired
JobSchedulerLocator locator;
@Autowired
ListableBeanFactory beanFactory;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
logger.info("Job Initilizer started.");
//TODO: Modify this call to only pull completed & enabled jobs
for (ApplicationJob applicationJob : jobRepository.findAll()) {
if (applicationJob.getIsEnabled() && (applicationJob.getIsCompleted() == null || !applicationJob.getIsCompleted())) {
JobSchedulerUtil.schedule(new JobContext(beanFactory, scheduler, locator, applicationJob));
}
}
}
}