Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在时间窗口中运行开放式quartz作业,然后使用spring干净地退出_Spring_Quartz Scheduler - Fatal编程技术网

如何在时间窗口中运行开放式quartz作业,然后使用spring干净地退出

如何在时间窗口中运行开放式quartz作业,然后使用spring干净地退出,spring,quartz-scheduler,Spring,Quartz Scheduler,我有一个quartz任务,更新数据库中的一些记录。由于需要更新的记录数量巨大,它将在每天凌晨1点到3点之间的2小时内,以100个为一批进行更新 因此,如果它在凌晨3点截止日期到来时处理作业,我希望它完成处理当前批次的100条记录,然后干净地退出。如何做到这一点?是否可以单独使用cron表达式来完成,或者是否有某种机制来实现这种优雅的关闭 我正在使用spring的SchedulerFactoryBean和MethodInvokingJobDetailFactoryBean类在我的应用程序上下文xm

我有一个quartz任务,更新数据库中的一些记录。由于需要更新的记录数量巨大,它将在每天凌晨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毫秒

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
    }
}
public void updateRecords()
{
long jobStartedAt=System.currentTimeMillis();
long Twohourslated=jobStartedAt+2*60*60*1000;//2小时,60分钟/小时,60秒/分钟,1000毫秒/秒
while(System.currentTimeMillis()<2小时后)
{
//1.选择100条记录
//2.更新100项记录
//3.承诺
}
}
此外: 根据数据库属性的不同,批处理中的100个作业可能会有点小,也可能会有点效率。不要害怕尝试更大的批次


如果这些工作需要很长时间,可能是因为索引太重。如果有可能,请在执行大规模(哑)批处理之前删除索引,然后在批处理不明确需要索引的情况下重新生成索引。每次更新索引都会导致比一次更改记录多得多的磁盘写入。

此行->long Twohourslated=jobStartedAt+2*60*60*1000;这是我特别想要避免的。也就是说,如果以后我有一个不同的时间窗口,那么这一行或值为2*60*60*1000的相应配置都需要更改。有更灵活的选择吗?这将只允许最后一批处理干净地处理和退出?您可以将updateRecord作业包装在
java.util.concurrent.Future
中,并在两小时后由另一个计划的作业对其调用
cancel()