スプリングMVC3.2以降では、リクエストの非同期処理が追加され、サーブレット3に基づいてカプセル化されています。
1。Web.xmlを変更します
<?xml version = "1.0" encoding = "utf-8"?> <web-appバージョン= "3.0" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instcance" 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。サーブレットまたはフィルター設定の非同期サポートを有効にする: <async-supported> true </async-supported> Webアプリケーションのweb.xmlを変更する
<! - スプリングMVC - > <Servlet> <Servlet-Name> SpringMVC </servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <init-name> <param-name> cont extconfiglocation </param-name> <param-value> ... </param-value> </init-param> <load-on-startup> 1 </load-on-startup> <async-supported> true </async-supported> </servet>
2.コントローラークラスをサポートしてください
2.1。 java.util.concurrent.callableを返して、非同期処理を完了します
パッケージorg.springframework.samples.mvc.async; java.util.concurrent.callableをインポートします。 org.springframework.stereotype.controllerをインポートします。 Import org.springframework.ui.model; org.springframework.web.bind.annotation.exceptionhandlerをインポートします。 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 callide <string>(){@override public string call()throws {swreed.sleep); 「呼び出し可能な結果」を返します。 }}}; } @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"); 「Views/HTML」を返します。 }}; } @RequestMapping( "/exception")public @responsebody callable <string> callablewithexception(final @RequestValue = false、defaultValue = "true")boolean handled){return new Callride public String Call()Throws Exception {Stread.Sleep); if(handled){//以下のHandleExceptionメソッドを参照してください新しいIllegalStateException( "Callable Error"); } else {throw new IllegalargumentException( "callable error"); }}}}}; } @RequestMapping( "/custom-timeout wandling")public @responsebody webasynctask <string> callablewithcustomtimeouthandling(){callable <string> callable = new callable <string>(){@override public string call()throws {thread.sleep(2000); 「呼び出し可能な結果」を返します。 }};新しいwebasynctask <string>(1000、callable)を返します。 } @exceptionhandler @responsebody public string handleexception(illegalstateexception ex){return "handled exception:" + ex.getMessage(); }}2.2。 org.springframework.web.context.request.async.deferredResultを返します。
@RequestMapping( "/quotes")@ResponseBodyPublic DeferredResult <String> Quotes(){deferredResult <string> deferredResult = new DeferredResult <String>(); //キューまたはマップにdeferredResultを追加... deferredResult;} //他のスレッドでdeferredResult.setResult(data); //キューまたはマップからdeferredResultを削除する3。スプリング構成ファイルの変更
スプリングMVCのDTDの宣言は、3.2以上でなければなりません
<MVC:annotation-driven> <! - デフォルトのタイムアウトを使用できます - > <MVC:async-support default-timeout = "3000"/> </mvc:annotation-driven>
実際の使用例:
function deferred(){$ .get( 'quotes.htm'、function(data){console.log(data); deferred(); //要求が完了するたびに、データを取得するために定期的にリフレッシュすることを避けるために別の要求を送信します});}これを行うことの利点により、Webサーバー接続プールの長期使用が回避され、パフォーマンスの問題が発生します。呼び出し後、それを処理するために非Webサービススレッドが生成され、Webサーバーのスループットが増加します~~
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。