Rely on the version
Spring Boot version: 1.5.18
Reference documentation
Springboot Scheduled tasks @scheduled and asynchronous @async
Springboot configuration
# schedule
cron.one=0 30 10 * * ?
Copy the code
The Spring configuration of the Schedule
import org.slf4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.IntervalTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.ScheduledMethodRunnable;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledThreadPoolExecutor;
@Configuration
@EnableScheduling
public class ScheduleConfigurer implements SchedulingConfigurer {
private static final Logger log = org.slf4j.LoggerFactory.getLogger(ScheduleConfigurer.class);
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
/** * Prints the task */ at startup
List<CronTask> cronTasks = taskRegistrar.getCronTaskList();
List<IntervalTask> fixedDelayTasks = taskRegistrar.getFixedDelayTaskList();
List<IntervalTask> fixedRateTasks = taskRegistrar.getFixedRateTaskList();
for (CronTask cronTask : cronTasks){
String expression = cronTask.getExpression();
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) cronTask.getRunnable();
log.info("method: {},expression: {}",runnable.getMethod(),expression);
}
for (IntervalTask intervalTask : fixedDelayTasks){
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable)intervalTask.getRunnable();
log.info("method: {},InitialDelay: {}",runnable.getMethod(),intervalTask.getInitialDelay());
}
for (IntervalTask intervalTask : fixedRateTasks){
long interval = intervalTask.getInterval();
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable)intervalTask.getRunnable();
log.info("method: {},Initial: {}",runnable.getMethod(),intervalTask.getInterval()); }}@Bean(destroyMethod="shutdown")
public Executor taskExecutor(a) {
/ / thread pool
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(5);
//scheduledThreadPoolExecutor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
// @Override
// public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
//
/ /}
/ /});
returnscheduledThreadPoolExecutor; }}Copy the code
Setting a Scheduled Task
import org.slf4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduleOne {
private static final Logger log = org.slf4j.LoggerFactory.getLogger(ScheduleOne.class);
@Scheduled(cron = "${cron.one}")
public void test(a) {
log.info("Scheduled task begins."); }}Copy the code