简体   繁体   English

如何在spring中使用@scheduled注释

[英]how to work with @scheduled annotation in spring

I have to read time of a file(video) created and if the time of file is greater than 3 days i have to delete the files. 我必须读取创建的文件(视频)的时间,如果文件的时间超过3天,我必须删除文件。 For this i have used @scheduled annotation in spring. 为此,我在春天使用了@scheduled注释。 But when i run the application, that deletion code is not working. 但是当我运行应用程序时,删除代码不起作用。 I have configured xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd <task:annotation-driven /> in spring xml file. 我已经配置了xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd <task:annotation-driven /> xml文件中的xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd <task:annotation-driven /> It is showing schema reference 4:failed to read. 它显示模式引用4:无法读取。 So i have decided to use complete annotation. 所以我决定使用完整的注释。 java version is 7 and spring version is 4.2.2. java版本是7,spring版本是4.2.2。 I have set cron expression at 4 PM and I ran application at 3.58 PM. 我在下午4点设置了cron表达式,然后我在下午3点28分运行了应用程序。 Nothing executed. 什么都没有执行。 What am I missing. 我错过了什么 What is the best package(controller/service/secondary logic) to place this code. 放置此代码的最佳包(控制器/服务/辅助逻辑)是什么。 And I would also like to know How to implement DELETE method. 我还想知道如何实现DELETE方法。 This is my code for deleting videos 这是我删除视频的代码

package com.test.logic;
import java.io.File;
import java.util.Date;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.sun.xml.internal.ws.developer.SchemaValidation;

@Component
@EnableScheduling
public class VideoDelete {
    @Scheduled(cron="* 1-15 16 * * * *")
    public void deleteVideo(){
    System.out.println("========> $$$$$$ scheduler method is executed $$$$$ <==========");
        long presentTime=System.currentTimeMillis();
        Date presentDate=new Date(presentTime);
        File file=new File("E://videorecording1");
        File[] fileList=file.listFiles();
        for (File files : fileList){
            if (files.isFile()){
                long filecreatedtime=files.lastModified();
                Date fileDate=new Date(filecreatedtime);
                long difference = presentDate.getTime() - fileDate.getTime();
                long hoursDifference = difference / (60 * 60 * 1000);
                if(hoursDifference >= 72)
                    files.delete();
            }
        }
}
}

Spring scheduler's cron allows only 6 kind of triggers. Spring调度程序的cron只允许6种触发器。

From the official documentation: 从官方文档:

A cron-like expression, extending the usual UN*X definition to include triggers * on the second as well as minute, hour, day of month, month and day of week. 一个类似cron的表达式,扩展了通常的UN * X定义,包括第二个以及分钟,小时,一天,一个月和一周中的触发器*。

To be able to support 7 trigger(including the YEAR) you have to use Quartz: https://www.quartz-scheduler.org/ 为了能够支持7触发器(包括YEAR),您必须使用Quartz: https//www.quartz-scheduler.org/

In your current case try this: 在你目前的情况下试试这个:

(cron = "0 0 16 * * ?")

我认为你会更好地尝试使用cron表达式"*/10 * * * * *这基本上意味着方法将每10秒执行一次,没有时间/日期/日限制。这样你就会看到弹簧是否有任何问题配置本身或文件权限。

I got the solution. 我得到了解决方案。 The reason is spring container is not recognizing VideoDelete class as a spring bean. 原因是spring容器没有将VideoDelete类识别为spring bean。 So i have annotated it with @controller and now it is working fine. 所以我用@controller对它进行了注释,现在它工作正常。

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

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