zoukankan      html  css  js  c++  java
  • springboot定时任务

    写法一:

    import com.xxx.entity.ByteDanceDataNode;
    import com.xxx.service.ByteDanceDataService;
    import com.xxx.service.DistrictService;
    import com.xxx.service.StatusService;
    import com.xxx.util.DateUtil;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.util.StringUtils;
    
    import java.io.IOException;
    import java.util.List;
    
    @Slf4j
    @Configuration
    @EnableScheduling
    public class ByteDanceFetchTask {
    
        @Autowired
        ByteDanceDataService dataService;
    
        @Autowired
        StatusService statusService;
    
        @Autowired
        DistrictService districtService;
    
    
        /**
         * 10分钟跑一次
         * @throws IOException
         */
        @Scheduled(fixedDelayString = "${task.initialDelay}", initialDelayString = "${task.fixedDelay}")
        public void doWork() throws IOException {
    
            log.info("正在执行定时任务");
            String json = dataService.getAllJsonData();
            if (StringUtils.isEmpty(json)) {
                log.error("json下载出错");
                return ;
            }
            ByteDanceDataNode node = dataService.getByteDanceDataNode(json);
    
            if (node == null) {
                log.error("json格式升级,解析根节点json出错");
                return ;
            }
    
            List<ByteDanceDataNode.ProvincesBean> provinces = node.getProvinces();
    
            if (provinces == null)
            {
                log.error("json格式升级,解析城市出错");
                return ;
            }
    
    
            String updateTime = DateUtil.epochSecondToFormattedDateTime(node.getUpdateTime());
    
            dataService.saveData(updateTime,provinces);
    
            statusService.saveStatus("lastUpdateTime",updateTime);
    
            dataService.computeWeight();
    
            log.info("定时任务执行完成");
        }
    
    }

    对应的配置文件

    application.properties

    #1*60*1000
    task.initialDelay=60000
    #10*60*1000
    task.fixedDelay=600000

    写法二:

    import com.xxx.entity.ByteDanceDataNode;
    import com.xxx.service.ByteDanceDataService;
    import com.xxx.service.DistrictService;
    import com.xxx.service.StatusService;
    import com.xxx.util.DateUtil;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.util.StringUtils;
    
    import java.io.IOException;
    import java.util.List;
    
    @Slf4j
    @Configuration
    @EnableScheduling
    public class ByteDanceFetchTask {
    
        @Autowired
        ByteDanceDataService dataService;
    
        @Autowired
        StatusService statusService;
    
        @Autowired
        DistrictService districtService;
    
    
        /**
         * 5分钟跑一次
         * @throws IOException
         */
        @Scheduled(fixedDelay = 1 * 60 * 1000, initialDelay = 10 * 60 * 1000)
        public void doWork() throws IOException {
    
            log.info("正在执行定时任务");
            String json = dataService.getAllJsonData();
            if (StringUtils.isEmpty(json)) {
                log.error("json下载出错");
                return ;
            }
            ByteDanceDataNode node = dataService.getByteDanceDataNode(json);
    
            if (node == null) {
                log.error("json格式升级,解析根节点json出错");
                return ;
            }
    
            List<ByteDanceDataNode.ProvincesBean> provinces = node.getProvinces();
    
            if (provinces == null)
            {
                log.error("json格式升级,解析城市出错");
                return ;
            }
    
    
            String updateTime = DateUtil.epochSecondToFormattedDateTime(node.getUpdateTime());
    
            dataService.saveData(updateTime,provinces);
    
            statusService.saveStatus("lastUpdateTime",updateTime);
    
            dataService.computeWeight();
    
            log.info("定时任务执行完成");
        }
    
    }
  • 相关阅读:
    SaltStack概述及安装
    Zabbix高可用
    Zabbix数据库表结构
    asp select count(*) 用 open还是excute
    asp+jquery+ajax,asp后台程序执行不正常
    aspupload ,在winows server 2008 下无法使用
    jquery 如何使用innerHTML
    thinkphp的select和find的区别(转)
    centos安装PHP-pdo支持pdo
    Excel 2007 打开 UTF-8 编码 CSV 文件的乱码BUG
  • 原文地址:https://www.cnblogs.com/passedbylove/p/12401476.html
Copyright © 2011-2022 走看看