Dropwizard - Jetty/thread

XYMON·2023년 7월 31일
post-thumbnail

Dropwizard is using Jetty to build HTTP wev server/servelet container.

HTTP request

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)

Async

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.

profile
염염

1개의 댓글

comment-user-thumbnail
2023년 7월 31일

좋은 글 감사합니다.

답글 달기