FileDocCategorySizeDatePackage
BatchingPrinter.javaAPI DocExample2250Thu Nov 08 00:22:48 GMT 2001com.ora.rmibook.chapter12.printer.printers

BatchingPrinter

public class BatchingPrinter extends UnicastRemoteObject implements Printer

Fields Summary
private LinkedList
_printQueue
private Printer
_realPrinter
private Object
_printerLock
private boolean
_currentlyPrinting
Constructors Summary
public BatchingPrinter(Printer realPrinter)


         
        _printQueue = new LinkedList();
        _realPrinter = realPrinter;
        (new BackgroundThread()).start();
    
Methods Summary
private synchronized DocumentDescriptiongetNextDocumentFromQueue()

        while (0 == _printQueue.size()) {
            try {
                wait();
            } catch (Exception ignored) {
            }
        }
        DocumentDescription nextDocument = (DocumentDescription) _printQueue.remove(0);

        return nextDocument;
    
public synchronized booleanprintDocument(DocumentDescription document)

        _printQueue.add(document);
        notifyAll();
        return true;
    
private voidprintNextDocument()

        try {
            DocumentDescription documentToPrint = getNextDocumentFromQueue();

            setCurrentlyPrinting(true);
            _realPrinter.printDocument(documentToPrint);
            setCurrentlyPrinting(false);
        } catch (Exception ignored) {

            /*
             This is a real issue-- what do we do with PrinterExceptions
             when we've batched things up like this. 
             */
        }
    
public synchronized booleanprinterAvailable()

        if (_currentlyPrinting) {
            return false;
        }
        return _realPrinter.printerAvailable();
    
private synchronized voidsetCurrentlyPrinting(boolean currentlyPrinting)

        _currentlyPrinting = currentlyPrinting;