简体   繁体   English

在春季计划任务

[英]Scheduling Tasks in Spring

We have a requirement where we have to collect documents for X minutes in the database where X could be 30-60. 我们有一个要求,我们必须在X可能为30-60的数据库中收集X分钟的文档。 Once these documents are collected in the Database, we have to push them to a separate service over a REST call. 将这些文档收集到数据库中之后,我们必须通过REST调用将它们推送到单独的服务。

Since these documents won't be very large we aggregate them together and then push them together. 由于这些文档不会很大,因此我们将它们汇总在一起,然后将它们一起推送。

Now, these documents have to be pushed after a fixed interval, So I thought of creating a separate thread which pulls records from the database, pushes them to the service and once the service responds with 200 OK, I remove these records from the Database and the thread goes to Sleep for X minutes. 现在,必须在固定的时间间隔后推送这些文档,因此我想到了创建一个单独的线程,该线程从数据库中提取记录,将其推送到服务,并且一旦服务响应为200 OK,我就从数据库中删除这些记录,线程进入睡眠状态X分钟。

class PushDocumentsToService extends Thread{
    DocumentRepository documentRepository;
    DocumentProcessingService documentProcessingService
    public void run(){
        List<Document> list = documentRepository.getAllDocuments();
        Integer statusCode = documentProcessingService.sendDocuments(list);
        if(statusCode == 200)
            documentRepository.remove(list);
        try{
            Thread.sleep(30*60);
        }catch(..){..}
    }    

}

Now a few of my colleagues have advised me against using Thread.sleep in my code. 现在,我的一些同事已建议我不要在代码中使用Thread.sleep。 I am not able to understand the ill-effects of 我无法理解...的不良影响

Thread.sleep() Thread.sleep()

Also what methods do libraries and frameworks as Spring Scheduler and Google Guava use for Scheduling task? Spring Scheduler和Google Guava等库和框架还可以使用哪些方法来进行计划任务?

All you need to do here is use ScheduledExecutorService to schedule a thread and provide proper frequency rate like this. 您需要做的就是使用ScheduledExecutorService安排线程并提供适当的频率速率。

ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);    

Thread t1= new PushDocumentsToService();    
scheduledThreadPool.scheduleAtFixedRate(t1, 0, 30,TimeUnit.SECONDS);

If you have the Spring Scheduling library in your classpath, add the @Scheduled annotation to the method you wish to execute over a period of time. 如果您的类路径中有Spring Scheduling库,则将@Scheduled批注添加到希望在一段时间内执行的方法。 No need to extend Thread or implement Runnable ; 无需扩展Thread或实现Runnable this is all taken care of on your behalf through Spring. Spring会代您处理所有这些工作。

You know, for as long as the application is running . 您知道,只要应用程序正在运行

@Scheduled(cron = "30 * * * * *")
public void loadDataIntoRepository() {
    // your code
}

It is ill advised to use Thread.sleep for this for several reasons: 出于以下原因, 建议使用Thread.sleep

  • The thread executing your code is now blocked for the duration of the sleep period. 现在 ,在休眠期间,执行代码的线程将阻塞 30 minutes is a very long period of time to have your thread blocked. 30分钟是您的线程被阻塞的长时间。
  • Your thread could be interrupted, offering you no real way to recover from your error state. 您的线程可能会中断,无法为您提供从错误状态中恢复的真正方法。
  • You don't want to have to reinvent the wheel with scheduling when that functionality already exists and is already well-tested, for maintainability's sake. 出于可维护性的考虑,当功能已经存在并且已经过充分测试时,您不必通过计划重新设计轮子。

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

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