Cancel previous request

For each request, a lot of calculations occur. The average response takes about 10 minutes. Now, at the same time, if the user sends a new request; There is no need to continue the previous request.

So, I wrote code in which I basically interrupt the previous thread executing it. Is this good practice in tomcat environment? Could there be a better solution to solve the problem. Is it possible to interrupt tomcat threads. Or should I control my own thread flow and let the pool calculate for me?


Additional Information: Basically, the entire task completes using FutureTask. For each request, this task is performed, and the link to the task is stored using ConcurrentHashMap. For each request, the entire future on the map is β€œcanceled”, and then proceeds to fulfill the last request. Thus, the cancellation of previous requests.

+4
source share
2 answers

Q> I basically interrupt the previous thread executing it. Is this good practice in tomcat environment?
A> I think that it is normal while you are satisfied that the HTTP stream is blocked for 10 minutes. This means that no other user will be able to process HTTP requests. Otherwise, create and manage your own thread pool.

Q> Is this good practice in tomcat environment?
A> Interruption Runnable or Callable can be difficult. For example, if your thread is in the middle of an I / O operation, an interrupt may leave the data in a damaged state. Other than that, this is quite normal practice. I also recommend using your own thread pool so that your server performance is predictable.

Can you break your big task down into many small tasks? Keeping conditional and output early can be a good alternative to interruption.

Alternatively, waiting / securing the first task completes the operation, while others just return the same value in your environment? If so, I would prefer that instead of your approach. There LoadingCache from the guava library that does just that.

+2
source

I'm not sure I fully understand your question ...

But if you are talking about starting threadpool in a tomcat application and canceling its future tasks, I see no problems with that.

I would not interrupt the stream allocated by tomcat if I did not write the code to handle the interrupt personally (for example, in the servlet class)

0
source

Source: https://habr.com/ru/post/1440490/


All Articles