6.1.13 异步请求处理 - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
6.1.13 异步请求处理
Grails support asynchronous request processing as provided by the Servlet 3.0 specification. To enable the async features you need to set your servlet target version to 3.0 in BuildConfig.groovy:With that done ensure you do a clean re-compile as some async features are enabled at compile time.
Grails支持Servlet 3.0规范的异步请求处理。要使异步功能生效,你需要在BuildConfig.groovy中设置servlet的版本为3.0:grails.servlet.version = "3.0"With a Servlet target version of 3.0 you can only deploy on Servlet 3.0 containers such as Tomcat 7 and above.
grails.servlet.version = "3.0"使用Servlet 3.0版本以后,你只能将应用部署于支持Servlet 3.0的容器中,比如Tomcat 7及其以上版本。
Asynchronous Rendering
You can render content (templates, binary data etc.) in an asynchronous manner by calling thestartAsync method which returns an instance of the Servlet 3.0 AsyncContext. Once you have a reference to the AsyncContext you can use Grails' regular render method to render content:def index() {
def ctx = startAsync()
ctx.start {
new Book(title:"The Stand").save()
render template:"books", model:[books:Book.list()]
ctx.complete()
}
}complete() method to terminate the connection.
异步渲染
你可以通过调用startAsync方法的方式进行异步的内容渲染(比如模板、二进制数据等),此方法的返回值是Servlet 3.0 AsyncContext的一个实例。一旦获取到了AsyncContext的引用,你就可以使用Grails的render方法来渲染内容了:def index() {
def ctx = startAsync()
ctx.start {
new Book(title:"The Stand").save()
render template:"books", model:[books:Book.list()]
ctx.complete()
}
}complete()方法来中止此连接。Resuming an Async Request
You resume processing of an async request (for example to delegate to view rendering) by using thedispatch method of the AsyncContext class:def index() {
def ctx = startAsync()
ctx.start {
// do working
…
// render view
ctx.dispatch()
}
}恢复一个异步请求
你可以通过AsyncContext类的dispatch方法来恢复一个异步请求(比如将其代理到一个视图):def index() {
def ctx = startAsync()
ctx.start {
// do working
…
// render view
ctx.dispatch()
}
}
