Printing a list is a basic function in the ordering system that is widely used in the catering industry. In catering, POS printers are generally used. However, in many ordering systems, dropping orders is a headache. This article provides a solution on how to use Java to control printing by bypassing the driver and handle dropped tickets.
The requirement in this article is that a document is printed by multiple printers at the same time. When a document is not printed successfully, it needs to be re-printed (replenishment ). Connect the printer to the vro and confirm that the printer is normal.
The idea of this article is to use queue and thread operations.
This article creates several objects:
1. Printer object printer: printer number, printer IP address, printer port, default paper width, etc.
2. Print the printableobject object: encapsulate the information to be printed.
3. Print job object printjob: it encapsulates a printableobject object and contains a status bit. The possible statuses include "waiting for printing", "printing", and "Finishing printing ". It also contains a print method.
4. Print the queue printqueue: a single instance is used to simulate a queue. The method is thread-safe. Each element is a printjob object. The elements are output first, failed to print, and returned to the end of the queue.
5. Print the capture object printabletracker: This object is a thread and a single instance. The printjob is continuously retrieved from the printqueue. The print is successful and the next one is printed. Otherwise, the current job is moved to the end of the queue and the next one is printed. If no printjob exists in the queue, the process ends. When the printjob is added to the printqueue again, the thread is started to continue printing.
6. printutils: Refer to the command manual provided by the printer to encapsulate the print command and call the command encapsulated by printutils in the print method of printjob for printing. In the print method, establish a socket connection to communicate with the printer.
The above implementation was tested and passed in the Jiabo thermal network port printer. Solve the Problem of ticket loss caused by network problems or printer paper problems. However, when the print command has been sent out, and the printer is powered off, it cannot be re-printed when a call is sent. In the program, this operation has actually been completed, but these commands exist in the network or printer cache, and the information does not exist after the power is down. This is because the printer cannot determine whether the printer has any paper. At least not found in Jiabo's printer command manual. However, if other printers can, you can receive this status in the socket to determine whether the print is successful. In this way, the probability of losing a ticket is lower.
This is the way of thinking, for your reference only!