SpringMVC之-Callable和DeferredResult异步请求

Callable和DeferredResult均是SpringMVC中处理异步请求的方法;

Callable异步请求步骤:

  * 1、控制器返回Callable
     * 2、Spring异步处理,将Callable 提交到 TaskExecutor 使用一个隔离的线程进行执行
     * 3、DispatcherServlet和所有的Filter退出web容器的线程,但是response 保持打开状态;
     * 4、Callable返回结果,SpringMVC将请求重新派发给容器,恢复之前的处理;
     * 5、根据Callable返回的结果。SpringMVC继续进行视图渲染流程等(从收请求-视图渲染)。
     * 
     * preHandle.../springmvc-annotation/async01
        主线程开始...Thread[http-bio-8081-exec-3,5,main]==>1513932494700
        主线程结束...Thread[http-bio-8081-exec-3,5,main]==>1513932494700
        =========DispatcherServlet及所有的Filter退出线程============================
        
        ================等待Callable执行==========
        副线程开始...Thread[MvcAsync1,5,main]==>1513932494707
        副线程开始...Thread[MvcAsync1,5,main]==>1513932496708
        ================Callable执行完成==========
        
        ================再次收到之前重发过来的请求========
        preHandle.../springmvc-annotation/async01
        postHandle...(Callable的之前的返回值就是目标方法的返回值)
        afterCompletion...
        
        异步的拦截器:
            1)、原生API的AsyncListener
            2)、SpringMVC:实现AsyncHandlerInterceptor;
     * @return


一、返回Callable

新建AsyncController .java

package com.atguigu.controller;

import java.util.UUID;
import java.util.concurrent.Callable;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.DeferredResult;

import com.atguigu.service.DeferredResultQueue;


@Controller
public class AsyncController {
	
	@ResponseBody
	@RequestMapping("/async01")
	public Callable async01(){
		System.out.println("主线程开始..."+Thread.currentThread()+"==>"+System.currentTimeMillis());
		
		Callable callable = new Callable() {
			@Override
			public String call() throws Exception {
				System.out.println("副线程开始..."+Thread.currentThread()+"==>"+System.currentTimeMillis());
				Thread.sleep(2000);
				System.out.println("副线程结束..."+Thread.currentThread()+"==>"+System.currentTimeMillis());
				return "Callable async01()";
			}
		};
		
		System.out.println("主线程结束..."+Thread.currentThread()+"==>"+System.currentTimeMillis());
		return callable;
	}

}

运行tomcat结果:

SpringMVC之-Callable和DeferredResult异步请求_第1张图片

二、返回 DeferredResult

1、新建一个队列

package com.atguigu.service;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.springframework.web.context.request.async.DeferredResult;

public class DeferredResultQueue {
	
	private static Queue> queue = new ConcurrentLinkedQueue>();
	
	public static void save(DeferredResult deferredResult){
		queue.add(deferredResult);
	}
	
	public static DeferredResult get( ){
		return queue.poll();
	}

}
 
  

2、新建AsyncController.java 

package com.atguigu.controller;

import java.util.UUID;
import java.util.concurrent.Callable;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.DeferredResult;

import com.atguigu.service.DeferredResultQueue;


@Controller
public class AsyncController {
	
	
	@ResponseBody
	@RequestMapping("/createOrder")
	public DeferredResult createOrder(){
		DeferredResult deferredResult = new DeferredResult<>((long)3000, "create fail...");
			
		DeferredResultQueue.save(deferredResult);
		
		return deferredResult;
	}
	
	
	@ResponseBody
	@RequestMapping("/create")
	public String create(){
		//创建订单
		String order = UUID.randomUUID().toString();
		DeferredResult deferredResult = DeferredResultQueue.get();
		deferredResult.setResult(order);
		return "success===>"+order;
	}

}
 
  

运行tomcat结果:

 查看订单号:

SpringMVC之-Callable和DeferredResult异步请求_第2张图片

结果说明:说明两个方法返回的订单号相同,说明如果有另一个线程给DeferredResult赋值后,DeferredResult在感知到自己的对象被赋值后就返回页面成功;

 

 

======以下于你或许是个好消息======

 

好消息就是:欢迎访问下面的博客网站哈哈哈......

 

网站名称:Java学习笔记网 (点击进入)

url:https://www.javaxxbj.com/ (点击进入)

网站特点:

  1. java主要网站的导航目录
  2. 你可以记录自己的博客,并可以控制显示和隐藏,可利于管理啦!!!
  3. 可以添加收藏各个网站的链接!!!
  4. 甚至也可以文章收藏,点赞,关注,查看我的消息等功能哦!!1

看一小点点的截图:

或可一试哦!

你可能感兴趣的:(springmvc,异步请求)