Is it possible to implement a background process in a servlet!?
Let me explain.
I have a servlet that shows some data and generate some reports.
The generation of the report implies that the data is already present, and it is this: someone else upload these data.
In addition to report generation, I should implement a way to send an email on arrival of new data (uploaded).
The functional requirement is unclear, but to answer the actual question: yes, it’s possible to run a background process in servletcontainer.
If you want an applicationwide background thread, use
ServletContextListenerto hook on webapp’s startup and shutdown and useExecutorServiceto run it.If you’re not on Servlet 3.0 yet and thus can’t use
@WebListener, register it as follows inweb.xmlinstead:If you want a sessionwide background thread, use
HttpSessionBindingListenerto start and stop it.On first creation and start, just do