Servlet threading

I am working on a servlet that can take several hours to complete a request. However, the client calling the servlet is only interested in whether the request was received by the servlet or not. The client does not want to wait several hours before receiving any response from the servlet. In addition, since the servlet call is a blocking call, the client cannot act until a response is received from the servlet. To avoid this, I am thinking of starting a new thread in the servlet code. The thread launched by the servlet will take a long time so that the servlet can quickly return a response to the client. But I'm not sure if this is an acceptable way to deal with the blocking nature of servlet calls. I looked at NIO, but it seems that this is not what is guaranteed to work in any servlet container, as the servlet container is also based on NIO.

+3
source share
3 answers

What you need is a task scheduler, because they give confidence that the task will be completed, even if the server is restarted.

Check out java OSS Task Schedulers , most notably Quartz .

+4
source

Your decision is correct, but creating threads in enterprise applications is considered bad practice. It is better to use a thread pool or JMS queue.

You must consider what should happen, that the server crashes during processing, how to respond when several requests (I think hundreds or even thousands) occur simultaneously, etc. So you have chosen the right direction, but it is a little more complicated.

+2

, . . , , . - , , . :

  • ,
  • ( )
  • URL- .
  • , , .

. , . , .

+2
source

All Articles