Struts2 execAndWait interceptor example for long running Actions

Sometimes we have long running actions where user will have to wait for final result. In this case, user can get annoyed with no response and may refresh the page causing issues in application.

Struts2 provide execAndWait interceptor that we can use for long running action classes to return an intermediate result to user while the processing is happening at server side. Once the processing is finished, user will be presented with the final result page.

Struts2 execAndWait interceptor is already defined in the struts-default package and we just need to configure it for our action classes. The implementation is present in ExecuteAndWaitInterceptor class that returns “wait” result page until the processing of action class is finished.

The interceptor provides two variables – delay to return the wait response for first time and delaySleepInterval to check if the action class processing is finished. Both of these variables can be overridden in struts configuration and measured in milliseconds.

We will look into this with a simple web application project whose project structure looks like below image. Create the dynamic web project in Eclipse with name as Struts2ExecAndWait and configure it as maven project.

Struts2-execAndWait-project

Project Configuration

Add below filter in web.xml to configure web application to use Struts2 framework.

Add struts2-core dependency in pom.xml file like below.

struts.xml configuration

struts.xml

The entry point of application is run.action through which user will provide time to execute the task in seconds. The task will be executed by ExecuteTaskAction. We have overridden the interceptors for ExecuteTask action to have defaultStack first and then execAndWait interceptor.

Note that execAndWait interceptor should be the last one in the interceptors stack. We are also overriding delay and delaySleepInterval values to 500ms. Default value for delay is 0 and delaySleepInterval is 100ms.

ExecuteAndWaitInterceptor returns wait result as intermediate response, so we need to configure this as result page for the action. Once the processing is finished success result is returned to the user.

Action Class

Action class is very simple and just waits for input time to return the success response. In real life, there might be heavy processing involved causing the delay in response.

JSP Pages

run.jsp

This is the entry point of the application and use in run action. We are required to have token in the form so that execAndWait interceptor can identify the request.

running.jsp

This is the intermediate response returned by the execAndWait interceptor. This page should have “refresh” meta where request will be sent to server to get the final response. If we won’t have this, then browser will not send the further requests and stuck on this page. We are refreshing the page every 2 seconds and passing all the input parameters to server. We are also showing processing.gif in the page to look like some processing is happening.

success.jsp

A simple response page that is the final result returned by the action class.

When we run the application, we get following response pages.

That’s all for execAndWait interceptor example, token is also used in Struts2 token interceptor to handle double form submission.

Read more about interceptors at Struts2 interceptors tutorial.

By admin

Leave a Reply

%d bloggers like this: