2016-10-26 10 views
-1

ダウンロードボタンをクリックすると、SANロケーションからPDFがフェッチされます。ただし、XYZの理由により、ドキュメントがSANで使用できないことがあります。 5秒ごとに5秒後にSANロケーションのドキュメントをダウンロードして検索するようにポーリングメカニズムを実装する必要があります。また、繰り返し回数も返さなければなりません。検索が成功するとログに記録されます。Javaポーリング5秒後に5回

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 
      } 
     }   
    } 


} 
+2

これはうれしいことです - 何か問題がありますか? –

+0

@ScaryWombat - タイマー/ポーリングの仕組みを知りません。もしあなたがコードを助けることができるなら –

+0

5秒の睡眠を持つ単純なループについてどうですか? –

答えて

1

何か必要なのはタイマーです。 TimerTasksの使い方の例を次に示します。

まずTimer

Timer downloadTimer = new Timer(); 

あなたがTimerTaskのスケジュールを設定するまでは何もしません。

TimerTask downloadTask = new TimerTask() { 
    @Override 
    public void run() { 
     //try to download here 
    }; 
} 

を今すぐタスクをスケジュールする必要があります。

downloadTimer.schedule(downloadTask,1000,5000); 

このあなたのdownloadTimerにあなたにはを伝えますdownloadTaskが1秒(1000 milliseconds)で最初に実行され、次に5秒ごとに(5000 milliseconds)実行されます。これはトリックを行う必要がありますが、これが最善であるならば、私は言うことができない

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 
}; 

:あなたが正常にダウンロードまたはタスクが5回実行された後、あなたはタスクを停止しない限り、それが継続的に実行しますが

あなたの問題の解決策。

+0

Thnx @ gamedroidsしかし、なぜタイマーが特別なのか説明するのを助けることができます。 scarywombatが上で述べたように、睡眠の単純なループは同じことをしません。 while(content!= null || counter <5) {content = policyDownloadInstance.getPolicy(policyNumber); カウンタ++; if(content == null) { ログ----} Thread.sleep(5000); } –

+0

'Thread.sleep'でもよいでしょう。これはあなたの状況によって異なります。スレッドがスリープ状態に入っている場合は、すべてのリソースを保持しているため、他のスレッドをブロックします。たぶん[this post](http://stackoverflow.com/q/17826651/896249)は、それについていくつか光を当てて、何が最善であるかを判断するのに役立ちます。 「発信者」が5秒ごとまたは「サーブレット」を再試行するようにするのが最善であるかどうかも考慮する必要があります。サーブレットが応答するのに時間がかかり過ぎると、呼び出し元にタイムアウトが発生する可能性があることに注意してください。 – GameDroids

+0

ありがとう、私は睡眠を使用し、それはうまくいきました..どのようにスレッドがスリープするまで私のコードに待機ローダーを追加できますか? –

0

スレッドの睡眠はあなたにもawaitilityを使用することによって、これを達成することができます

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

私のためにうまくいきました。使用方法についてthis linkを確認してください

関連する問題