PrintJobListener Always Returns printJobNoMoreEvents
Hi,
In my Java application I print some text and afterwards I want to get the status of the job using the example in Determining When a Print Job Has Finished | Example Depot , however I always get the status "printJobNoMoreEvents" either the print operation has succeeded or not. I used OKI dot printer, HP laser printer and EPSON TM-U295 , but the results is the same for all of them.
By the way, I observe that Windows gets the status of the job correctly, namely it gives the error status if the job is not printed properly. Therefore I guess it may be something related with PrintJobListener interface. What should I do?
Thanks in advance.
Re: PrintJobListener Always Returns printJobNoMoreEvents
Re: PrintJobListener Always Returns printJobNoMoreEvents
Sorry, I am new to java forums and I was not aware of the fact that posting the same question to multiple forums is considered as wrong. I will be careful about that. However, this problem is very important to me and I will be grateful if some useful answer is supplied.
Thanks in advance.
Re: PrintJobListener Always Returns printJobNoMoreEvents
Quote:
Originally Posted by
Basak
... I always get the status "printJobNoMoreEvents" either the print operation has succeeded or not.
Can you post up your implementation of the PrintJobWatcher? The example you link to treats all print job events the same way, so I'm curious to know how you determine that you only get "printJobNoMoreEvents". Are you checking the PrintJobEvent object itself?
Re: PrintJobListener Always Returns printJobNoMoreEvents
Here is my code:
Code :
public class Printer implements PrintJobListener
{
private String printJobStatus = null;
private DocPrintJob docpj ;
public String print(String printerName, String printString){
PrintService printService = null;
String name = "";
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
for (PrintService ps : printServices) {
PrintServiceAttribute attr = ps.getAttribute(PrinterName.class);
name = ((PrinterName) attr).getValue();
if (name.equalsIgnoreCase(printerName)) {
printService = ps;
break;
}
}
if (printService == null) {
return "Printer is not available..";
}
StringBuffer printBuffer = new StringBuffer(50);
writeMessage(printBuffer, "STRING: " + printString, 1);
docpj = printService.createPrintJob();
docpj.addPrintJobListener(this);
DocFlavor df = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(printBuffer.toString().getBytes(), df, null);
try
{
docpj.print(doc, null);
// Wait till we know the status of our print job
while(printJobStatus == null)
{
// Wait for 1 second before checking to see if the job is complete.
try
{
Thread.sleep(1000);
}
catch (InterruptedException ignore)
{
ignore.printStackTrace();
}
}
}
catch (PrintException exception)
{
exception.printStackTrace();
return "Some exception thrown..";
}
return printJobStatus;
}
public void writeMessage(FileWriter fw, String message, int repeatCount) {
for (int i = 0; i < repeatCount; i++) {
try {
fw.write(message);
fw.write(13);
fw.flush();
} catch (IOException e) {
}
}
}
public void writeMessage(StringBuffer sb, String message, int repeatCount) {
for (int i = 0; i < repeatCount; i++) {
sb.append(message);
char c = 13;
sb.append(c);
}
}
@Override
public void printJobCompleted(PrintJobEvent pje)
{
printJobStatus = "printJobCompleted";
}
@Override
public void printJobNoMoreEvents(PrintJobEvent pje)
{
printJobStatus = "printJobNoMoreEvents";
}
@Override
public void printDataTransferCompleted(PrintJobEvent pje)
{
printJobStatus = "printDataTransferCompleted";
}
@Override
public void printJobCanceled(PrintJobEvent pje)
{
printJobStatus = "printJobCanceled";
}
@Override
public void printJobFailed(PrintJobEvent pje)
{
printJobStatus = "printJobFailed";
}
@Override
public void printJobRequiresAttention(PrintJobEvent pje)
{
printJobStatus = "printJobRequiresAttention";
}
}
In this code print() function always returns "printJobNoMoreEvents" as the printJobStatus string.
Thank you.
Re: PrintJobListener Always Returns printJobNoMoreEvents
OK, your code isn't allowing for multiple PrintJobEvents - remember that the NoMoreEvents notification should always be sent when the print service finishes a job. Your listener code effectively returns only the last event it receives, because the printJobStatus variable is overwritten by every event that arrives, so you're always going to see NoMoreEvents because that's always the last event. Try sending the printJobStatus to the console in every PrintJobEvent handler method to see what events are sent. You could use a list to hold the events received for a job, instead of a single printJobStatus variable.
However, it's possible (but unlikely) that the print service is at fault. The API docs say that the 'NoMoreEvents' notification is always sent:
"Not all print services may be capable of delivering interesting events, or even telling when a job is complete. This message indicates the print job has no further information or communication with the print service. This message should always be delivered if a terminal event (completed/failed/canceled) is not delivered. For example, if messages such as JOB_COMPLETE have NOT been received before receiving this message, the only inference that should be drawn is that the print service does not support delivering such an event".