SSM + Quartz achieve timer (in conjunction with the article, do regularly delete expired images)

Previous article to achieve the picture upload function, there needs to be a deletion of expired picture function, the expiration time is tentatively scheduled for two weeks, namely 14 days

 

pom.xml added:

  <!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.3.0</version>
        </dependency>

spring configuration file:

    <! - definition of the target bean and bean Method -> 
    < bean ID = "SpringQtzJob" class = "yi.survey.DeleteQuartz"  />   <! - projects to be executed ->

     < bean ID = "SpringQtzJobMethod" 
          class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > 
        < Property name = "targetObject" > 
            < ref bean = "SpringQtzJob"  />  <-! bean to execute -> 
        </ Property > 
        <property name="targetMethod">  <!--To perform the method name -> 
            < value > the Delete </ value > 
        </ Property > 
    </ bean > 
    <-! ====================== == scheduling trigger ======================== -> 
    < the bean ID = "CronTriggerBean" class = "org.springframework.scheduling.quartz. CronTriggerFactoryBean " > 
        < Property name =" jobDetail " ref =" SpringQtzJobMethod " > </ Property >   <-! program to be executed -> 
        <property name="cronExpression"value = "0 0 0 * *?" > </ Property >   <-! trigger start time is 0:00 this day -> 
    </ bean > 

    <-! ============ ============ scheduling plant ======================== -> 
    < the bean ID = "SpringJobSchedulerFactoryBean" 
          class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" > 
        < Property name = "triggers" > 
            < List > 
                < REF the bean = "CronTriggerBean"  />   <-! performed trigger ->
            </list>
        </property>
    </bean>

 DeleteQuartz.java class

public class DeleteQuartz    {
    public  void delete(){
         System.out.println(new Date()+"触发定时器");
          String basePath= System.getProperty("SMBMMVC.root");
          String imgPath=basePath+"statics/img";
          File file=new File(imgPath);
          File[] files = file.listFiles();
          for (File file1 : files) {
              String fileName=file1.getName();
              if(!fileName.contains("timeOut")){
                  BaseName String = FilenameUtils.getBaseName (fileName); 
                  String [] S = baseName.split ( "_" ); 
                  a Date DATE = new new a Date (Long.parseLong (S [2 ]));   // Get Picture upload date
                   // current date 
                  Calendar Calendar = Calendar.getInstance (); 
                  a date nowtime = calendar.getTime (); 

                  // image upload date of the first 14 days of the date 
                  calendar.setTime (dATE); 
                  calendar.set (Calendar.DAY_OF_YEAR, Calendar.get (Calendar. DAY_OF_YEAR) +14 ); 
                  a Date outTime= Calendar.getTime ();
                   IF (nowTime.after (outTime)) { // current date is after the expiration date 
                      System.out.println ( "Delete expired Photo:" + fileName); 
                      file1.delete (); 
                  } 
              } 
          } 
    } 
}

 

Guess you like

Origin www.cnblogs.com/yhood/p/11447005.html