简体   繁体   中英

Returning to the main function after job completion in java

I have the codes here: For the main class:

 PrintingOperation printnew = new PrintingOperation();
 printnew.PrintingOperation(newfile);

 System.out.println("The file is to be deleted and will return from the start");

 File toDelete = listOfFiles[0]; //This output the full path of the file to be deleted
 System.out.println(toDelete); //I confirmed that it is a full path
 toDelete.delete(); //The problem occurs here -> It does not delete the file
 System.out.println("The file is deleted");

For the printing service:

   public String PrintingOperation(String file_name){

    System.out.println("The file name is: " + file_name);
    PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
    PdfDecoder decodePdf = new PdfDecoder(true);
    try {
    decodePdf.openPdfFile(file_name);
    FontMappings.setFontReplacements();

 } catch (Exception e) {
    //...
 }

//Setting the attributes
    PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
    JobName jobName = new JobName(file_name, null);
    attributeSet.add(jobName);
    attributeSet.add(new Copies(1)); //NUMBER OF COPIES
    attributeSet.add(MediaSizeName.ISO_A4); // Paper size : ISO_A4 or NA_LEGAL
    attributeSet.add(Chromaticity.COLOR); //COLOR or MONOCHROME
    decodePdf.setPrintAutoRotateAndCenter(false);

    PrintService[] services = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributeSet);
    for(PrintService s : services) {
      System.out.println(s.getName());
    }
        PrintService printingDevice = null; 
    for(PrintService s : services) {
      if(s.getName().equals("Brother MFC-5890CN Printer")) { //Change it to the printer available
           printingDevice = s; 
      }
    }
    PdfBook pdfBook = new PdfBook(decodePdf, printingDevice, attributeSet);
    SimpleDoc doc = new SimpleDoc(pdfBook, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
    DocPrintJob printJob = printingDevice.createPrintJob();
    JobCompleteMonitor monitor = new JobCompleteMonitor();
    printJob.addPrintJobListener(monitor);
    try {
      printJob.print(doc, attributeSet);
    } catch (PrintException e) {
        //...
    }
    monitor.waitForJobCompletion();


    System.out.println("Exiting printing process");      
    return null;
}
private static class JobCompleteMonitor extends PrintJobAdapter {

    private boolean completed = false;

    @Override
    public void printJobCanceled(PrintJobEvent pje) {
        signalCompletion();
    }

    @Override
    public void printJobCompleted(PrintJobEvent pje) {
        signalCompletion();
    }

    @Override
    public void printJobFailed(PrintJobEvent pje) {
        signalCompletion();
    }

    @Override
    public void printJobNoMoreEvents(PrintJobEvent pje) {
        signalCompletion();
    }

    private void signalCompletion() {

synchronized (JobCompleteMonitor.this) {

        completed = true;

        JobCompleteMonitor.this.notify();
}

  }

public synchronized void waitForJobCompletion() {

  try {

while (!completed) {

    wait();

}

  } catch (InterruptedException e) {

 }


}
}

Once the program reached the

printJob.print(doc, attributeSet)

The printer will start its operation and will print the document. However, it automatically returns to the main method and will not delete the file being printed. I placed the function for waiting for the completion of the print job but it still returns to the main method without waiting for the print job to finished.

The question is , how will I only return to the main method after the printing process is finished and will delete the file after the printing process? I used the system.exit() but I don't want the program to terminate, but I just want to return to the main method after the print job is finished to execute the remaining line of codes in the main method. I hope someone can help me regarding this.

Note: I also viewed the difference between return and system.exit() as well as the halt. I also checked that in order to delete the file, I need the full path for it to be deleted and I found out that I already placed the full path.

Why do you just delete the file in the job? I think add deleting code to the job is better. Or you can add a flag to the job when it has been finished, and add waiting code in main method (for example while true) to wait the flag be true, then do delete.

I found out the simplest answer for this one. It is only because I did not close the pdfFile.

The decodePdf.closePdfFile(); should be added before returning so that the file will be deleted.

:) Hope this will help other people :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM