Skip to main content

Servlet 3.0 Async and Threads

1 reply [Last post]
good2go
Offline
Joined: 2009-01-08
Points: 0

Over at the Servlet 3.0 spec lead's blog (http://weblogs.java.net/blog/mode/archive/2008/12/asynchronous_su.html#c...) there's a post regarding the Asynchronous support ... It offers an example of using the AsyncContext to wait for a Web Service to return:

@WebServlet("/foo" asyncSupported=true)
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) {
...
AsyncContext aCtx = request.startAsync(req, res);
ScheduledThreadPoolExecutor executor = new ThreadPoolExecutor(10);
executor.execute(new AsyncWebService(aCtx));
}
}
...
public class AsyncWebService implements Runnable {
AsyncContext ctx;
public AsyncWebService(AsyncContext ctx) {
this.ctx = ctx;
}
public void run() {
// Invoke web service and save result in request attribute
// Forward the request to render the result to a JSP.
ctx.forward("/render.jsp");
}
}

I posed my question as follows ... maybe someone here can offer insight:

This may be just my ignorance ... but ... from the code here, it looks like the request handling is going to spawn a thread that sits around until the web service returns and then render the response ... isn't the point of having the non-blocking IO to avoid consuming threads while we wait?

Or am I missing something???

Best ... and Happy New Year ...

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jmcconnell
Offline
Joined: 2009-02-27
Points: 0

spawning a thread is fine, so long as your not consuming the threads in your servlet thread pool which is what is largely limiting your ability to service requests