首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java每隔5秒轮询5次

Java每隔5秒轮询5次
EN

Stack Overflow用户
提问于 2016-10-26 16:21:23
回答 3查看 1.9K关注 0票数 0

单击download按钮从SAN获取location.But有时由于XYZ原因,文档在SAN不可用。我需要实现一种轮询机制,这样单击download就会在每隔5秒搜索SAN位置的文档5次,并返回迭代号。在搜索成功时记录在日志中。

代码语言:javascript
复制
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 
            } 
        }        
    }


}
EN

回答 3

Stack Overflow用户

发布于 2016-10-26 16:36:58

你需要的是某种计时器。这里有一个关于如何使用TimerTasks的例子。

首先是Timer

代码语言:javascript
复制
Timer downloadTimer = new Timer();

在您安排一个TimerTask之前,它不会做任何事情

代码语言:javascript
复制
TimerTask downloadTask = new TimerTask() {
    @Override
    public void run() {
       //try to download here
    };
}

现在,您需要调度任务:

代码语言:javascript
复制
downloadTimer.schedule(downloadTask,1000,5000); 

这将告诉您的downloadTimer,您希望schedule downloadTask在1秒内第一次执行(1000 milliseconds),然后每5秒执行一次(5000 milliseconds)。

但是,它将持续运行,除非您在成功下载后停止任务或任务执行了5次:

代码语言:javascript
复制
private int count;
public void run() {
   if(count++==5){
       downloadTimer.cancel();  // will stop the timer
       downloadTimer.purge();   // will remove all canceled tasks from the timer
       return;   // makes sure the task will not be executed to the end
   }
   // try to download here
   // if download successful cancel and purge as well
};

这应该会起作用,但我不能说这是否是您的问题的最佳解决方案。

票数 2
EN

Stack Overflow用户

发布于 2016-11-03 02:08:22

线程睡眠对我来说效果很好

代码语言:javascript
复制
 for(int i=0;i<5;i++)
                 {
                     content=getPDAO.getPFromEb( strPN);
                      DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
                      Date dateobj = new Date();
                    if(content==null) 

                     {  
                        Thread.sleep(5000);
                        }
                    else {
                        content=getPDAO.getPFromEb( strPN);
                        break;
                    }
                 } 
票数 0
EN

Stack Overflow用户

发布于 2017-07-20 23:28:48

您也可以通过使用awaitility来完成此操作。请查看this link了解如何使用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40257137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档