[英]How to run an open ended quartz job in a time window and then exit cleanly with spring
我有一个石英作业,可以更新数据库中的某些记录。 由于要更新的记录数量巨大,因此它将每天晚上1点至凌晨3点之间分2个小时分100个批处理。
因此,如果在凌晨3点截止日期之前正在处理作业,我希望它完成当前批次的100条记录的处理,然后干净地退出。 如何做到这一点? 可以单独使用cron表达式完成此操作,还是可以通过某种机制来实现正常关机?
我正在使用spring的SchedulerFactoryBean
和MethodInvokingJobDetailFactoryBean
类在我的应用程序上下文xml中配置作业。
我的工作执行方法的一些伪代码。
public void updateRecords()
{
while(true) // i need to replace true with some other logic to exit from loop at scheduled time.
{
// 1. select 100 records
// 2. update 100 records
// 3. commit
}
}
运行updateRecords时,存储System.currentTimeMillis,并在while循环中查看时间是否已超过该时间+ 2 * 60 * 60 * 1000 ms。
public void updateRecords()
{
long jobStartedAt = System.currentTimeMillis();
long twoHoursLater = jobStartedAt + 2 * 60 * 60 * 1000; //2 hours, 60 minutes/hour, 60 seconds/minute, 1000 ms/second
while(System.currentTimeMillis() < twoHoursLater)
{
// 1. select 100 records
// 2. update 100 records
// 3. commit
}
}
另外:根据您的数据库属性,一批中的100个作业可能有点小到高效。 不要害怕尝试大批量。
如果工作需要很长时间,那可能是因为索引过多。 如果有可能,请在进行大规模(哑)批处理之前先删除索引,然后再进行重建(如果批处理不需要显式的索引)。 索引的每次更新可以比记录的单个更改呈现更多的磁盘写入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.