Servlet 3.0
AsyncWebRequest.java
- 異步請求接口,繼承NativeWebRequest
接口
/**
* Set the time required for concurrent handling to complete.
* This property should not be set when concurrent handling is in progress,
* i.e. when {@link #isAsyncStarted()} is {@code true}.
* @param timeout amount of time in milliseconds; {@code null} means no
* timeout, i.e. rely on the default timeout of the container.
*/
//設置超時時間
void setTimeout(Long timeout);
/**
* Add a handler to invoke when concurrent handling has timed out.
*/
//增加超時處理類
void addTimeoutHandler(Runnable runnable);
/**
* Add a handle to invoke when request processing completes.
*/
//增加請求處理完成處理類
void addCompletionHandler(Runnable runnable);
/**
* Mark the start of asynchronous request processing so that when the main
* processing thread exits, the response remains open for further processing
* in another thread.
* @throws IllegalStateException if async processing has completed or is not supported
*/
//標示異步請求開始,返回異步Context
void startAsync();
/**
* Whether the request is in async mode following a call to {@link #startAsync()}.
* Returns "false" if asynchronous processing never started, has completed,
* or the request was dispatched for further processing.
*/
//判斷是否是當前線程是否啓動異步模式,false表示沒有啓動異步模式,或者已經完成,否則請求被分發到其他線程處理
boolean isAsyncStarted();
/**
* Dispatch the request to the container in order to resume processing after
* concurrent execution in an application thread.
*/
//分發請求到容器,喚醒其他線程處理
void dispatch();
/**
* Whether asynchronous processing has completed.
*/
//判斷異步處理是否完成
boolean isAsyncComplete();
StandardServletAsyncWebRequest.java
- 繼承ServletWebRequest,實現AsyncWebRequest, AsyncListener,一個標準異步web請求的實現類。
代碼
/**
* {@inheritDoc}
* <p>In Servlet 3 async processing, the timeout period begins after the
* container processing thread has exited.
*/
public void setTimeout(Long timeout) {
Assert.state(!isAsyncStarted(), "Cannot change the timeout with concurrent handling in progress");//斷言狀態沒有啓動異步
this.timeout = timeout;
}
public boolean isAsyncStarted() {
return ((this.asyncContext != null) && getRequest().isAsyncStarted());//異步狀態不爲空,請求是否啓動異步處理模式,如果請求被AsyncContext.dispatch()到容器,或 AsynContext.complete ,則返回false.
}
/**
* Whether async request processing has completed.
* <p>It is important to avoid use of request and response objects after async
* processing has completed. Servlet containers often re-use them.
*/
public boolean isAsyncComplete() {
return this.asyncCompleted.get();//請求的異步處理是否完成
}
public void startAsync() {
Assert.state(getRequest().isAsyncSupported(),
"Async support must be enabled on a servlet and for all filters involved " +
"in async request processing. This is done in Java code using the Servlet API " +
"or by adding \"<async-supported>true</async-supported>\" to servlet and " +
"filter declarations in
web.xml.");//判斷請求是否支持異步處理
Assert.state(!isAsyncComplete(), "Async processing has already completed");
if (isAsyncStarted()) {//判斷狀態是否已經啓動異步處理模式
return;
}
this.asyncContext = getRequest().startAsync(getRequest(), getResponse());
this.asyncContext.addListener(this);
if (this.timeout != null) {
this.asyncContext.setTimeout(this.timeout);
}
}
public void dispatch() {
Assert.notNull(this.asyncContext, "Cannot dispatch without an AsyncContext");
this.asyncContext.dispatch();
}
// ---------------------------------------------------------------------
// Implementation of AsyncListener methods
// ---------------------------------------------------------------------
public void onStartAsync(AsyncEvent event) throws IOException {
}
public void onError(AsyncEvent event) throws IOException {
}
public void onTimeout(AsyncEvent event) throws IOException {
for (Runnable handler : this.timeoutHandlers) {
handler.run();
}
}
public void onComplete(AsyncEvent event) throws IOException {
for (Runnable handler : this.completionHandlers) {
handler.run();
}
this.asyncContext = null;
this.asyncCompleted.set(true);//設置異步處理已經完成
}
NoSupportAsyncWebRequest.java
DeferredResultProcessingInterceptor.java
- DeferredResult處理過程攔截器
- 在start async前,超時後/異步處理完成後/網絡超時後觸發攔截
DeferredResultProcessingInterceptorAdapter.java
- 抽象類實現DeferredResultProcessingInterceptor,做空實現
DeferredResultInterceptorChain.java
- 調用DeferredResultProcessingInterceptor的輔助類
DeferredResult.java
- 遞延結果,在兩個線程中傳遞的對象結果
- 實現Comparable接口以保證加入PriorityQueue隊列的正確順序
CallableProcessingInterceptor.java
CallableProcessingInterceptorAdapter.java
- 抽象類實現CallableProcessingInterceptor接口,空實現
CallableInterceptorChain.java
- 調用CallableProcessingInterceptor的輔助類
TimeoutCallableProcessingInterceptor.java
- 繼承CallableProcessingInterceptorAdapter
- 實現超時處理方法
TimeoutDeferredResultProcessingInterceptor.java
- 繼承DeferredResultProcessingInterceptorAdapter
- 實現超時處理方法
WebAsyncTask.java
- web異步任務
- 包含一個Callable類,一個超時時間,一個任務執行着或名字
WebAsyncUtils.java
- 實現getAsyncManager
- 實現createAsyncWebRequest
WebAsyncManager.java
- 對Callables和DeferredResults啓動的管理,包括攔截器的注入,Excutor的注入等
- 異步處理的入口類