首页 > 其他 > 详细

Spring异步调用

时间:2014-03-07 23:20:17      阅读:667      评论:0      收藏: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);
    }
}















Spring异步调用,布布扣,bubuko.com

Spring异步调用

原文:http://qiangmzsx.blog.51cto.com/2052549/1370016

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!