AsyncContext Interface important methods
In this section , you will get to know about important methods of AsyncContext Interface.
AsyncContext can be initialized using startAsync() method. The need of AsyncContext is given below :
Need of AsyncContext
Often during handling request, your application thread is waiting for some external resource due to which it become idle for some time. Due to this, you are engaging the thread and therefore a lot of memory is occupied by you without doing any function.
Consider a situation where your application is providing the downloading of files with limited output. In this case, your threads are idle most of the time since they are awaiting to send next bundle of data. Prior to Servlet 3.0 , you couldn't attend/process more than your HTTP thread limit.
With Servlet 3.0, you can attend/ process thousands of connections concurrently which is much more than thread limit. It means you can connect to thousands of client with few HTTP threads.
Method startAsync() doesn't create any thread. Means it will not create new thread for every async request. It just tells the Servlet container do not close this request until in tell you to do so.
The essential methods / functions of AsyncContext Interface is given below :
AsyncContext Method |
Functionality |
addListener(AsyncListener listener) | Add the AsyncListener instance to the currently existing most recent asynchronous cycle(AsyncContext instance). |
addListener(AsyncListener listener, ServletRequest servletRequest, ServletResponse servletResponse) | Add the AsyncListener instance to the currently existing most recent asynchronous cycle(AsyncContext instance). |
complete() | This method is used when the asynchronous operation was completed. |
createListener(java.lang.Class<T> clazz) | AsyncListener class can be instantiated by this method. |
dispatch() | This method dispatches the URI returned by HttpServletRequest.getRequestURI(),
if the AsyncContext was initialized
via the startAsync(ServletRequest,ServletResponse) and the request passed is an instance of HttpServletRequest. Otherwise the dispatch is to the URI of the request when it was last dispatched by the container. |
dispatch(ServletContext context, java.lang.String path) | This dispatch() method accepts a String argument describing a path within the scope of the ServletContext specified. This path must be relative to the root of the ServletContext specified and should starts with a ?/?. |
dispatch(java.lang.String path) | This dispatch() method takes a String argument as path which must be inside the scope of the ServletContext. This path must be relative to the root of the ServletContext and begin with a ?/?. |
getRequest() | This method returns the request that was used to initialize this AsyncContext. |
getResponse() | This method returns the response that was used to initialize this AsyncContext. |
getTimeout() | Using this method, you can get timeout in milliseconds which was set for this AsyncContext. |
hasOriginalRequestAndResponse() | Using this method, you can verify whether the request and response objects, used to initialize this AsyncContext, is original or application-wrapped. |
setTimeout(long timeout) | Using this method, you can set timeout in milliseconds for this AsyncContext. |
start(java.lang.Runnable run) | Using this method, you can dispatch a thread to run the assigned Runnable. |