JavaScud | Forum | JIRA | Blog |
  Dashboard > WebWork2文档中文化计划 > ... > Interceptors > Execute and Wait Interceptor
  WebWork2文档中文化计划 Log In View a printable version of the current page.  
  Execute and Wait Interceptor
Added by 黑灵, last edited by scud on May 19, 2006  (view change)
Labels: 
(None)

ExecuteAndWaitInterceptor拦截器能够让一个执行时间较长的Action在后台执行,并向用户显示进度信息。当一个Action的执行时间会超过5或10分钟时,它可以防止HTTP请求超时。

这个拦截器的使用方法很直接。假如你已经引入了webwork-default.xml,这个拦截器就已经配置好了,只是没有出现在默认的拦截器栈中。这个拦截器的特性要求它必须被放在栈的最后

这个拦截器是基于每个Session工作,也就是说同一个Action不能在同一个的Sessioin中运行一次以上。初始请求和后续请求(在Action执行结束以前)将返回wait 结果。wait结果负责让后续的请求返回到这个Action,并显示更新的进度信息

如果没有找到"wait"结果,WebWork会自动生成一个wait结果.这个结果是用FreeMarker做的,所以需要Freemarker支持才能正常工作。如果你不想在程序中加入FreeMarker,那就必须自己实现一个wait结果。这一般来说是有必要的,因为默认的wait页面很简单。

无论何时只要返回了wait结果,正在后台运行的action将会被放到栈顶。这就允许你在等待页面中显示进度数据,例如计数器。通过让等待页面自动重新加载这个action(它会被这个拦截器短路),你可以实现一个自己更新的进度条.

这个拦截器也支持初始等待延迟.初始等待延迟就是我们可以让服务器在显示等待页面之前延迟一段时间(用毫秒表示).在这段时间里这个拦截器每隔100毫秒检查后台进程是不是过早的执行完了.如果出于某种原因这个任务并没有使用很长时间就完成了,等待画面就不会显示给用户.

这个对于执行时间较长的搜索action很有用.我们可以延迟2000毫秒,如果搜索时间较短就立刻返回结果,如果较长就返回等待画面.

注意:因为这个action将会以单独的线程执行,所以你不能用ActionContext,因为它是ThreadLocal.这也就是说如果你要访问session数据,你必须实现SessionAware结构而不是调用ActionContext.getSesion().

这个拦截器使用的线程会被命名为 actionNameBrackgroundProcess, 例如searchAction会以一个叫searchBackgroundProcess的线程执行。

参数

  • threadPriority(可选) 指定线程的优先级.默认是Thread.NORM_PRIORITY
  • delay(可选) 初始延迟的毫秒,在显示等待画面(返回wait结果码)前的等待时间.默认是没有等待延迟.
  • delaySleepInterval(可选) 只能和delay一起用.检查后台进程是否执行完毕的周期(毫秒).默认是100 毫秒

扩展

如果你需要在后台线程调用前后做一些准备或处理,你可以扩展BackgroundProcess类并实现beforeInvocation() 和afterInvocation()方法.在你需要获得和释放后台进程要成功执行所需的资源时,这一点很有用. 要使用你扩展了的后台进程类,你需要扩展ExecuteAndWaitInterceptor并实现getNewBackgroundProcess()方法.

例子

<action name="someAction" class="com.examples.SomeAction">
     <interceptor-ref name="completeStack"/>
     <interceptor-ref name="execAndWait"/>
     <result name="wait">longRunningAction-wait.jsp</result>
     <result name="success">longRunningAction-success.jsp</result>
 </action>
<%@ taglib prefix="ww" uri="/webwork" %>
 <html>
   <head>
     <title>Please wait</title>
     <meta http-equiv="refresh" content="5;url=<ww:url includeParams="all" />"/>
   </head>
   <body>
     Please wait while we process your request.
     Click <a href="<ww:url includeParams="all" />"></a> if this page does not reload automatically.
   </body>
 </html>

例子代码2:
这个例子在等待画面显示给用户前等待2分钟(2000毫秒).这样如果这个需要长时间的处理没有花费很长时间,用户就不会看到等待画面.

<action name="someAction" class="com.examples.SomeAction">
     <interceptor-ref name="completeStack"/>
     <interceptor-ref name="execAndWait">
         <param name="delay">2000<param>
     <interceptor-ref>
     <result name="wait">longRunningAction-wait.jsp</result>
     <result name="success">longRunningAction-success.jsp</result>
</action>

例子代码3:
这个例子在等待画面显示给用户前等待1分钟(1000毫秒).并且每隔50毫秒检查一下后台进程有没有执行完毕,如果完成了它就立刻返回,不用等到1分钟,用户不会看到等待画面.

<action name="someAction" class="com.examples.SomeAction">
     <interceptor-ref name="completeStack"/>
     <interceptor-ref name="execAndWait">
         <param name="delay">1000<param>
         <param name="delaySleepInterval">50<param>
     <interceptor-ref>
     <result name="wait">longRunningAction-wait.jsp</result>
     <result name="success">longRunningAction-success.jsp</result>
 </action>
HibernateAndSpringEnabledExecuteAndWaitInterceptor (WebWork2文档中文化计划)
Site running on a free Atlassian Confluence Open Source Project License granted to WebWork China. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.3 Build:#808 May 29, 2007) - Bug/feature request - Contact Administrators