我使用Java打印机API进行打印。我的打印机需要通过电线连接。所以,当电缆没有连接(连接到我的电脑)时,应抛出PrinterException。当我第一次运行我的程序时,抛出异常。但第二次,异常不会抛出,程序正常结束。重新启动操作系统后,第一次运行时会抛出异常。打印机API不会抛出异常
我的想法是打印机作业正在等待打印机连接。当我连接打印机时,以前的数据会自动打印。
我想知道是什么,我应该添加抛出PrinterException每当打印机断开连接。现在只有在第一次运行时才会抛出,我需要重新启动操作系统来测试行为。
这里是我的代码:
PrintService ps = PrinterService.getPrintService(
printer.getValue());
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.PORTRAIT);
aset.add(new JobName(" - Document", null));
aset.add(MediaSizeName.ISO_DESIGNATED_LONG);
DocPrintJob printjob = ps.createPrintJob();
Doc doc = new SimpleDoc(new PrintableReceipt(mydata),
DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);
try
{
printjob.print(doc, aset);
Thread.sleep(2000);
}
catch (PrintException printerException)
{
// printjob.removePrintJobListener(this);
printStatus.put("printStatus", false);
return addCORS(Response.serverError().entity(
JSON.encode(printStatus)).build());
}
嗯,我想你的假设是正确的:即使打印机没有连接,作业仍在排队。你的问题是什么? –
是的,我忘了添加我的问题。我的问题是“我想知道什么时候应该添加抛出PrinterException每当打印机断开连接,现在它只是第一次运行时才会抛出,我需要重新启动操作系统来测试行为。” – user3870776