使用Spring @Async异步执行方法的笔记
来源:互联网 发布:三维图纸软件 编辑:程序博客网 时间:2024/06/05 21:14
测试代码:
- <p>@RunWith(SpringJUnit4ClassRunner.class)</p>@ContextConfiguration(locations = { "/spring/*.xml" })
- public class JobUtilsTest{
- @Autowired
- private DaoService service;
- @Test
- public void testAsync() throws Exception {
- System.out.println("start" );
- service.update(); // ★ 假设这个方法会比较耗时,需要异步执行
- System.out.println("end");
- Thread.sleep(3000); // 因为junit结束会结束jvm,所以让它等会异步线程
- }
- }
DaoService代码:
- @Service
- public class DaoService {
- @Async
- public void update() {
- try {
- Thread.sleep(2000);
- // do something
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("operation complete.");
- }
- }
applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
- <context:component-scan base-package="com.chinacache" />
- <task:annotation-driven />
- </beans>
输出结果:
start
end
operation complete.
end
operation complete.
可以看出,输出不是顺序执行,说明异步调用成功了。
- 使用Spring @Async异步执行方法的笔记
- 使用Spring @Async异步执行方法的笔记
- 使用spring的@Async异步执行方法
- 使用spring的@Async异步执行方法
- 使用spring的@Async异步执行方法
- 使用spring的@Async异步执行方法
- 使用spring的@Async异步执行方法
- Spring 异步方法(@Async)的使用
- 使用spring的@Async异步执行方法以及不执行问题
- spring @Async 注解异步执行
- Spring Boot Async异步执行
- c# async、await的使用,异步方法
- Spring异步方法注解@Async
- Spring异步方法注解 @Async
- Spring boot 使用异步@Async
- Spring异步执行(@Async)2点注意事项
- Spring中@Async注解实现方法的异步调用
- Spring中@Async注解实现“方法”的异步调用
- c#利用反射,实现将model类变成字符串、再还原成mode对象的功能
- JFreeChart示例
- 【iOS游戏开发】GameCenter之Achievement成就最佳实践
- Ubuntu环境下切换root用户的方法
- fake sendmail for windows(wamp)
- 使用Spring @Async异步执行方法的笔记
- 各种AJAX方法的使用比较(1)
- C# 对 结构体 或 类对象 的arraylist 排序 sort()
- ruby 通过hive连接Hadoop
- 编译器错误消息: CS0016
- 经典ASP.NET MVC3.0入门详解
- index.jsp
- 一个很好的DB2存储过程例子
- mysql 详解YEAR() MONTH() DAYOFMONTH()日期函数