目录
一、通过实现SchedulingConfigurer接口的方法实现定时任务
就这一个类…
import org.springframework.context.annotation.Configuration;
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 java.time.LocalDateTime;
@Configuration
@EnableScheduling // 开启定时任务
public class MyScheduledTask implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
/**
* Runnable task要执行的任务, Trigger trigger执行周期
*/
taskRegistrar.addTriggerTask(
() -> System.out.println("任务执行中" + LocalDateTime.now().toLocalTime()),
(a)->{
// 此处可以书写数据库访问逻辑
// String cron = tempMapper.selectById(1);
// return new CronTrigger(cron).nextExecutionTime(a);
return new CronTrigger("0/3 * * * * ?").nextExecutionTime(a);
}
);
}
}
二、@Scheduled注解(springboot定时任务默认使用线程池)
2.1单线程
import com.jule.springtask.service.SelectService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class TestController {
@Resource
private SelectService selectService;
@RequestMapping("/")
public void send(){
selectService.send();
}
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
@Slf4j
@EnableScheduling
@Service
public class SelectService {
@Scheduled(cron = "0/3 * * * * ?")
public void send(){
log.info("定时任务:" + LocalDateTime.now().toLocalTime());
}
}
2.2线程池(不是定时任务的多线程)
import com.jule.springtask.service.SelectService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class TestController {
@Resource
private SelectService selectService;
@RequestMapping("/")
public void send(){
selectService.send();
}
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
@Slf4j
@EnableScheduling
@Service
public class SelectService {
@Scheduled(cron = "0/3 * * * * ?")
@Async("poolTaskSMSExecutor")
public void send(){
log.info("定时任务:" + LocalDateTime.now().toLocalTime());
}
}
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;
import java.util.concurrent.ThreadPoolExecutor;
/**
* 线程池
*/
@EnableAsync
@Configuration
public class TaskPoolConfig {
/**
* 多线程线程池配置
*/
@Bean("poolTaskSMSExecutor")
public Executor mailNumPoolSingleTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);//核心线程数:线程池创建时候初始化的线程数
executor.setMaxPoolSize(100);//最大线程数:线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程
executor.setQueueCapacity(20);//缓冲队列200:缓冲执行任务的队列
executor.setKeepAliveSeconds(60);//允许线程的空闲时间60秒:当超过了核心线程出之外的线程在空闲时间到达之后会被销毁
executor.setThreadNamePrefix("预约短信发送-");//线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());// 线程池对拒绝任务的处理策略
return executor;
}
}