Spring Boot 定时任务之多线程
2018-03-05 00:00
579 查看
首先,你需要的注解(关注最后两个注解即可,其他无关):
然后是一个配置类:
然后是一个Task:
@SpringBootApplication @EnableTransactionManagement @EnableSwagger2Doc @EnableCaching @ComponentScan(basePackages = "cn.xxx") @EnableAsync @EnableScheduling public class WebApplication { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } }
然后是一个配置类:
@Configuration public class TaskExcutorConfig implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); threadPoolTaskExecutor.setMaxPoolSize(20); threadPoolTaskExecutor.setCorePoolSize(5); threadPoolTaskExecutor.setMaxPoolSize(100); threadPoolTaskExecutor.initialize(); return threadPoolTaskExecutor; } @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return new MyAsyncExceptionHandler(); } class MyAsyncExceptionHandler implements AsyncUncaughtExceptionHandler { @Override public void handleUncaughtException(Throwable throwable, Method method, Object... obj) { //do something } } }
然后是一个Task:
@Component public class WorTask { @Autowired private RedisTemplate redisTemplate; /** * 每3秒运行一次 */ @Scheduled(fixedDelay = 3000) @Async public void spiderWordsByUrlFromRedis() { System.out.println("Test"); try { Thread.sleep(300000); } catch (InterruptedException e) { e.printStackTrace(); } } }
相关文章推荐
- springboot动态配置定时任务2种方式,整合Quartz多线程并发执行多个定时任务配置
- springboot动态配置定时任务2种方式,整合Quartz多线程并发执行多个定时任务配置
- spring-boot | 多线程并发定时任务
- spring-boot实战【09】【转】:Spring Boot中使用@Scheduled创建定时任务
- Springboot定时任务
- Spring_boot定时任务
- springboot定时任务
- Spring-Boot中如何使用多线程处理任务
- spring定时任务.线程池,自定义多线程配置
- Spring Boot 定时任务之Quartz
- SpringBoot创建定时任务
- springboot 定时任务(线程配置,并行【同步】、异步等)
- 详解spring多线程与定时任务
- SpringBoot定时任务(Spring Schedule )实现方法
- Spring-Boot定时任务 Could not find default TaskScheduler bean异常处理
- SpringBoot 定时任务踩坑记录
- springboot整合quartz实现定时任务示例
- 从0开始学习SpringCould(3)--SpringBoot 定时任务
- (14)Spring Boot定时任务的使用【从零开始学Spring Boot】
- spring boot调用spring自带定时任务