validate method gets called again and again while implementing exec and wait interceptor struts2

Thursday, July 3, 2014

I am implementing exec and wait interceptor for an action that takes 5-7 mins to process.The action class contains a function add(), validateAdd() method,file uploading methods and also uses session variables. validateAdd() method is written to validate a form that has a file marked as compulsory field.


The problem is that validateAdd() method gets called again and again. I tried debugging in eclipse.


As soon as the form is submitted the validateAdd() method is called 2 times then exec and wait interceptor is called.The screen starts showing wait page.Then again validateAdd() and then add function is called respectively.Mean while add() function is being executed validate function gets called again.


For first,second and third time the validateAdd() method gets the uploaded file and validates the form correctly. But the fourth time it goes to validate method an returns error page instead of success page.


Pls help me with this. Thanks



Here is my interceptor code

public class CustomExecAndWaitInterceptor extends ExecuteAndWaitInterceptor {

private static final long serialVersionUID = 1L;
protected BackgroundProcess getNewBackgroundProcess(String name, ActionInvocation actionInvocation,
int threadPriority) {

BackgroundProcess bgProcess = null;
System.out.println(".................................I am in Exec and Wait Interceptor....................................");
if (actionInvocation.getAction() instanceof MyActionClass) {


MyActionClass ptd = (MyActionClass)actionInvocation.getAction();
Details pd=new Details();
try {
File docFile = ptd.getDetails().getfilepath();
if (docFile != null) {
File altFile = new File(docFile.getParentFile(), docFile.getName() + "-alt.tmp");
FileUtils.copyFile(docFile, altFile);
String fileName=altFile.getName();
System.out.println("Alt File Name is"+fileName);
altFile.deleteOnExit();
pd.setFilepath(altFile);
pd.setFilepathFileName(fileName);

}
ptd.setDetails(pd);
}
catch (Exception ex) {
throw new RuntimeException("Error copying uploaded file", ex);
}
bgProcess = new CustomBackgroundProcess(name + "BackgroundThread", actionInvocation, threadPriority,ActionContext.getContext());
// }//


}
else {
bgProcess = super.getNewBackgroundProcess(name, actionInvocation, threadPriority);
}
return bgProcess;

}}












/**
* Wraps the standard {@link BackgroundProcess} to clean up alternate file created above.
*/
public class CustomBackgroundProcess extends BackgroundProcess {

/**
*
*/
private static final long serialVersionUID = 1L;
private final ActionContext context;


public CustomBackgroundProcess(String threadName, ActionInvocation invocation, int threadPriority, ActionContext actionContext) {
super(threadName, invocation, threadPriority);
this.context=actionContext;
}

@Override
protected void afterInvocation() throws Exception {
System.out.println("..................After Calling Action........................");
// super.afterInvocation();
ActionContext.setContext(null);

}
@Override
protected void beforeInvocation() {
System.out.println("..................Before Calling Action........................");
ActionContext.setContext(context);
}






http://ift.tt/1jMkvrN