Spring Boot 的定时任务

Spring Boot 的定时任务,实现定时删除,缓存中,已经翻译完成超过24小时的文件


public class ScheduledTask {
	//从yaml配置文件中获取time值 ,time可以随便设置多少, 此时time被设置24
    @Value("${trans-reserve-time.time}")
    private int time;

    /**
     * 定时任务,翻译完成后,如果翻译完成时间大于time时删除,time在配置文件中可配置
     */
     //设置定时任务的 定时时间  该表达式表示,每天的0点开始执行任务
    @Scheduled(cron = "0 0 0 * * ? ")
    public void deleteTransFile(){
    //工具类,获取缓存中的文件对象。
        Map<String, DocumentTransBo> allFileInfo = DocumentTransInfoUtils.getAllFileInfo();
        if (CollectionUtil.isNotEmpty(allFileInfo)){
            for (String key:allFileInfo.keySet()){
                DocumentTransBo documentTransBo = allFileInfo.get(key);
                //判断翻译状态,4为翻译完成,如果不是4,直接break
                if (documentTransBo.getTransStatus()!=4){
                    break;
                }
                //将documtTransBo对象中翻译完成时间转换为毫秒数
                long transDoneTime = documentTransBo.getTransDoneTime().atZone(ZoneId.of("Asia/Shanghai")).toInstant().toEpochMilli();
                //判断(当前时间-减去文章翻译完成时的时间)除以3600000是将毫秒数转为小时
                //time=24  >24说明是大于等于24小时,>=24的话(System.currentTimeMillis()-transDoneTime)/3600000需要+1
                if ((System.currentTimeMillis()-transDoneTime)/3600000>time){
             //调用一个自写工具类,调用的方法是 将该对象从缓存中移除
                    DocumentTransInfoUtils.deleteFilePathDocumentTransBo(documentTransBo.getFileUuid());
                }
            }
        }


    }
}


定时任务,cron的在线工具https://tool.lu/crontab/

https://tool.lu/crontab/ 点击上方跳转

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值