2016-11-22 3 views
0
public String generateDataPDF() { 
    System.out.println("Inside generate PDF"); 
    String filePath = ""; 
    HttpSession sess = ServletActionContext.getRequest().getSession(); 
    try { 


     sess.setAttribute("msg", ""); 
     if (getCrnListType().equalsIgnoreCase("F")) { 
      try { 
       filePath = getModulePath("CRNLIST_BASE_LOCATION") + File.separator + getCrnFileFileName(); 
       System.out.println("File stored path : " + filePath); 
       target = new File(filePath); 
       FileUtils.copyFile(crnFile, target); 
      } catch (Exception e) { 
       System.out.println("File path Exception " + e); 
      } 
     } 


     System.out.println("Values from jsp are : 1)Mode of Generation : " + getCrnListType() + " 2)Policy Number : " + getCrnNumber() + " 3)Uploaded File Name : " + getCrnFileFileName() + " 4)LogoType : " + getLogoType() 
       + " 5)Output Path : " + getOutputPath() + " 6)Type of Generation : " + getOptionId() + " 7)PDF Name : " + getPdfName()); 

     String srtVAL = ""; 

     String arrayVaue[] = new String[]{getCrnListType(), getCrnListType().equalsIgnoreCase("S") ? getCrnNumber() : filePath, getLogoType().equalsIgnoreCase("WL") ? "0" : "1", 
      getOutputPath(), getGenMode(), getRenType()}; 

     //INS DB Connection 
     con = getInsjdbcConnection(); 

     ArrayList selectedCRNList = new ArrayList(); 
     String selectedCRNStr = ""; 
     selectedCRNStr = getSelectedVal(selectedCRNStr, arrayVaue[1]); 

     String[] fileRes = selectedCRNStr.split("\\,"); 
     if (fileRes[0].equalsIgnoreCase("FAIL")) { 
      System.out.println("fileRes is FAIL beacause of other extension file."); 
      sess.setAttribute("pr", "Please upload xls or csv file."); 
      return SUCCESS; 
     } 

     System.out.println("List file is : " + selectedCRNStr); 

     String st[] = srtVAL.split("[*]"); 
     String billDateStr = DateUtil.getStrDateProc(new Date()); 
     Timestamp strtPasrsingTm = new Timestamp(new Date().getTime()); 
     String minAMPM = DateUtil.getTimeDate(new Date()); 
     String str = ""; 
     String batchID = callSequence(); 
     try { 

      System.out.println("Inside Multiple policy Generation."); 
      String userName=sess.getAttribute("loginName").toString(); 
      String list = getProcessesdList(userName); 
      if (list != null) { 
       System.out.println("list is not null Users previous data is processing....."); 
       //setTotalPDFgNERATEDmSG("Data is processing please wait."); 
       sess.setAttribute("pr","Batch Id "+list+" for User " + userName + " is currently running.Please wait till this Process complete."); 
       return SUCCESS; 
      } 

      String[] policyNo = selectedCRNStr.split("\\,"); 

      int l = 0, f = 0,counter=1; 
      for (int j = 0; j < policyNo.length; j++,counter++) { 

       String pdfFileName = ""; 
       int uniqueId=counter; 
       globUniqueId=uniqueId; 
       insertData(batchID, new Date(), policyNo[j], getOptionId(), userName,uniqueId); 

       System.out.println("Executing Proc one by one."); 
       System.out.println("policyNo[j]" + policyNo[j]); 
       System.out.println("getOptionId()" + getOptionId()); 
       System.out.println("seqValue i.e batchId : " + batchID); 
      } 
        str = callProcedure(policyNo[j], getOptionId(), batchID); 
       String[] procResponse = str.split("\\|"); 
       for (int i = 0; i < procResponse.length; i++) { 
        System.out.println("Response is : " + procResponse[i]); 
       } 

       if (procResponse[0].equals("SUCCESS")) { 
        Generator gen = new Generator(); 

        if (getPdfName().equalsIgnoreCase("true")) { 
         System.out.println("Checkbox is click i.e true"); 
         pdfFileName = procResponse[1]; 
        } else { 
         System.out.println("Checkbox is not click i.e false"); 
         String POLICY_SCH_GEN_PSS = getDetailsForFileName(userName, policyNo[j], batchID); 
         String[] fileName = POLICY_SCH_GEN_PSS.split("\\|"); 
         if (getLogoType().equals("0") || getLogoType().equals("2")) { 
          System.out.println("If logo is O or 1"); 
          pdfFileName = fileName[1]; 
         } else if (getLogoType().equals("1")) { 
          System.out.println("If logo is 2"); 
          pdfFileName = fileName[0]; 
         } 

        } 

        b1 = gen.genStmt(procResponse[1], procResponse[2], "2", getLogoType(), "0", pdfFileName,"1",userName,batchID); 
        l++; 
        updateData(uniqueId,batchID, "Y"); 
       } else { 
        f++; 
        updateData(uniqueId,batchID, "F"); 
       } 

      }   
      sess.setAttribute("pr","Total "+l+" "+getGenericModulePath("PDF_RES1") + " " + " " + getGenericModulePath("PDF_RES2") + " " + f); 
     }catch (Exception e) { 
      updateData(globUniqueId,batchID, "F"); 
      System.out.println("Exception in procedure call"); 
      setTotalPDFgNERATEDmSG("Fail"); 
      e.printStackTrace(); 
      sess.setAttribute("pr", "Server Error."); 
      return SUCCESS; 
     } 
    }catch (Exception ex) { 
     ex.printStackTrace(); 
     sess.setAttribute("pr", "Server Error."); 
     return SUCCESS; 
    } 
    System.out.println("Above second return"); 
    return SUCCESS; 
} 

GenerateDataPDfメソッドはパラメータすなわちでProductType(GenMode)に基づいてPDFを生成変更され、CrnList(Excelファイルでアップロード...)コード際に正常に動作します単一のユーザーだけがPDFを生成します。しかし、2つの異なるユーザー(ユーザーとユーザーの役割がアプリケーションに割り当てられている場合)が同じ時間にリクエストを開始すると、パラメーターがオーバーライドされます。最初のユーザーが製品1の50人の顧客のpdfを要求すると仮定します。ユーザー1のプロセスはまだ実行中であり、ユーザー2はproduct2を要求しています。今、User1のpdfが生成されますが、product2 .....のために生成されます。ここではbatchIdはすべての単一のrequest.Oneテーブルで一意ですが、どこのbatch_id、すべてのpdf、生成フラグがそこに維持されているかが管理されています。これをどうすれば解決できますか?リクエストパラメータは、二つの異なるユーザーがコードと同じ時間にアクセスするときに

+0

すべてのデータを処理して別の関数で使用する関数を作成する...同期化、セマフォー、その他のスレッドロックを確認することもできます – Aks

+0

どちらの場合でも出力を同じファイルに書き込んでいますか?メモリ内バッファまたは少なくともランダムに名前を付けた一時ファイルを作成するべきではありませんか? – chrylis

+0

@Aksこんにちは、私はどこで同期をとるべきか説明してください。記事のカップルを読むが、多くの助けが見つかりませんでした! –

答えて

0

あなたのコメントによれば、これは私がやることです、それはおそらく最善の方法ではありません!

まず、すべてのデータを最初にコレットする関数を作成します。 PDFを生成しているときは、何も変更/更新/作成しないでください。 IE:array/list collectPDFData()アレイ/リストをリトゥンする必要があります。次に

:「同期」方法が同期、各方法シェア対応するオブジェクトの同じモニタを使用ときに同期を管理するためにモニターロックまたは固有ロックを使用synchronized boolean generatePDF(array/list)

ような同期方法を使用します。

NB:Synchronizeを使用している場合は、すべてのデータを別々に収集することはおそらく役に立たないかもしれませんが、特定のタスク専用の小さな関数を作成することをお勧めします。

このように、コードを少しリファクタリングする必要があります。

関連する問題