Spring 비동기 호출 (spring 4.0 설정 추가)

응용 시스템 기능 이 계속 증가 함 에 따라 일부 기능 의 실현 은 실시 간 요구 가 그리 높 지 않 지만 논 리 는 복잡 하고 실행 에 시간 이 많이 걸린다. 예 를 들 어 외부 시스템 호출, 다 중 데이터 소스 등 이다.이때 우 리 는 이런 복잡 한 업무 논 리 를 백 스테이지 에 놓 고 집행 할 수 있 기 를 바 랍 니 다. 그리고 프론트 데스크 와 사용자 의 상호작용 은 기다 리 지 않 고 사용자 체험 을 향상 시 킬 수 있 습 니 다.
config-services.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: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" />
    …….

비동기 코드:
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
        }
                 
    }
}

호출 코드:
@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 />

좋은 웹페이지 즐겨찾기