SpringBootSchedule配置
1. 定时任务实现⽅式
定时任务实现⽅式:
Java⾃带的java.util.Timer类,这个类允许你调度⼀个java.util.TimerTask任务。使⽤这种⽅式可以让你的程序按照某⼀个频度执⾏,但不能在指定时间运⾏。⼀般⽤的较少,这篇⽂章将不做详细介绍。
使⽤Quartz,这是⼀个功能⽐较强⼤的的调度器,可以让你的程序在指定时间执⾏,也可以按照某⼀个频度执⾏,配置起来稍显复杂,有空介绍。
SpringBoot⾃带的Scheduled,可以将它看成⼀个轻量级的Quartz,⽽且使⽤起来⽐Quartz简单许多,本⽂主要介绍。
定时任务执⾏⽅式:
单线程(串⾏)
多线程(并⾏)
2. 创建定时任务
package com.st;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.autonavi.task.ScheduledTasks;
@Component
public class ScheduledTest {
private static final Logger logger = Logger(ScheduledTasks.class);
@Scheduled(cron="0 0/2 * * * ?")
public void executeFileDownLoadTask() {
// 间隔2分钟,执⾏任务
Thread current = Thread.currentThread();
System.out.println("定时任务1:"+Id());
logger.info("uteFileDownLoadTask 定时任务1:"+Id()+ ",name:"+Name());
}
}
@Scheduled 注解⽤于标注这个⽅法是⼀个定时任务的⽅法,有多种配置可选。cron⽀持cron表达式,指定任务在特定时间执⾏;fixedRate 以特定频率执⾏任务;fixedRateString以string的形式配置执⾏频率。
3. 启动定时任务
@SpringBootApplication
@EnableScheduling
public class App {
private static final Logger logger = Logger(App.class);
public static void main(String[] args) {
SpringApplication.run(App.class, args);
logger.info("start");
}
}
其中 @EnableScheduling 注解的作⽤是发现注解@Scheduled的任务并后台执⾏。
Springboot本⾝默认的执⾏⽅式是串⾏执⾏,也就是说⽆论有多少task,都是⼀个线程串⾏执⾏,并⾏需⼿动配置
4. 并⾏任务
继承SchedulingConfigurer类并重写其⽅法即可,如下:
@Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
}
@Bean(destroyMethod="shutdown")
public Executor taskExecutor() {
wScheduledThreadPool(100);
}
}
再次执⾏之前的那个Demo,会欣然发现已经是并⾏执⾏了! 
4. 异步并⾏任务
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.urrent.ThreadPoolTaskScheduler; import org.fig.ScheduledTaskRegistrar;
@Configuration
@EnableScheduling
@EnableAsync(
mode = AdviceMode.PROXY, proxyTargetClass = false,
order = Ordered.HIGHEST_PRECEDENCE
)
@ComponentScan(
basePackages = "hello"
)
public class RootContextConfiguration implements
AsyncConfigurer, SchedulingConfigurer {
@Bean
public ThreadPoolTaskScheduler taskScheduler()
{
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.setPoolSize(20);
scheduler.setThreadNamePrefix("task-");
scheduler.setAwaitTerminationSeconds(60);
scheduler.setWaitForTasksToCompleteOnShutdown(true);
return scheduler;
}
@Override
public Executor getAsyncExecutor()
{springframework和springboot
Executor executor = this.taskScheduler();
return executor;
}
@Override
public void configureTasks(ScheduledTaskRegistrar registrar)
{
TaskScheduler scheduler = this.taskScheduler();
registrar.setTaskScheduler(scheduler);
}
}
在启动的main⽅法加⼊额外配置:
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext rootContext =
new AnnotationConfigApplicationContext();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。