use of execAndWait interceptor

use of execAndWait interceptor

The execAndWait interceptor in the Struts 2 framework is used to handle long-running actions or operations asynchronously, providing a responsive user interface while the action is being processed in the background. This is particularly useful in scenarios where an action involves heavy processing, such as complex calculations, database operations, or external API calls, which could potentially block the server and make the application appear unresponsive to users.

use of execAndWait interceptor

Explanation

  • 1. Asynchronous Processing:
    • Definition:  Asynchronous processing allows the server to handle multiple tasks concurrently without blocking the user interface. This is achieved by executing tasks in the background while the user can continue interacting with the application.
    • Usage:  Useful for actions that involve time-consuming operations, ensuring a responsive user experience by separating the processing from the user interface thread.
  • 2. ExecAndWait Interceptor:
    • Definition:  The execAndWait interceptor is provided by Struts 2 to manage asynchronous processing of actions. It displays a loading indicator or progress bar to the user while the action is being executed in the background.
    • Usage:  Helps in improving user experience by preventing the browser from timing out due to long-running requests and by showing visual feedback (e.g., spinning wheel) during the execution.

Example in Java

Let’s illustrate the use of execAndWait interceptor in a Struts 2 application with a simple example.

Syntax
java
// pom.xml (Maven dependency for struts2-core)
<!-- Struts 2 Core -->
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.x.x</version> <!-- Replace with the latest version -->
</dependency>


xml
<!-- struts.xml (Struts 2 configuration) -->
<struts>
    <package name="default" extends="struts-default">
        <interceptors>
            <interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecAndWaitInterceptor"/>
        </interceptors>
        
        <default-interceptor-ref name="execAndWait"/>
        
        <action name="longRunningAction" class="com.example.LongRunningAction">
            <result name="success">/result.jsp</result>
            <interceptor-ref name="defaultStack"/>
        </action>
    </package>
</struts>


java
// LongRunningAction.java (Struts 2 action class)
import com.opensymphony.xwork2.ActionSupport;

public class LongRunningAction extends ActionSupport {

    public String execute() throws Exception {
        // Simulating a long-running operation
        Thread.sleep(5000); // Simulate 5 seconds of processing
        
        return SUCCESS;
    }
}


jsp
<!-- result.jsp (JSP page to display result) -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Result Page</title>
</head>
<body>
    <h1>Long Running Action Completed Successfully!</h1>
</body>
</html>

Explanation

  • 1.  Struts Configuration (struts.xml):
    • The execAndWait interceptor is configured in struts.xml within the <interceptors> section. It is referenced for the longRunningAction action using <default-interceptor-ref> to ensure that this interceptor is applied by default to this action.
  • 2.  Action Class (LongRunningAction.java):
    • LongRunningAction class simulates a long-running operation using Thread.sleep(5000), which represents a 5-second delay.
  •    – This delay simulates heavy processing that would normally cause the server to block.
  • 3.  JSP Page (result.jsp):
    • Upon successful completion of the action, result.jsp is rendered to indicate that the long-running action has completed.
  • 4.  User Experience:
    • When a user triggers longRunningAction, Struts intercepts the request and starts executing it asynchronously using execAndWait interceptor.
    • During the 5-second processing period, the application displays a loading indicator or progress bar to inform the user that the action is in progress.
    • After completion, the user is redirected to result.jsp to see the outcome of the action.

Conclusion

The execAndWait interceptor in Struts 2 enhances user experience by handling long-running actions asynchronously, preventing the user interface from freezing and providing visual feedback to users during processing. This is achieved by separating the action execution from the main thread and managing the asynchronous behavior effectively within the Struts framework.