随着应用系统功能的不断新增,而某些功能的实现对实时性要求并不是那么高,但是逻辑却很复杂、执行比较耗时,比如涉及外部系统调用、多数据源等等;此时,我们就希望可以让这些复杂的业务逻辑放在后台执行,而前台与用户的交互可以不用等待,从而提高用户体验。


config-services.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<? 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:mvc = "http://www.springframework.org/schema/mvc"
     xmlns:context = "http://www.springframework.org/schema/context"
     xmlns:mongo = "http://www.springframework.org/schema/data/mongo"
     xmlns:task = "http://www.springframework.org/schema/task"
     xmlns:aop = "http://www.springframework.org/schema/aop"  xmlns:tx = "http://www.springframework.org/schema/tx"
     xsi:schemaLocation="
         http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc-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/tx
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
         http://www.springframework.org/schema/data/mongo
         http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
         http://www.springframework.org/schema/task
     http://www.springframework.org/schema/task/spring-task-3.0.xsd"  default-autowire = "byName" >
     < mvc:annotation-driven  />
     < task:annotation-driven />
     < context:annotation-config  />
     < context:component-scan  base-package = "com.digitalchina.lbs"   name-generator = "com.digitalchina.frame.spring.support.FullQualifieldBeanNameGenerator"  />
     …….

异步代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package  com.digitalchina.lbs.serve.manager;
import  java.util.Date;
import  org.springframework.scheduling.annotation.Async;
import  org.springframework.stereotype.Component;
@Component
public  class  AsynManager {
              
     @Async
     public  void  sleep(){
         Date date= new  Date();
         System.out.println( "====================================执行时间:" +date);
         try  {
             Thread.sleep( 10000 );
             System.out.println( "====================================执行时间:" + new  Date());
                      
         catch  (Exception e) {
             // TODO: handle exception
         }
                  
     }
}


调用代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Service
public  class  LBSService {
     //测试
     @Autowired
     private  AsynManager asynManager;
         
     public  void  asyn(CspServiceContext serviceContext){
         System.out.println( "+++++++++++asynasynasynasyn+++++++++++++" + new  Date());
         asynManager.sleep();
         System.out.println( "++++++++++++++++++++++++" + new  Date());
         // 向框架返回数据
         Response re =  new  Response( new  Date());
         serviceContext.setResponseData(re);
         serviceContext.setResult(Result.SUCCESS_RESULT);
     }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<? 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:tx = "http://www.springframework.org/schema/tx"
     xmlns:context = "http://www.springframework.org/schema/context"
     xmlns:mongo = "http://www.springframework.org/schema/data/mongo"
     xmlns:aop = "http://www.springframework.org/schema/aop" 
     xmlns:p = "http://www.springframework.org/schema/p"
     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-4.0.xsd    
         http://www.springframework.org/schema/tx    
         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd   
         http://www.springframework.org/schema/context   
         http://www.springframework.org/schema/context/spring-context-4.0.xsd
         http://www.springframework.org/schema/data/mongo
         http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd   
         http://www.springframework.org/schema/mvc   
         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
         http://www.springframework.org/schema/task
         http://www.springframework.org/schema/task/spring-task-4.0.xsd">
     <!-- 注解扫描包 -->
     < context:component-scan  base-package = "com.baidu"  />
     <!-- 开启注解 -->
     < mvc:annotation-driven  />
     < context:annotation-config  />
     < task:annotation-driven  />