If possible, for your servlet, accept a request for processing in the background, for a servlet, transfer processing to a separate thread, which then runs in the background.
Using Spring, you can invoke a separate thread using TaskExecutor . The advantage of using spring over the standard JDK 5 java.util.concurrent.Executor is that if you are on application servers that need to use managed threads (IBM websphere or Oracle weblogic), you can use WorkManagerTaskExecutor to connect to the work of CommonJ managers .
Another alternative would be to move the long request logic into a Message Driven Bean or Message Driven POJO ( Spring JMS can help here) and let the servlet just put the message in the JMS queue. This will have the advantage that if the loading of your web container becomes too large due to your long request, you can easily move the MDB to another (dedicated) system.
beny23
source share