zoukankan      html  css  js  c++  java
  • springboot项目,启动项目后启动的定时器,定时执行任务

    package com.wiscom.ism.webapi.ismController;
    
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Service;
    
    import java.io.File;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    
    @Component
    public class DeleteImgController implements ApplicationRunner {
    
        /*
         * 定时删除以图搜图上传到static upload images search 文件夹下的照片
         * d定时每天的23点59分删除
         * */
    
    //项目启动后执行的方法 @Override
    public void run(ApplicationArguments args) throws Exception { startDeleteImgService();// }
    //定时器
    public void startDeleteImgService() { System.out.println("启动删除===="); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 0); Date time = calendar.getTime(); Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { File file = new File("static/upload/images/search"); if (file.exists()) { String[] list = file.list(); for (String name : list) { File f = new File("static/upload/images/search", name); if (f.isFile()) { f.delete(); } } } } }, time, 1000 * 60 * 60 * 24); } //执行时间 间隔时间 }
  • 相关阅读:
    Java学习——HashMap
    git 常用命令
    java 正则表达式
    java 读写文件
    python Shapely 使用指南
    java 删除目录、 文件
    Java maven安装GDAL
    postgresql运维
    (转)媒体格式分析之flv -- 基于FFMPEG
    (转)rtmp协议简单解析以及用其发送h264的flv文件
  • 原文地址:https://www.cnblogs.com/maocai2018/p/10122291.html
Copyright © 2011-2022 走看看