`

如何测试@Async异步任务

 
阅读更多
spring3支持@Async注解的异步任务,之前大家都是通过使用如线程池来完成,spring3也是使用这种方式,但更简单。

 

其具体实现在:org.springframework.aop.interceptor.AsyncExecutionInterceptor,是一个方法拦截器,其invoke方法的部分代码如下: 

Java代码 复制代码 收藏代码
  1. Future<?> result = determineAsyncExecutor(specificMethod).submit(   
  2.                 new Callable<Object>() {   
  3.                     public Object call() throws Exception {   
  4.                         try {   
  5.                                
  6.                             Object result = invocation.proceed();   
  7.                             if (result instanceof Future) {   
  8.                                 return ((Future<?>) result).get();   
  9.                             }   
  10.                         }   
  11.                         catch (Throwable ex) {   
  12.                             ReflectionUtils.rethrowException(ex);   
  13.                         }   
  14.                         return null;   
  15.                     }   
  16.                 });  
Future<?> result = determineAsyncExecutor(specificMethod).submit(
                new Callable<Object>() {
                    public Object call() throws Exception {
                        try {
                            
                            Object result = invocation.proceed();
                            if (result instanceof Future) {
                                return ((Future<?>) result).get();
                            }
                        }
                        catch (Throwable ex) {
                            ReflectionUtils.rethrowException(ex);
                        }
                        return null;
                    }
                });

即把当前任务的调用提交给线程池,很简单。

 

 

1、测试无事务的异步任务

这个相对来说比较简单:

1.1、设置任务的返回值为Future: 

Java代码 复制代码 收藏代码
  1. public Future sendSystemMessage(Long[] receiverIds, Message message);  
public Future sendSystemMessage(Long[] receiverIds, Message message);

 

1.2、调用future.get();等待任务结束。 

Java代码 复制代码 收藏代码
  1. Future future = messageApi.sendSystemMessage(userIds, message);   
  2. future.get();  
        Future future = messageApi.sendSystemMessage(userIds, message);
        future.get();

 

 这个很简单。

 

2、测试带事务的异步任务

因为是带事务的,所以异步任务肯定要启动一个线程来执行任务,所以无法在主线程回滚,造成数据会commit到数据库,这在集成测试时肯定是不行的;解决方案是移除异步任务:

 

2.1、使用spring profile,在测试环境下不执行<task:annotation-driven>即可。

 

2.2、使用我提供的工具类,在测试时移除异步支持即可: 

Java代码 复制代码 收藏代码
  1. //移除异步支持   
  2. if(AopProxyUtils.isAsync(messageApi)) {   
  3.     AopProxyUtils.removeAsync(messageApi);   
  4. }  
        //移除异步支持
        if(AopProxyUtils.isAsync(messageApi)) {
            AopProxyUtils.removeAsync(messageApi);
        }

 

测试类可以参考MessageApiServiceIT.java 

工具类下载 AopProxyUtils.java

 

3、包级别测试 

Java代码 复制代码 收藏代码
  1. @Async  
  2. public void sendSystemMessage() {   
  3.       sendSystemMessageInner();   
  4. }   
  5.   
  6. void sendSystemMessageInner() {   
  7.       //测试时测试这个方法即可   
  8. }  
@Async
public void sendSystemMessage() {
      sendSystemMessageInner();
}

void sendSystemMessageInner() {
      //测试时测试这个方法即可
}

 这样测试时测试这个包级别的sendSystemMessageInner方法即可

 

 

 其实更好的做法是spring内部提供支持,支持这样异步调用的测试。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics