spring boot 定时任务
/** * Created by Admin on 2016/4/28. */ @Service("flowService") @Component public class FlowServiceImpl implements FlowService{ protected Logger log = LoggerFactory.getLogger(getClass()); @Autowired private FlowCountRepository flowCountRepository; @Autowired private ScopConfig scopConfig; @Override @Scheduled(cron = "${scop.exportTime}") public void export1(){ /** * 如果没有文件夹,则创建 */ SimpleDateFormat fm=new SimpleDateFormat("yyyyMMddHHmmss"); String urlName=scopConfig.getFlowCount(); log.debug("导出文件存放的路劲为"+urlName); File file = new File(urlName); if (!file.exists()) { file.mkdir(); }
application.properties 中
#流量统计定时导出时间
每天12点04执行导出
scop.exportTime=0
04 12 ? * *
如果你的项目中,统一时间点有多个定时任务要执行,那么就可能出现一个任务执行时间过长占用线程资源,导致其他任务无法执行。需要加如下的类:
/** * Created by zhiqi.shao on 2017/12/7. */ @Slf4j @Configuration @EnableConfigurationProperties @AutoConfigureAfter public class TaskConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { } @Bean(destroyMethod = "shutdown") public ThreadPoolTaskScheduler taskExecutor() { ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.setPoolSize(4); scheduler.initialize(); return scheduler; } }