How can a reload a servlet once a month?
We got some data which will change once a month, the data is for the servlet, but we don't need to save the data into DB, instead we want to make it a configuration file which will be replaced once a month, how can I do this?
I know that the servlet's lifecycle policy is controlled by the container, I am using websphere 7, but I don't know if there is a way to configure that in websphere.
Will calling the destory() method affect the running instances of servlet? AFAIK, the servlet is multi-threaded.
Don't use a servlet to store the data. Rather store the data as an attribute of the
ServletContext
. You can do it with help of aServletContextListener
. The very same listener class can also be used to reload the data at timed intervals with help ofScheduledExecutorService
.Here's a kickoff example:
(note that there's no
TimeUnit.MONTH
, so this is the best you can get for "once a month")Where the
Reloader
class look like this:After registering the listener in
/WEB-INF/web.xml
as followsit will create the
Data
instance and a single thread scheduler which executes thedata.reload()
every 30 days and you can access theData
instance in every servlet as follows:and in JSPs as follows: