使⽤SpringMVC@Async异步执⾏⽅法的笔记(转载)
代码:
@RunWith(SpringJUnit4ClassRunner.class)</p>@ContextConfiguration(locations = { "/spring/*.xml" })
public class JobUtilsTest{
@Autowired
private DaoService service;
@Test
public void testAsync() throws Exception {
spring framework表达式assignSystem.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.");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance" xmlns:p="/schema/p"
xmlns:context="/schema/context"
xmlns:mvc="/schema/mvc" xmlns:task="/schema/task"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans-3.0.xsd
/schema/context
/schema/context/spring-context-3.0.xsd
/schema/task /schema/task/spring-task-3.0.xsd">
<context:component-scan base-package="com.chinacache"/>
<!-- <task:annotation-driven />  -->
<task:annotation-driven executor="myexecutor"/>
<task:executor id="myexecutor" pool-size="50"/>
</beans>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。