單擊下載按鈕可從SAN位置獲取PDF。但由於XYZ原因,有時文檔在SAN中不可用。我需要實施輪詢機制,以便點擊下載每5秒鐘後在SAN位置搜索文檔5次,並返回第5行的迭代。在搜索成功的日誌中。每5秒後進行5次Java輪詢
package abc.documentdownload;
import abc.util.Email;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class DownloadDocServlet extends HttpServlet {
private static final Log log = LogFactory.getLog(DownloadDocServlet.class);
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
DownloadDocDAO DownloadInstance = new DownloadDocDAO();
String downloadType = request.getParameter("downloadType");
String pNumber = request.getParameter("PNumber");
BufferedOutputStream output = null;
String strFileName = pNumber + ".pdf";
if(downloadType != null && downloadType.equalsIgnoreCase("download")){
try{
byte[] content=DownloadInstance.getP(pNumber);
log.info("COnverting content into PDF in EmailServlet");
System.out.println("COnverting content into PDF in EmailServlet");
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment; filename=\"" + strFileName + "\"");
response.setHeader("Cache-Control","no-cache");
response.setHeader("Cache-Control","no-store");
response.setHeader("Pragma","no-cache");
response.setDateHeader("Expires", 0);
output = new BufferedOutputStream(response.getOutputStream());
output.write(content);
output.flush();
output.close();
}
catch (Exception ex) {
ex.printStackTrace();
log.error("Error in DownloadDocServlet ", ex);
/* Using the below block to trigger the email whenever there is a error*/
Writer result = new StringWriter();
PrintWriter printWriter = new PrintWriter(result);
ex.printStackTrace(printWriter);
Email emailSend = new Email();
int strEmailConfirm = emailSend.sendEmail("Exception in DownloadDocServlet of documentdownload package for pno :"+pNumber,"<B>Please find Exception Details for the DownloadDocServlet of documentdownload package</b><br><br>"+result.toString());
log.info("strEmailConfirm in DownloadDocServlet"+strEmailConfirm); // if value is 1 , mail will be trigger is successful
}
}
}
}
這很好 - 任何問題? –
@ScaryWombat - 我不知道如何使用定時器/輪詢機制。如果你可以幫助代碼 –
簡單的循環與5秒的睡眠怎麼樣 –