Reputation: 151
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. For this i have used @scheduled annotation in spring. 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. It is showing schema reference 4:failed to read. So i have decided to use complete annotation. java version is 7 and spring version is 4.2.2. I have set cron expression at 4 PM and I ran application at 3.58 PM. 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. 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();
}
}
}
}
Upvotes: 3
Views: 1048
Reputation: 151
I got the solution. The reason is spring container is not recognizing VideoDelete class as a spring bean. So i have annotated it with @controller and now it is working fine.
Upvotes: 1
Reputation: 51
I think you will be better of trying with cron expression "*/10 * * * * *
which basically means method will be executed every 10 seconds with no hour/date/day limitation.
This way you will see if there's anything wrong with spring configuration itself or files permission.
Upvotes: 2
Reputation: 4391
Spring scheduler's cron allows only 6 kind of triggers.
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.
To be able to support 7 trigger(including the YEAR) you have to use Quartz:https://www.quartz-scheduler.org/
In your current case try this:
(cron = "0 0 16 * * ?")
Upvotes: 3