Springboot集成schedule内置任务调度@Scheduled(cron=”5 * * * * ?”)

导读:本篇文章讲解 Springboot集成schedule内置任务调度@Scheduled(cron=”5 * * * * ?”),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Spring内置任务调度使用是非常简便的,只需要一个类,三个注解就ok。
  • 类上方加 @Component,标识为Bean。
  • 方法上加 @Scheduled(fixedRate = 5000),我这个写的是5秒调度一次,也可以写成cron表达式。如:@Scheduled(cron=“5 * * * * ?”)
    cron表达式是由7个空格分隔的时间元素组成的(分别表示:秒分时日月周年),一般最后一个年都省略不写。不多说,想更深入了可以了解一下Quartz.
  • 启动项上加 @EnableScheduling
package com.mycompany.myapp.schedule;

import com.mycompany.myapp.domain.AssetEquipment;
import com.mycompany.myapp.service.AssetEquipmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author wangyan@163.com
 * @version 1.0
 * @date 2019-08-29 09:16
 */
@Component
public class OutTimeAssetEquipmentScheduleConfig {
    private static final Logger logger = LoggerFactory.getLogger(OutTimeAssetEquipmentScheduleConfig.class);
    private final AssetEquipmentService assetEquipmentService;

    public OutTimeAssetEquipmentScheduleConfig(AssetEquipmentService assetEquipmentService) {
        this.assetEquipmentService = assetEquipmentService;
    }

    @Scheduled(fixedRate = 5000)
    public void cronJobSchedule() {
        List<AssetEquipment> outTimeAssetEquipmentList = assetEquipmentService.systemOutTimeAssetEquipmentList();
        outTimeAssetEquipmentList.stream().forEach(item -> {
            assetEquipmentService.updateOutTimeAssetEquipmentFlag(item.getId());
        });
        List<AssetEquipment> preGuaranteeAssetEquipmentList = assetEquipmentService.preGuaranteeAssetEquipmentList();
        preGuaranteeAssetEquipmentList.stream().forEach(item -> {
            assetEquipmentService.systemPreGuaranteeAssetEquipmentFlag(item.getId());
        });
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date now = new Date();
        logger.info("XXXX任务调度执行:" + sdf.format(now));
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/115801.html

(0)
Java光头强的头像Java光头强

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!