国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

詳解spring mvc對(duì)異步請(qǐng)求的處理

2019-11-26 13:20:27
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在spring mvc3.2及以上版本增加了對(duì)請(qǐng)求的異步處理,是在servlet3的基礎(chǔ)上進(jìn)行封裝的。

1、修改web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">...</web-app>

1.1、聲明version="3.0",聲明web-app_3_0.xsd

1.2、為servlet或者filter設(shè)置啟用異步支持: <async-supported>true</async-supported> ,修改WEB應(yīng)用的web.xml

<!-- spring mvc --><servlet><servlet-name>SpringMvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>...</param-value></init-param><load-on-startup>1</load-on-startup><async-supported>true</async-supported></servlet>

2、使controller類支持async

2.1、返回java.util.concurrent.Callable來(lái)完成異步處理

package org.springframework.samples.mvc.async; import java.util.concurrent.Callable; import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.context.request.async.WebAsyncTask; @Controller@RequestMapping("/async/callable")public class CallableController {  @RequestMapping("/response-body")  public @ResponseBody Callable<String> callable() {     return new Callable<String>() {      @Override      public String call() throws Exception {        Thread.sleep(2000);        return "Callable result";      }    };  }   @RequestMapping("/view")  public Callable<String> callableWithView(final Model model) {     return new Callable<String>() {      @Override      public String call() throws Exception {        Thread.sleep(2000);        model.addAttribute("foo", "bar");        model.addAttribute("fruit", "apple");        return "views/html";      }    };  }   @RequestMapping("/exception")  public @ResponseBody Callable<String> callableWithException(      final @RequestParam(required=false, defaultValue="true") boolean handled) {     return new Callable<String>() {      @Override      public String call() throws Exception {        Thread.sleep(2000);        if (handled) {          // see handleException method further below          throw new IllegalStateException("Callable error");        }        else {          throw new IllegalArgumentException("Callable error");        }      }    };  }   @RequestMapping("/custom-timeout-handling")  public @ResponseBody WebAsyncTask<String> callableWithCustomTimeoutHandling() {     Callable<String> callable = new Callable<String>() {      @Override      public String call() throws Exception {        Thread.sleep(2000);        return "Callable result";      }    };     return new WebAsyncTask<String>(1000, callable);  }   @ExceptionHandler  @ResponseBody  public String handleException(IllegalStateException ex) {    return "Handled exception: " + ex.getMessage();  } }

2.2、在異步處理完成時(shí)返回org.springframework.web.context.request.async.DeferredResult其他線程,例如一個(gè)JMS或一個(gè)AMQP消息,Redis通知等等:

@RequestMapping("/quotes")@ResponseBodypublic DeferredResult<String> quotes() { DeferredResult<String> deferredResult = new DeferredResult<String>(); // Add deferredResult to a Queue or a Map... return deferredResult;}  // In some other thread...deferredResult.setResult(data);// Remove deferredResult from the Queue or Map

3、spring配置文件的修改

spring mvc的dtd的聲明必須大于等于3.2

<mvc:annotation-driven><!-- 可不設(shè)置,使用默認(rèn)的超時(shí)時(shí)間 -->  <mvc:async-support default-timeout="3000"/></mvc:annotation-driven>

 實(shí)際使用示例:

function deferred(){  $.get('quotes.htm',function(data){    console.log(data);    deferred();//每次請(qǐng)求完成,再發(fā)一次請(qǐng)求,避免客戶端定時(shí)刷新來(lái)獲取數(shù)據(jù)  });}

這么做的好處避免web server的連接池被長(zhǎng)期占用而引起性能問(wèn)題,調(diào)用后生成一個(gè)非web的服務(wù)線程來(lái)處理,增加web服務(wù)器的吞吐量~~

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 泗阳县| 奉新县| 托克逊县| 通河县| 泰顺县| 华宁县| 龙山县| 阿克苏市| 安阳县| 钦州市| 江孜县| 永清县| 喀喇沁旗| 贺州市| 嫩江县| 天峻县| 清新县| 宿松县| 页游| 淮滨县| 桐乡市| 灌阳县| 汤阴县| 章丘市| 台东市| 彩票| 南开区| 门源| 威宁| 乐山市| 石渠县| 禄丰县| 泰宁县| 岱山县| 富锦市| 微山县| 内江市| 芦山县| 茂名市| 晴隆县| 福泉市|