@EnableAsync和@Async开始异步任务支持
2017-07-28 17:37
357 查看
Spring通过任务执行器(TaskExecutor)来实现多线程和并发编程。使用ThreadPoolTaskExecutor可实现一个基于线程池的TaskExecutor.在开发中实现异步任务,我们可以在配置类中添加@EnableAsync开始对异步任务的支持,并在相应的方法中使用@Async注解来声明一个异步任务。
配置类
任务执行service类
测试类
测试结果如下:
执行结果可以看出,并没有按照顺序来执行。
配置类
package com.xingguo.logistics.controller; import java.util.concurrent.Executor; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @Configuration @ComponentScan({"com.xingguo.logistics.service.aspect","com.xingguo.logistics.service.event"}) //开始异步支持 @EnableAsync public class AopConfig implements AsyncConfigurer{ @Override public Executor getAsyncExecutor() { //线程池 ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(5); taskExecutor.setMaxPoolSize(10); taskExecutor.setQueueCapacity(25); taskExecutor.initialize(); return taskExecutor; } @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return null; } }
任务执行service类
package com.xingguo.logistics.service.aspect; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Service public class TestService2 { //声明异步任务 @Async public void executeAsyncTask(Integer i){ System.out.println("执行异步任务:"+i); } @Async public void executeAsyncTask2(Integer i){ System.out.println("执行异步任务2:"+i); } }
测试类
package com.xingguo.logistics.controller; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.xingguo.logistics.service.aspect.TestService2; public class TestController { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class); TestService2 testService2 = context.getBean(TestService2.class); for(int i = 0; i<10; i++){ testService2.executeAsyncTask(i); testService2.executeAsyncTask2(i); } context.close(); } }
测试结果如下:
执行结果可以看出,并没有按照顺序来执行。
相关文章推荐
- 译文:如何运用Spring框架的@Async实现异步任务
- SimpleAsyncTaskExecutor(源码)简单的异步任务支持限流,ListenableFutureTask支持注册事件->FutureTask的继承
- Windows Azure功能更新:网站服务支持过渡环境,支持异步任务,监控改进等
- 使用@Async和@EnableAsync实现主线程中异步执行其他方法
- 分享一个异步任务在遇到IO异常时支持递归回调的辅助方法
- java并发编程--Executor框架(一) 摘要: Eexecutor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程
- Spring异步任务处理,@Async的最基础的配置和使用
- 如何运用Spring框架的@Async实现异步任务
- 异步或多线程支持一个任务执行完成
- Spring中的@Async注解 异步任务处理
- spring boot中使用@Async实现异步调用任务
- 如何运用Spring框架的@Async实现异步任务
- python huey 轻量级异步任务队列简介
- 摘要异步任务AsyncTask初识
- iOS中检测是否支持多任务
- IOS学习之路七(使用 Operation 异步运行任务)
- Android和iOS开发中的异步处理(三)——多个异步任务协作
- 让UpdatePanel支持文件上传(1):开始
- 使用GCD处理非UI相关的异步任务 Object-C异步多线程加载网络图片