Results 1 to 2 of 2
  1. #1
    aoi.onore is offline Member
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0

    Exclamation Java printing & multithread

    Hello folks, I hope this is the right forum where to post, it seemed the most appropriate to me.

    After some days looking for a solution and posting to every Java-board i know without answers, I come to you.

    To make it simple, I have to send to a network printer more files at once, so i've tried to build a pseudo-Queue managed by a Thread waiting for the previous file to be printed, but the problem is that the printer NEVER send me back that the job is finiched / failed / cancelled.

    I also tried to switch printer (never knows...) and the result is the same, the only events sent back are DATA_TRANSFER_COMPLETE and NO_MORE_EVENTS...

    Thanks previously for any advice :)

    btw, here the code i am actually running :

    PrintService
    Java Code:
    package print;
    
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import javax.print.Doc;
    import javax.print.DocFlavor;
    import javax.print.DocPrintJob;
    import javax.print.PrintService;
    import javax.print.PrintServiceLookup;
    import javax.print.SimpleDoc;
    
    public class MyPrintService extends Thread {
    
        String name;
    
        public MyPrintService(String name)
        {
            super(name);
            this.name = name;
            System.out.println("MyPrintService("+this.name+")");
            this.start();
        }
    
        public void print()
        {
            System.out.println("print()");
    
            try {
    
                for (int i = 1; i < 3; i++) {
    
                    // Open the image file
                    InputStream is          = new BufferedInputStream(new FileInputStream("D://giulio.provasi//Desktop//"+i+".txt"));
                    PrintService service    = PrintServiceLookup.lookupDefaultPrintService();
    
                    // Create the print job
                    DocPrintJob job         = service.createPrintJob();
                    Doc doc                 = new SimpleDoc(is, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
    
                    // Monitor print job events
                    PrintJobWatcher pjDone  = new PrintJobWatcher();
    
                    job.addPrintJobListener(pjDone);
    
                    // Print it
                    job.print(doc, null);
    
                    // Wait for the print job to be done
                    pjDone.waitForDone();
    
                    while(i < 10000000) {
                        System.out.println("le thread n'a pas attendu !");
                        i++;
                    }
    
                    // It is now safe to close the input stream
                    is.close();
                }
            } catch (Exception e) {
            }
        }
    
        @Override
        public void run()
        {
            System.out.println("run( "+this.name+" )");
            this.print();
        }
    }
    My Listener

    Java Code:
     package print;
    
    import javax.print.event.PrintJobEvent;
    import javax.print.event.PrintJobListener;
    
    public class PrintJobWatcher implements PrintJobListener {
    
        Boolean done    = false;
        Integer status  = 0;
    
        PrintJobWatcher()
        {
            System.out.println("PrintJobWatcher()");
        }
    
        @Override
        public void printDataTransferCompleted(PrintJobEvent pje)
        {
            this.done(PrintJobEvent.DATA_TRANSFER_COMPLETE);
            System.out.println("DATA_TRANSFER_COMPLETE");
        }
    
        @Override
        public void printJobCompleted(PrintJobEvent pje)
        {
            this.done(PrintJobEvent.JOB_COMPLETE);
            System.out.println("JOB_COMPLETE");
        }
    
        @Override
        public void printJobFailed(PrintJobEvent pje)
        {
            this.done(PrintJobEvent.JOB_FAILED);
            System.out.println("JOB_FAILED");
        }
    
        @Override
        public void printJobCanceled(PrintJobEvent pje)
        {
            this.done(PrintJobEvent.JOB_CANCELED);
            System.out.println("JOB_CANCELED");
        }
    
        @Override
        public void printJobNoMoreEvents(PrintJobEvent pje)
        {
            this.done(PrintJobEvent.NO_MORE_EVENTS);
            System.out.println("NO_MORE_EVENTS");
        }
    
        @Override
        public void printJobRequiresAttention(PrintJobEvent pje)
        {
            this.done(PrintJobEvent.REQUIRES_ATTENTION);
            System.out.println("REQUIRES_ATTENTION");
        }
    
        private synchronized void done(Integer status)
        {
            System.out.println("DONE !");
            this.status = status;
            this.done   = true;
            notifyAll();
        }
    
        synchronized void waitForDone()
            throws InterruptedException
        {
            System.out.println("AVANT : IMPRESSION EN COURS...");
            try {
                while (!this.done || ((this.status != PrintJobEvent.JOB_COMPLETE) || (this.status != PrintJobEvent.JOB_FAILED))) {
                    System.out.println("IMPRESSION EN COURS...");
                    wait();
                }
            } catch (InterruptedException e) {}
        }
    }
    and finally the Launcher
    Java Code:
    import print.MyPrintService;
    
    
    public class Main {
    
        public static void main(String[] args)
        {
            System.out.println("C'est parti !");
            MyPrintService test1 = new MyPrintService("1");
        }
    }

  2. #2
    Horcrux7 is offline Member
    Join Date
    Sep 2010
    Posts
    33
    Rep Power
    0

    Default

    You should no expect to receive all events on all platforms. I have never use the interface PrintJobWatcher. But I have work many with the print API on many different platforms.

    I think on Windows the print jobs are send directly to the print queue of windows. After you have send the Job under Windows then a program has no control over the Job anymore. That I think Java does no receive an event if the print job was printed.

    What you can help more is to request the status of the printer. You can call:
    service.getAttribute(category)
    with:
    PrinterState.class
    QueuedJobCount.class

    Can you update me if you find some interesting things. This will also important for me in the next time. I will translate the printing API to IKVM.

    Volker Berlin
    www.inetsoftware.de

Similar Threads

  1. simple question: A multithread situation
    By simorgh in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-13-2010, 08:25 PM
  2. Multithread Chat server/client
    By gwaldarick in forum Advanced Java
    Replies: 3
    Last Post: 09-19-2009, 12:22 AM
  3. Help design a program - multithread?
    By rushenas in forum New To Java
    Replies: 4
    Last Post: 01-23-2009, 06:41 AM
  4. Printing using java
    By ramachandran in forum Advanced Java
    Replies: 1
    Last Post: 01-05-2008, 10:16 AM
  5. [Java Printing]
    By remnahush in forum Advanced Java
    Replies: 1
    Last Post: 11-08-2007, 03:28 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •