I have a multitenant system (Java EE) running on Glassfish 4, which receives roughly 500 requests for a printing job at a time at a specific period in the year. Before this period, resources are enough to handle these requests, but at this period, the requests simply become too much for the server to handle, which leads to a lot of downtimes. My idea to solve the problem is to simply bring some order to how these requests are handled. That is, a sort of first-come-first-serve thing where one request is handled, and then the next is attended to until there are no more requests. I tried to build it as a sort of service which continuously checks to see if there are any requests, and then services the requests in order.
As per the solutions I have found from StackOverflow and also lots of searching online, I have narrowed it down to a few. But I have some concerns:
Scheduling: Most implementations of job scheduling I have seen each requires some recurring interval for a task to be performed. This won't work for my system because the time for each printing job to be completed is dependent on how many pages of reports would be generated. It could be 5 pages and it could be 50. In other words, I don't know how long a request would take to be serviced.
Java Messaging Service (JMS): I thought about using JMS queues, but I just didn't understand how I could relate it to my current situation. I understand it's meant for messaging and could maybe solve a part of my problem, but I'm yet to see how.
Endless looping: This seems very tacky and quite frankly, a hack I would rather not even try on a Java EE application and on a system that's lacking resources.
I would appreciate suggestions as to how I would, in summary, implement a system that would endlessly receive requests, service them irrespective of how long they would take, and move on to the next request. If there aren't any requests, it waits. If there are too many requests, it simply services them in the order in which they are received.
First Edit: So after giving it some thought, considering multitenancy, and overall complexity of my current system, I decided to create another system which would receive client requests to generate these results. This proposed system would not itself generate reports, but would simply ask the current system to generate the report which would subsequently be emailed to the client. The queuing of requests (I think) can be achieved in the proposed system. Now I just need to figure out how as this system would also be a Java EE application. Maybe this is the point some of these answers come into play. Your thoughts would be deeply appreciated.