Java -Check if the file is in the print queue / Usage

OK I have a program that:

  • Creates a temporary file based on user login
  • Prints a file (optional)
  • Deletes a file (optional)

My problem sits between steps 2 and 3, I need to wait for the file to finish printing until I can delete it.

FYI: printing will take 5-10 minutes (a large file will be downloaded to the old computer)

So I need to be able to check with Java:

  • defualt print queue is empty

  • the file is used (note: File.canWrite () returns true when printed)

+3
source share
1 answer

API Java? http://download.oracle.com/javase/1.4.2/docs/api/javax/print/event/PrintJobListener.html:

PrintJobListener

DocPrintJob .

, , .

exampledepot.com/egs/javax.print/WaitForDone.html: (: URL-, , )

try {
    // Open the image file
    InputStream is = new BufferedInputStream(
        new FileInputStream("filename.gif"));
    // Create the print job
    DocPrintJob job = service.createPrintJob();
    Doc doc = new SimpleDoc(is, flavor, null);

    // Monitor print job events
    PrintJobWatcher pjDone = new PrintJobWatcher(job);

    // Print it
    job.print(doc, null);

    // Wait for the print job to be done
    pjDone.waitForDone();

    // It is now safe to close the input stream
    is.close();
} catch (PrintException e) {
} catch (IOException e) {
}

class PrintJobWatcher {
    // true iff it is safe to close the print job input stream
    boolean done = false;

    PrintJobWatcher(DocPrintJob job) {
        // Add a listener to the print job
        job.addPrintJobListener(new PrintJobAdapter() {
            public void printJobCanceled(PrintJobEvent pje) {
                allDone();
            }
            public void printJobCompleted(PrintJobEvent pje) {
                allDone();
            }
            public void printJobFailed(PrintJobEvent pje) {
                allDone();
            }
            public void printJobNoMoreEvents(PrintJobEvent pje) {
                allDone();
            }
            void allDone() {
                synchronized (PrintJobWatcher.this) {
                    done = true;
                    PrintJobWatcher.this.notify();
                }
            }
        });
    }
    public synchronized void waitForDone() {
        try {
            while (!done) {
                wait();
            }
        } catch (InterruptedException e) {
        }
    }
}
+5

Source: https://habr.com/ru/post/1764205/


All Articles