Spring Servlet3 扩展模块笔记
Servlet 3.0AsyncWebRequest.java- 异步请求接口,继承NativeWebRequest
接口
/** * {@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- 不支持异步处理模式的web请求
DeferredResultProcessingInterceptor.java- DeferredResult处理过程拦截器
- 在start async前,超时后/异步处理完成后/网络超时后触发拦截
DeferredResultProcessingInterceptorAdapter.java- 抽象类实现DeferredResultProcessingInterceptor,做空实现
DeferredResultInterceptorChain.java- 调用DeferredResultProcessingInterceptor的辅助类
DeferredResult.java- 递延结果,在两个线程中传递的对象结果
- 实现Comparable接口以保证加入PriorityQueue队列的正确顺序
CallableProcessingInterceptor.java- Callable拦截器
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的注入等
- 异步处理的入口类
- 异步请求接口,继承NativeWebRequest
接口
/** * {@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
- 不支持异步处理模式的web请求
DeferredResultProcessingInterceptor.java
- DeferredResult处理过程拦截器
- 在start async前,超时后/异步处理完成后/网络超时后触发拦截
DeferredResultProcessingInterceptorAdapter.java
- 抽象类实现DeferredResultProcessingInterceptor,做空实现
DeferredResultInterceptorChain.java
- 调用DeferredResultProcessingInterceptor的辅助类
DeferredResult.java
- 递延结果,在两个线程中传递的对象结果
- 实现Comparable接口以保证加入PriorityQueue队列的正确顺序
CallableProcessingInterceptor.java
- Callable拦截器
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的注入等
- 异步处理的入口类
- 调用CallableProcessingInterceptor的辅助类
- 抽象类实现CallableProcessingInterceptor接口,空实现
- Callable拦截器
- 调用DeferredResultProcessingInterceptor的辅助类
- 抽象类实现DeferredResultProcessingInterceptor,做空实现
- 不支持异步处理模式的web请求