简体   繁体   English

如何在时间窗口中运行开放式石英作业,然后用弹簧干净地退出

[英]How to run an open ended quartz job in a time window and then exit cleanly with spring

I have a quartz job that updates some records in a database. 我有一个石英作业,可以更新数据库中的某些记录。 Since the number of records to update is huge, it will do it in batches of 100 for a period of 2 hours every night between 1 and 3 AM. 由于要更新的​​记录数量巨大,因此它将每天晚上1点至凌晨3点之间分2个小时分100个批处理。

So if it is processing a job when the 3 AM deadline reaches, I would like it to finish processing the current batch of 100 records and then exit cleanly. 因此,如果在凌晨3点截止日期之前正在处理作业,我希望它完成当前批次的100条记录的处理,然后干净地退出。 How can this be achieved? 如何做到这一点? Can it be done using a cron expression alone or is there some mechanism to achieve this graceful shutdown? 可以单独使用cron表达式完成此操作,还是可以通过某种机制来实现正常关机?

I am using spring's SchedulerFactoryBean and MethodInvokingJobDetailFactoryBean classes to configure the job in my application context xml. 我正在使用spring的SchedulerFactoryBeanMethodInvokingJobDetailFactoryBean类在我的应用程序上下文xml中配置作业。

Some pseudo code of my job implementation method. 我的工作执行方法的一些伪代码。

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
    }
}

When you run the updateRecords, store the System.currentTimeMillis and see if the time has exceeded that time + 2*60*60*1000 ms in the while loop. 运行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
    }
}

Also: Depending on your database properties, 100 jobs in a batch could be a bit to small to efficient. 另外:根据您的数据库属性,一批中的100个作业可能有点小到高效。 Don't be afraid to try larger batches. 不要害怕尝试大批量。

And if the jobs takes very long it could be because of heavy indexing. 如果工作需要很长时间,那可能是因为索引过多。 If you have the possibility, drop the indexes before doing large scale (dumb) batching and rebuild them afterwards, if they are not explicitly needed for the batch processing. 如果有可能,请在进行大规模(哑)批处理之前先删除索引,然后再进行重建(如果批处理不需要显式的索引)。 Every update of indexes can render many more diskwrites than the single change of a record. 索引的每次更新可以比记录的单个更改呈现更多的磁盘写入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM