Finishing a HttpServletResponse but continue processing

后端 未结 4 1133
不思量自难忘°
不思量自难忘° 2021-02-15 11:14

I have a situation that seems to fit the Async Servlet 3.0 / Comet situation but all I need to do is return a 200 response code (or other) after accepting the incoming parameter

4条回答
  •  心在旅途
    2021-02-15 11:57

    Here's how I've handled this situation:

    1. When the app starts up, create an ExecutorService with Executors.newFixedThreadPool(numThreads) (there are other types of executors, but I suggest starting with this one)
    2. In doPost(), create an instance of Runnable which will perform the desired processing - your task - and submit it to the ExecutorService like so: executor.execute(task)
    3. Finally, you should return the HTTP Status 202 Accepted, and, if possible, a Location header indicating where a client will be able to check up on the status of the processing.

    I highly recommend you read Java Concurrency in Practice, it's a fantastic and very practical book.

提交回复
热议问题