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==================");
}
技术交流
微信号:vxhqqh