java 定时任务简单实现

使用注解@Configuration
@EnableScheduling 定时任务注解

@Async异步注解

@Scheduled(cron = "0/5 * * * * ? ")定时任务执行

每天都执行一次,如果命中条件

package com.tsit.ertai.web.core.config;

import com.tsit.common.core.domain.entity.RecoverFootageEntity;
import com.tsit.system.domain.dto.RecoverFootageDTO;
import com.tsit.system.service.RecoverFootageService;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 手动回采的定时任务
 */
@Async
@Component
@Configuration
@EnableScheduling
public class RecoverScheduled {
    @Resource
    private RecoverFootageService recoverFootageService;

        @Scheduled(cron = "0/5 * * * * ? ")
//    @Scheduled(cron = "0 0 0 * * ? ")
    public void recoveryFootageScheduled() {
        //获取当前日期
        Date date = new Date();
        //将时间格式化成yyyy-MM-dd HH:mm:ss的格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //创建Calendar实例
        Calendar cal = Calendar.getInstance();
        //设置当前时间
        cal.setTime(date);
        //减去一天的方法:
        cal.add(Calendar.DATE, -1);
        String format1 = format.format(cal.getTime());
        String format2 = format.format(date);
        System.out.println(format1);
        System.out.println(format2);
        try {
            Long startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(format1).getTime();
            Long endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(format2).getTime();
            List<RecoverFootageEntity> recoverFootageEntities = recoverFootageService.queryCurrentDay(startTime, endTime);
            //判断集合是否为空,为空就新增一条数据
            //TODO 新增的是哪个工作面数据
            if (CollectionUtils.isEmpty(recoverFootageEntities)) {
                RecoverFootageDTO recoverFootageDTO = new RecoverFootageDTO();
                recoverFootageDTO.setFlag("1");//未填写的数据
                recoverFootageDTO.setCreateTime(System.currentTimeMillis()*1000-3600*1000);//精确到毫秒
                recoverFootageDTO.setMiningTime(System.currentTimeMillis()*1000-3600*1000);
                recoverFootageDTO.setMiningPace(BigDecimal.ZERO);
                recoverFootageDTO.setFaceId(5000l);
                recoverFootageService.insert(recoverFootageDTO);
                System.out.println("当天为查到数据,新增成功");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        System.out.println("定时任务开始=============");

    }
}

2 @Scheduled从配置文件读取时间,且执行不玩,不会执行下个执行计划

 @Scheduled(fixedDelayString = "#{${scheduled.time}*1000}")

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没伞的孩子努力奔跑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值