springboot使用定时任务、异步
2017-06-12 22:05
771 查看
1、定时任务:纯注解方式
@Configuration @EnableScheduling @Component public class TaskConfig { // 定时任务:每天凌晨3点跑定时 @Scheduled(cron = "0 0 3 * * ?") public void myTask() { System.out.println("定时任务启动。。。。。。"); } }
2、异步
1)注解方式开启异步并设置异步线程池参数@Configuration @EnableAsync public class AsyncConfig { /* 此处成员变量应该使用@Value从配置中读取 */ /** Set the ThreadPoolExecutor's core pool size. */ private int corePoolSize = 10; /** Set the ThreadPoolExecutor's maximum pool size. */ private int maxPoolSize = 200; /** Set the capacity for the ThreadPoolExecutor's BlockingQueue. */ private int queueCapacity = 10; @Bean public Executor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(corePoolSize); executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); executor.initialize(); return executor; } }
2)测试异步
@RequestMapping(value = "/api/async") @RestController public class AsyncController { @Autowired private AsyncTask asyncTask; @GetMapping(value = "/test") public String testAsync() { /* 123-->异步,132-->同步 */ System.out.println("1"); asyncTask.async(); System.out.println("2"); return "test"; } }
@Component public class AsyncTask { @Async public void async() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("3"); } }
相关文章推荐
- springboot中使用定时任务,异步调用,自定义配置参数(八)
- Spring Boot:在Spring Boot中使用定时任务
- Spring Boot 定时任务的使用
- spring boot中使用@Async实现异步调用任务
- (14)Spring Boot定时任务的使用【从零开始学Spring Boot】
- spring boot使用自定义配置的线程池执行Async异步任务
- Spring Boot:在Spring Boot中使用定时任务
- 详解Spring Boot中使用@Scheduled创建定时任务
- Spring Boot定时任务的使用
- spring boot-分布式定时任务中,异步执行时JMS无法重新投递消息
- spring boot-使用redis的Keyspace Notifications实现定时任务队列
- (14)Spring Boot定时任务的使用【从零开始学Spring Boot】
- Spring Boot中使用@Scheduled创建定时任务
- Spring Boot 定时任务的使用
- Spring Boot定时任务的使用实例代码
- Spring Boot使用方法小札(2):执行定时任务
- Springboot 之 使用Scheduled做定时任务
- 在Spring Boot中使用@Scheduled实现定时任务
- Spring Boot定时任务的使用方法
- 14. Spring Boot定时任务的使用【从零开始学Spring Boot】