
Dropwizard is using Jetty to build HTTP wev server/servelet container.
When a request comes into Jetty(DW), Jetty dispatches it to a thread from its thread pool.
DW follows a thread-per-request model, so every request are processed in separate threads.(limited size of work)
DW supports non-blocking I/O using asynchronous servelets.
Instead of keeping a thread during long-running job, the async servlet releases thread back to the thread pool so that the thread can hanlde other requests.
When async operation is initiated, async servlet returns thread immediately.
Then async servlet selects an avaliable thread from thread pool to process remaining works, generate response, and send it back to client.
import jakarta.ws.rs.container.AsyncResponse;
import jakarta.ws.rs.container.Suspended;
@Path("/example")
public class ExampleResource {
@GET
@Path("/async/{id}")
public void asyncResourceMethod(
@PathParam("id") String id,
@QueryParam("param1") String param1,
@QueryParam("param2") int param2,
@Suspended final AsyncResponse asyncResponse) {
// Perform long-running operation asynchronously
// ...
String result = "Response from async resource method for ID: " + id;
asyncResponse.resume(result);
}
}
Thats how dw can handle async operations.
asyncResponse.resume will response to client.
Basically async servelet and http request share same thread pool, so you should make your own threadpool if you want to use separate thread pool.
좋은 글 감사합니다.