新建ScheduleConfig类
@Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer, AsyncConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
TaskScheduler taskScheduler = taskScheduler();
taskRegistrar.setTaskScheduler(taskScheduler);
}
/**
* 定时任务多线程处理
*/
@Bean(destroyMethod = "shutdown")
public ThreadPoolTaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(100);
scheduler.setThreadNamePrefix("task-");
scheduler.setAwaitTerminationSeconds(60);
scheduler.setWaitForTasksToCompleteOnShutdown(true);
return scheduler;
}
}
新建定时任务类
@ConditionalOnProperty(prefix = "scheduling", name = "log-with", havingValue = "true")
@Component
public class SyncLogJob {
private Logger logger = LogManager.getLogger(SyncLogJob.class);
@Autowired
private SyncLogService syncLogService;
@Scheduled(cron = "${scheduling.log-cron}")
private void configureTasks() {
logger.info("====================="+System.currentTimeMillis());
具体需要实现的业务
logger.info("====================="+System.currentTimeMillis());
}
}
在配置文件中配置定时任务
scheduling.log-with: true
scheduling.log-cron: 0/5 * * * * ?