2016-11-22 15 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方法生成基於參數PDF即ProductType(GenMode),CrnList(在Excel文件上傳...)的代碼工作正常時,只有一個用戶生成PDF。但是,如果兩個不同的用戶(用戶和角色分配在應用程序中)啓動該進程,同時請求參數被覆蓋,那麼!假設50個客戶第一用戶請求PDF產品1用戶1的進程仍在運行和產品2第二個用戶請求。現在User1的pdf生成,但產品2 .....!在這裏,batchId對於每個請求都是唯一的。一個表保存了batch_id,所有pdf,生成標誌都在那裏運行。我該如何解決這個問題?從JSP頁面來請求參數改變

+0

作出這樣的處理所有數據,然後使用它在其他功能的功能...你還可以檢查同步,信號量和其他螺紋鎖 – Aks

+0

你寫在這兩種情況下,輸出到同一個文件?你不應該創建一個內存緩衝區或至少一個隨機命名的臨時文件嗎? – chrylis

+0

@Aks嗨能否請您解釋一下我應該在哪裏使用同步...我是新線程的概念。閱讀幾篇文章,但沒有找到太多幫助! –

回答

0

根據你的評論,這是我會做的,這可能不是最好的辦法!

首先:創建一個函數在開始時收集所有數據。生成PDF時,不應修改/更新/創建任何內容。 IE:array/list collectPDFData()這應該是一個數組/列表。

其次:使用一個同步的方法,如synchronized boolean generatePDF(array/list)

「同步」的方法使用監視器鎖或內部鎖以便管理同步,以便當使用同步,每個方法份額對應對象的同一監視器。

注:如果您使用的同步,它可能是無用的,收集所有的數據在一個單獨的方式,但我認爲這是一個很好的做法,使小功能專用於特定的任務。

因此,你的代碼需要重構一點點。