SpringBoot手动开启或者关闭定时任务

675 阅读2分钟

小知识,大挑战!本文正在参与“ 程序员必备小知识 ”创作活动

本文同时参与 「掘力星计划」 ,赢取创作大礼包,挑战创作激励金

Controller层定时任务控制器代码

import com.tarzan.reptile.service.DynamicTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
 
/**
 * 定时任务控制器
 *
 * @author tarzan
 * @version 1.0
 * @date 2020/8/5$ 10:25$
 * @since JDK1.8
 */
@Controller
public class DynamicTaskController {
 
    @Autowired
    private DynamicTaskService taskService;
 
 
    @GetMapping("/task/start")
    private void start(Model model){
        taskService.startCron();
    }
 
    @GetMapping("/task/stop")
    private void stop(Model model){
        taskService.stopCron();
    }
}

sevice层定时任务开启关闭方法

import com.tarzan.reptile.mapper.CronDao;
import com.tarzan.reptile.task.RunnableTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;
 
import java.util.concurrent.ScheduledFuture;
 
 
/**
 * @author tarzan
 * @version 1.0
 * @date 2020/8/5$ 10:07$
 * @since JDK1.8
 */
@Slf4j
@Service
public class DynamicTaskService {
 
 
    @Autowired
    private CronDao cronDao;
 
    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;
 
    private ScheduledFuture<?> future;
 
    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        return new ThreadPoolTaskScheduler();
    }
 
    public  void startCron() {
        String cron=cronDao.getCron();
        if(StringUtils.isBlank(cron)){
            log.error("定时任务开启失败");
        }else{
            future = threadPoolTaskScheduler.schedule(new RunnableTask(), triggerContext -> new CronTrigger(cron).nextExecutionTime(triggerContext));
            log.info("定时任务开启成功");
        }
    }
 
    public void stopCron() {
        if (future != null) {
            future.cancel(true);
        }
        log.info("定时任务关闭");
    }
 
}

Dao层代码

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
 
/**
 * @author tarzan
 * @version 1.0
 * @date 2020/8/5 11:23
 * @since JDK1.8
 */
@Mapper
@Component
public interface CronDao {
 
    @Select("select cron from t_cron limit 1")
    String getCron();
}

runable线程代码

import com.tarzan.reptile.service.ReptileService;
import com.tarzan.reptile.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
 
/**
 * @author tarzan
 * @version 1.0
 * @date 2020/8/5$ 10:12$
 * @since JDK1.8
 */
@Component
@Slf4j
public class RunnableTask implements Runnable {
 
    @Autowired
    private ReptileService reptileService= SpringUtils.getBean("reptileService");
 
    private Integer i=0;
 
    @Override
    public void run() {
        i++;
        reptileService.crawling();
        log.info("===================第"+i+"轮======================");
        log.info("=============第"+i+"轮采集数据start================");
        //reptileService.crawling();
        log.info("=============第"+i+"轮采集数据end==================");
    }

「欢迎在评论区讨论,掘金官方将在掘力星计划活动结束后,在评论区抽送100份掘金周边,抽奖详情见活动文章」。

  • 抽奖礼物:100份,掘金官方提供,包括掘金徽章、拖鞋、马克杯、帆布袋等,随机发放
  • 抽奖时间:「掘力星计划」活动结束后,预计3个工作日内,官方将在所有符合规则的活动文章评论区抽出
  • 抽奖方式:掘金官方随机抽奖+人工核实
  • 评论内容:与文章内容相关的评论、建议、讨论等,「踩踩」「学习了」等泛泛类的评论无法获奖