@Async异步注解

前端:

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
	
    RequestParam测试

	
    
	




请稍候,服务器正在处理中...

当前处理进度:准备中

    后台控制层:

	@RequestMapping(value = "/async/asyncTest")
    public String asyncTest(HttpServletRequest req,
                            HttpServletResponse resp,	 HttpSession session) throws Exception {
        asyncService.asyncMethod(	 session);
        return "async";
    }
    
    @RequestMapping(value = "/async/status")
    public void showAsyncStatus(HttpServletRequest req,
                                  HttpServletResponse resp,	 HttpSession session) throws Exception {
        String status = asyncService.getProcess(session);
        resp.getWriter().print(status);
        //return null;
    }

    @RequestMapping(value = "/async/clear")
    public void clearAsyncStatus(HttpServletRequest req,
                                   HttpServletResponse resp, HttpSession session) throws Exception {
        asyncService.clearCache(session);
        //resp.getWriter().print("ok");
        //System.out.println(session.getAttribute("cacheKey"));
        //return null;
    }
后台service:

package com.htxx.service.business;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncServiceImpl implements AsyncService{

    
    @Override
    @Async
    public void asyncMethod(	 HttpSession session) throws Exception {
        //模拟总有20个步骤,每个步骤耗时2秒
        int maxStep = 10;
        for (int i = 0; i < maxStep; i++) {
        	System.out.println(i);
            Thread.sleep(1000);
            //将执行进度放入缓存
            session.setAttribute("cacheKey", (i + 1) + "/" + maxStep);
        }
    }

    @Override
    public String getProcess(	 HttpSession session) throws Exception {
        return (String) session.getAttribute("cacheKey");
    }

    @Override
    public void clearCache(	 HttpSession session) throws Exception {
        //完成后,清空缓存
        session.setAttribute("cacheKey","0/10");
    	//session.removeAttribute("cacheKey");
    }

}
  浏览器:http://localhost:8090/MyLab/rpc/async/asyncTest

  在asyncTest方法里会调用service中一个for循环,这个for循环大约会执行10秒,然后再跳转到"async.jsp"

加上@async注解后,asyncTest方法不必等到for循环完全结束再跳转到"async.jsp",在jsp页面内有setInterval循环查询后台session内的值,

将结果显示在页面上

效果显示:

@Async异步注解_第1张图片@Async异步注解_第2张图片@Async异步注解_第3张图片

--------------------------------------------------------遇到的问题------------------------------------------------------------------------------------------------

加了注解没反应,等到for循环结束后才进入页面:

解决办法:

在springmvc配置文件内加:

    
   
	
-----------------------------------------------------参考-------------------------------------------------------------------------------------------------------

http://www.cnblogs.com/yjmyzz/p/how-to-use-Async-annotation-in-spring-mvc.html


你可能感兴趣的:(Java,Web,高级编程,SpringMVC)