springboot使用定时任务、异步
来源:互联网 发布:金十数据如何看原油 编辑:程序博客网 时间:2024/06/05 10:21
1、定时任务:纯注解方式
@Configuration@EnableScheduling@Componentpublic class TaskConfig { // 定时任务:每天凌晨3点跑定时 @Scheduled(cron = "0 0 3 * * ?") public void myTask() { System.out.println("定时任务启动。。。。。。"); }}
2、异步
1)注解方式开启异步并设置异步线程池参数
@Configuration@EnableAsyncpublic 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")@RestControllerpublic 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"; }}
@Componentpublic class AsyncTask { @Async public void async() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("3"); }}
阅读全文
0 0
- springboot使用定时任务、异步
- springboot定时任务使用
- springboot定时任务的使用
- SpringBoot 定时任务的使用
- SpringBoot定时任务的使用
- springboot中使用定时任务,异步调用,自定义配置参数(八)
- Springboot 之 使用Scheduled做定时任务
- eclipse 使用maven构建springboot+定时任务
- Springboot使用定时任务,Spring Schedule
- SpringBoot 使用Scheduled 定时任务入门
- Springboot使用定时任务,Spring Schedule
- Springboot之使用Scheduled做定时任务
- SpringBoot定时任务说明
- springboot定时任务
- springboot(九):定时任务
- springboot(九):定时任务
- springboot 定时任务
- SpringBoot-@Schedule定时任务
- uva1636概率
- 强分类器-----提升树的原理和推导
- 主题三 编译过程介绍---- 15.编译过程简介
- Linux如何配置ftp服务器、给ftp服务器添加访问用户
- C++学习:你该了解一点C++的设计模式之单例模式
- springboot使用定时任务、异步
- 手势(OC)
- 如何打造自己的pe呢?微软官方WinPE的制作流程
- 运用XML建立零件BOM
- Mybatis的sql封装
- 牛牛的数值变换
- 配置项---java环境变量的配置
- 一周乱弹: jquery validate 手动触发方式
- springboot使用AOP