The Execute and Wait Interceptor
(execAndWait) 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. Click Here for more info.
Let’s see how we can use it –
1. Welcome.jsp -
This
is the initial jsp which will request for some processing.
<%@page
contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags"
prefix="s" %>
<!DOCTYPE
html>
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP
Page</title>
</head>
<body>
<h1>execAndWait
interceptor</h1>
<h2>
This Web app shows how
to use execAndWait Interface <br/>
to show a page when
business logic taking long time <br/>
and you want to show
another page, till the execution<br/>
is not completed
successfully.
</h2>
<br/>
<br/>
<s:form name="frm"
action="waitAction.action">
<s:submit
value="Process ..."/>
</s:form>
</body>
</html>
2. Wait.jsp
The page which
will shows when user is waiting for server response.
<%@ taglib
prefix="s" uri="/struts-tags" %>
<html>
<head>
<head>
<meta
http-equiv="refresh" content="2;"/>
<title>Please Wait
...</title>
</head>
<body>
<div
style="width:200px">
Please wait...
</div>
</body>
</html>
3. Done.jsp
This is the jsp will be displayed after
processing
<%@page
contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE
html>
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>Thanks for waiting
...</title>
</head>
<body>
<h1>Process
completed!!!</h1>
<h2>Thanks for waiting!!
</h2>
</body>
</html>
4. Action
Class:
This
Action is called from the welcome.jsp file. It will process the actual business
logic. Name it as “WaitAction.java”
package
action;
import
com.opensymphony.xwork2.ActionSupport;
public class
WaitAction extends ActionSupport {
/* default constructor */
public WaitAction() {
}
@Override
public String execute() {
try {
/* wait for 12000 ms */
Thread.sleep(12000);
} catch (Exception e) {
System.out.println(e);
}
return SUCCESS;
}
} // end of
class
5.
Make
an entry in struts.xml file for action class –
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.
Important: Because the action will be running in a separate 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.getSession().
Parameters
for execAndWait Interceptor:
threadPriority
(optional) - the priority to assign the thread.
Default is Thread.NORM_PRIORITY.
delay
(optional) - an initial delay in millis to
wait before the wait page is shown (returning wait as result code). Default is
no initial delay.
delaySleepInterval
(optional) - only used with delay. Used for
waking up at certain intervals to check if the background process is already
done. Default is 100 millis.
<package
name="default" extends="struts-default">
<action
name="waitAction" class="action.WaitAction">
<interceptor-ref
name="defaultStack"/>
<interceptor-ref
name="execAndWait">
<param
name="delay">1500</param>
</interceptor-ref>
<result
name="wait">/jsp/Wait.jsp</result>
<result
name="success">/jsp/Done.jsp</result>
</action>
</package>
6. Entry in web.xml
file –
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>jsp/welcome.jsp</welcome-file>
</welcome-file-list>
This would be folder structure of
your project, after developing all the necessary files
| IMG 01: Folder Structure |
Now deploy and run the webapp.
| IMG 2: Welcome.jsp |
Welcome.jsp
| IMG 03: Wait.jsp |
Wait .jsp
Done.jsp
| IMG 04: Done.jsp |
Thanks for reading ....Namaste ....