Java printing and multithreading

I wrote a small snippet called php to print .postscripts files on a predefined network printer.

While I am printing only one file, everything is going fine, but when I try to set up multi-threaded print management to send more than one file directly to the printer, only the first one comes out.

The problem is that PrintJobEvent PrintJobEvent.JOB_COMPLETE and PrintJobEvent.JOB_FAILED never happen, and the only events sent back by the print spooler are DATA_TRANSFER_COMPLETE and NO_MORE_EVENTS .

I searched the Internet and Sun forums, but could not find an answer. Thanks for any help :)

last-minute editing: if I run the application in debug mode from netbeans and I interrupt the first stream manually, the second file is sent to the printer ... so I think it should work somehow

+3
source share
2 answers

Actually, it makes no sense to send multiple files parallel to the printer. Why don't you create a queue and send jobs to a stream that will sequentially read data from the queue, and then send the results to the printer. If not, you will need to serialize the output.

You may have encountered problems initializing the printer in multiple threads.

A The printer can print only one job at a time.

+3

, :)

, , , , , , , , / /.

.

( ...), , - DATA_TRANSFER_COMPLETE NO_MORE_EVENTS...

.

btw, , :

PrintService

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();
    }
}

    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) {}
    }
}

Launcher

import print.MyPrintService;


public class Main {

    public static void main(String[] args)
    {
        System.out.println("C'est parti !");
        MyPrintService test1 = new MyPrintService("1");
    }
}
+1

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


All Articles