Use method ExecutorService.execute() to spawn some task in a background thread.
Steps to follow:
- Read some init parameters from
web.xml
in servletinit()
method such as timeout and threadpoolsize- timeout parameter is used to set the timeout of Async thread
- threadpoolsize is used to create a pool of Async threads
- Get
AsyncContext
by calling HTTPrequest.startAsync()
in doGet() or doPost() method - Set timeout of AsyncContext
- Attach listener to respond to lifecycle events of this AsyncContext such as
onComplete()
,onTimeout()
,onError()
,onStartAsync()
- Call
ExecutorService.execute()
to spawn some task in a background thread
I have already posted the code here Asynchronous servlet not acting asynchronously
Please have look and let me know if there is any confusion. It really works. You have full control on each life cycle method of sub-process to handle any type of exception.