0

I think, I don't really get concept of asynchronous Servlet. First of all, why we had to mark asyncSupport = true? Whether it true/false I can created ThreadPool and add task to it, to execute that task asynchronous, right? So what is the difference, what I cannot achieve without asyncSupport? Any example appreciated.

abc
  • 2,371
  • 3
  • 25
  • 36

1 Answers1

1

AsyncContext is convenient when you do need to keep the connection until you have some data to return after some time in the future without blocking the thread.

In one project I had a remote client which connected to the server and waited (long timeout) for the command from server. So called long-polling approach. Once server has some data to send it takes the AsyncContext and commits the response. It's very convenient as otherwise we would need to block a thread.

Alexey A.
  • 1,389
  • 1
  • 11
  • 20
  • So to make it clear. When I don't use async support, then using `ThreadPool` will execute task, but response would be send as soon as main servlet thread reach end of doGet/doPost method and I have no way to inform client about result of this process, right? On the other hand if I use async support, response would be send after async work would finish execution? So actually from point of view of browser there is nothing asynchronous about it? Just how the request would be process server side and that main thread of Servlet would back to thread pool and be ready for next request, right? – abc Jan 02 '14 at 18:42
  • Yes that's correct before async support the response is commited once you are returned from doGet/doPost. But with async you have a choice. From browser point of view nothing changes, it's just more convenient API for requests handling on server side. – Alexey A. Jan 02 '14 at 18:50