The ExecuteAndWaitInterceptor is great for running long-lived actions in the background while showing the user a nice progress meter. This also prevents the HTTP request from timing out when the action takes more than 5 or 10 minutes.
Using this interceptor is pretty straight forward. Assuming that you are including webwork-default.xml, this interceptor is already configured but is not part of any of the default stacks. Because of the nature of this interceptor, it must be the last interceptor in the stack. A typical configuration looks like:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
... <action name="myLongRunningAction" class="..."> <interceptor-ref name="defaultStack"/> <interceptor-ref name="execAndWait"/> <result name="wait">longRunningAction-wait.jsp</result> <result name="success">longRunningAction-success.jsp</result> </action> ... |
This interceptor works on a per-session basis. That means that the same action name (myLongRunningAction, in the above example) cannot be run more than once at a time in a given session. On the initial request or any subsequent requests (before the action has completed), the wait result will be returned. The wait result is responsible for issuing a subsequent request back to the action, giving the effect of a self-updating progress meter.
An example of a simple wait JSP is:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<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> |
Whenever the wait result is returned, the action that is currently running in the background will be placed on top of the stack. This allows you to display progress data, such as a count, in the wait page. By making the wait page automatically reload the request to the action (which will be short-circuited by the interceptor), you can give the appearance of an automatic progress meter.
To set the Thread priority, add the parameter threadPriority (defaults to Thread.NORM_PRIORITY):
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<interceptor name="execAndWait" class="com.opensymphony.webwork.interceptor.ExecuteAndWaitInterceptor"/> <interceptor-stack name="defaultStack"> ... <interceptor-ref name="execAndWait"> <param name="threadPriority">10</param> </interceptor-ref> </interceptor-stack> |
The thread will receive the name of the action with 'BackgroundThread' appended.
Note |
---|
Because the action will be running in a seperate thread, you can't use ActionContext because it is a ThreadLocal. This means if you need to access, for example, session data, you need to implement SessionAware rather than calling ActionContext.getSesion(). |
Extending the Interceptor
If you wish to make special preparations before and/or after the invocation of the background thread, you can extend the BackgroundProcess class and implement the beforeInvocation() and afterInvocation() methods. This may be useful for obtaining and releasing resources that the background process will need to execute successfully. To use your background process extension, extend ExecuteAndWaitInterceptor and implement the getNewBackgroundProcess() method.