wood burning stoves 2.0*
The moose likes Struts and the fly likes Threads within Struts framework Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Threads within Struts framework" Watch "Threads within Struts framework" New topic
Author

Threads within Struts framework

Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
We are using Struts and having a problem, while generating a pdf report based on some search condition. The scenario is as follows, we have a thread to time the pdf generation. After the report generation takes more than 20 seconds, we have to throw a message saying that �due to network congestion, the report is taking longer time. Please wait�. After we throw this message the pdf report has to be generated and displayed to the user after however long it takes for this to happen. The pdf report and the above mentioned error message has to be displayed on the same page i.e., the message if thrown has to be overwritten with the pdf after it is generated.
We have the code in place to time the report generation using thread, the problem is in throwing the error message (using struts) while letting pdf generation also happen at the same time.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It seems to me this problem could be solved with client-side code written in javaScript.

Using the javaScript setTimeout() function, you could display a message after so many seconds have elapsed. Here's a crude example:



Your struts application will just go on and create the pdf file, however long it takes. If it takes under 20 seconds, it will replace the current html document, and the message will never be displayed.

If it takes more than 20 seconds, the message will display after 20 seconds, and when the pdf is finished, it will replace the current page.

Merrill


Merrill
Consultant, Sima Solutions
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
My actoin class is as attached below. Now the "return forward" forwards the control to the JSP which loads the PDF file. I want to post the message, on the same page which wud load the PDF, incase time out occurs. However as you would infer, the next JSP gets loaded only if the PDF file has been completely retrieved from the URL. Consequently, the time out message never comes into picture as the JSP containing the setTimeOut method comes into the scene only after the PDF has been retrieved, the very thing it needs to time !!





public class GetPdfAction extends Action {

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws FSException {

//Do work
bytInfo=getPdfReportFromURL(request,response,url,sFlag);
}
catch(Exception e){
System.out.println("ERRRORRR :"+e);
}

session.setAttribute(FSConstants.FIN_STMT_BYTE_ARRAY,bytInfo);
}
}
else{
// code to retrieve the latest PDF.
}

forward = mapping.findForward(path); //Directs it to the next JSP file to print the PDF
}

return forward;

}

/**
* Description :Method to create a byte array of the PDF and add it as a session variable
* @param:reportURL
* @return : byte[]
* @throws :FSException
*/
private static byte[] getPdfReportFromURL(HttpServletRequest request,HttpServletResponse response,
URL reportURL, String sFlag)
throws FSException {

InputStream isPDF;
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
try {
isPDF = reportURL.openConnection().getInputStream();

byte[] buff = new byte[2048];
int bytesRead;

System.out.println("Content Type: " + reportURL.openConnection().getContentType());
System.out.println("Content Length: "+ reportURL.openConnection().getContentLength());

while (-1 != (bytesRead = isPDF.read(buff, 0, buff.length))) {
baosPDF.write(buff, 0, bytesRead);
}
if(baosPDF!=null && !sFlag.equals("0")) {

StringBuffer sbContentDispValue = new StringBuffer();
sbContentDispValue.append("inline"); //make it 'inline/attachment' to open the file directly
sbContentDispValue.append("; filename=");
sbContentDispValue.append("FinancialReport.pdf");
response.setHeader("Content-disposition",sbContentDispValue.toString());
response.setContentLength(baosPDF.size());
response.setContentType("application/pdf");
response.setHeader("Cache-Control", "max-age=30");
ServletOutputStream out =response.getOutputStream();
out.write(baosPDF.toByteArray());
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
throw new FSException("Unable to get Pdf from URL : Stream Error"+e);
}
return baosPDF.toByteArray();
}
}
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You might be able to solve this problem by using the "window.open()" javascript command to open a second window. Here is a possible scenario.

1. The jsp that is used to request the report performs the window.open() command immediately before it submits the request for the PDF.
2. The window.open() requests a simple html page that says something like "your report is being generated, please wait".
3. The html page also has an "onload" event in the body which will perform the setTimout command that will display another message after the time has elapsed.
4. when the jsp containing the pdf file is finally displayed, this jsp will also have an onload event that will send a blank html page with a target attribute of the new window. (using the location.replace() function in javaScript)
5. In the blank html page, there is an onload event that will close the window.

In this scenario, if the page is returned before the 20 seconds are up, the window will be closed.

This is a bit involved, and there may be a more elegant way to do this, but I do believe this would work.

Merrill
 
wood burning stoves
 
subject: Threads within Struts framework
 
Similar Threads
Request getting hanged..
creating a new thread from servlet
request time out
Iframe problem
Display image while page loads