2016-11-22 1 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; 
} 

단일 사용자 만 PDF를 생성합니다. 그러나 두 개의 다른 사용자 (사용자와 역할이 응용 프로그램에 할당 된 경우)가 요청을 처리 할 때 동일한 시간에 요청 매개 변수가 무시됩니다! 첫 번째 사용자가 제품 1에 대해 50 명의 고객에 대해 pdf를 요청했다고 가정 해보십시오. User1의 프로세스가 아직 실행 중이며 두 번째 사용자가 product2에 대해 요청하고 있습니다. 이제 User1의 pdf가 생성되었지만 product2 ..... 생성되었습니다! 여기서 batchId는 모든 단일 요청에 대해 고유합니다. 하나의 테이블은 batch_id, 모든 pdf, 생성 플래그가 유지되는 곳에서 유지됩니다. 어떻게 해결할 수 있습니까?요청 매개 변수가 변경 GenerateDataPDf 방법은 매개 변수를 기반으로 PDF는 ProductType (GenMode), CrnList을 즉 (엑셀 파일 업로드 ...) 코드 때 잘 작동 생성

+0

모든 데이터를 처리 한 다음 다른 함수에서 사용하는 함수를 만듭니다 ... 동기화 된, 세마포 및 다른 스레드 잠금을 확인할 수도 있습니다 – Aks

+0

두 경우 모두 동일한 파일에 출력을 쓰고 있습니까? 메모리 내 버퍼 또는 적어도 임의로 이름이 지정된 임시 파일을 만들어야하지 않습니까? – chrylis

+0

@Aks 안녕하세요, 동기를 어디에 사용해야하는지 설명해주십시오. 스레딩 개념에 익숙하지 않습니다. 몇 가지 기사를 읽었지만 많은 도움을 얻지 못했습니다! –

답변

0

귀하의 의견에 따라, 이것이 내가하는 일입니다. 아마도 최선의 방법은 아닙니다!

첫 번째 : 모든 데이터를 처음부터 콜링하는 함수를 만듭니다. PDF를 생성 할 때 아무 것도 수정/업데이트/생성해서는 안됩니다. IE : array/list collectPDFData() 배열/목록을 다시 가져와야합니다.

은 둘째 : 동기화 사용시

는 "동기"메소드는 그래서 동기화를 관리하기 위해 모니터 잠금 또는 극한 잠금을 사용 synchronized boolean generatePDF(array/list) 같은 동기화 된 방법을 사용하여, 각각의 방법을 공유 해당 개체의 동일한 모니터.

NB : 동기화를 사용하는 경우 별도의 방법으로 모든 데이터를 수집하는 것은 쓸모가 없겠지만 특정 작업 전용 전용 기능을 만드는 것이 좋습니다.

따라서 코드를 약간 리팩토링해야합니다.

관련 문제