隨着應用系統功能的不斷新增,而某些功能的實現對實時性要求並非那麼高,可是邏輯卻很複雜、執行比較耗時,好比涉及外部系統調用、多數據源等等;此時,咱們就但願可讓這些複雜的業務邏輯放在後臺執行,而前臺與用戶的交互能夠不用等待,從而提升用戶體驗。java
config-services.xmlgit
<?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" /> …….
異步代碼:spring
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 } } }
調用代碼:spring-mvc
@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); } }
<?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 />