Determining if a printer can handle a print job without look-up

JavaPerformancePrinting

Java Problem Overview


I have been battling the PrintServiceLookup; the lookupPrintServices(DocFlavor flavor, AttributeSet attributes) method is excessively slow to detect printers in our application with the initial run-in. Clients with more than 100 network printers have reported that behaviour that executes this code is poorly performing the first time it is run.

After seeing that the look-up results are being cached, I have initially deployed a dummy look-up within a separate thread (executed at start-up). However, for a particular client this solution is not working.

I do not currently have their environment and cannot see what is causing the exact performance problem.

I am trying to see if a PrintService supports a given MediaSizeName without performing a look-up of DocFlavor and AttributeSet. So I pull all available PrintServices and the default PrintService:

private static final PrintService[] PRINTSERVICES = 
   PrintServiceLookup.lookupPrintServices(null, null);

private static final PrintService DEFAULTSERVICE = 
   PrintServiceLookup.lookupDefaultPrintService();

And then, obtain the PrintService and the MediaSizeName from the client request. Finally, I ask the PrintService if the MediaSizeName is supported by:

private void checkPrintServiceForMediaSize(PrintService pservice) throws MediaSizeNotSupportedException{
     if(!pservice.isAttributeValueSupported(_mediaSizeName,null,null))
    		throw new MediaSizeNotSupportedException("This media size is not supported by the selected printer.");
	 }

The API declares that when isAttributeValueSupported(Attribute attrval,DocFlavor flavor,AttributeSet attributes) is called with null DocFlavor and AttributeSet

> this method tells whether this Print Service supports the given printing attribute value for some possible combination of doc flavor and set of attributes

and has behaved correctly up until now. However, I am not entirely sure if this is the way to perform if a printer supports a selected page size.

I would appreciate your feedback and experience on this issue.


Update

Around the time I implemented my approach, my workstation decided to have serious network issues, which took me awhile to figure out. Finally, my implementation has been tested with the networking tool SoftPerfect Connection Emulator (to simulate network load) and the results have not improved significantly.

I will continue testing and update this question. Hopefully I can find a solution and share it with people here. I am guessing that the initial lookup:

private static final PrintService[] PRINTSERVICES = 
   PrintServiceLookup.lookupPrintServices(null, null);

is still causing issues.


Update 2

The beta build is finally tested on the client environment and performance of the printing dialog is about 5 times improved (the initial pull of printer now takes about 1 minute under the same environment compared to about 5 minutes). Still the initial wait time is not an acceptable amount of time, however, is the best I could do for now. We have also heard from the client that a print server is being used and following the suggestions in the comments (@Wardy), I will be moving on in this direction. Hopefully, we can leverage the advantages of the print server.

Java Solutions


Solution 1 - Java

More aggressive caching. Have the client perform the look-up once and persist the cache between restarts. Better yet, save the cache to a central data store which is accessible by all clients.

I'm assuming that network printers and their capabilities don't change that often but you have to update the cache eventually, but the "who" and "when" is dependent on your environment.

Updates to the cache can be made by a client that runs your current discovery in the background and if changes are detected updates the cache. If you have a central component that runs continuously anyway, that would be a good place where you can check in fixed intervals.

If you have some kind of directory service you can compare its list of printers with you cache before contacting each printer to get its capabilities to lessen network and cpu load.

Solution 2 - Java

If list of printers is stored in LDAP, you may try to lookup printers using LDAP.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionarinView Question on Stackoverflow
Solution 1 - JavaDarcaraView Answer on Stackoverflow
Solution 2 - Javakos32View Answer on Stackoverflow