springboot集成定时器
背景
今天公司有个业务,需要定时扫描表中未执行的数据,晚上就研究一下springboot如何整合自带的定时器
- 首先用的springboot自带的不需要引入jar包
- 我考虑到可能后期还会有定时器业务,所以我直接做的是多线程定时器。并且有对mapper的调用。话不多说直接上干货
package com.yanhua.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@Configuration
@EnableAsync
public class AsyncConfig {
/*
此处成员变量应该使用@Value从配置中读取
*/
private int corePoolSize = 2;
private int maxPoolSize = 20;
private int queueCapacity = 10;
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.initialize();
return executor;
}
}
- 业务代码
package com.yanhua.demo.service.serviceImpl;
import com.yanhua.demo.service.responsitory.ItemResponsitory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@EnableScheduling
@Component
@EnableAsync
public class TimerServiceImpl {
@Autowired
private ItemResponsitory itemResponsitory;
private int k = 5;
@Async
@Scheduled(fixedDelay = 1000)
public void timerTest() {
long count = itemResponsitory.count();
System.out.println("thread1=============" + count);
}
@Async
@Scheduled(fixedDelay = 2000)
public void timerTest1() throws Exception {
long count = itemResponsitory.count();
Thread.sleep(3000);
System.out.println("线程2=============" + count);
k++;
if(k >= 5){
throw new Exception("error");
}
}
}
说明:
fixedDelay:执行完成之后延迟多久执行
fixedRate:间隔几秒执行(不一定上一次定时器已经执行完成)
cron:达到设置时间条件时触发
fixedDelayString:设置可从配置文件取值(“${a.b}”)
fixedRateString:同上
然鹅就那么溜的就整合完毕~
第二天天有不测风云,领导告诉我说得把时间配置到数据库中。蒙圈中的我继续研究,终于在一篇博客中柳暗花明。以下代码多为copy
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
@EnableScheduling
public class TaskCronChange implements SchedulingConfigurer{
public static String cron;
@Autowired
private Amapper mapper;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
//项目部署时,会在这里执行一次,从数据库拿到cron表达式
cron = mapper.getCronTime();
Runnable task = new Runnable() {
@Override
public void run() {
//任务逻辑代码部分.
System.out.println("come here~");
}
};
Trigger trigger = new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
//任务触发,可修改任务的执行周期.
//每一次任务触发,都会执行这里的方法一次,重新获取下一次的执行时间
cron = mapper.getCronTime();
CronTrigger trigger = new CronTrigger(cron);
Date nextExec = trigger.nextExecutionTime(triggerContext);
return nextExec;
}
};
taskRegistrar.addTriggerTask(task, trigger);
}
}
嗷呜 再次完成了领导的需求,打完收工~
946




被折叠的 条评论
为什么被折叠?



