starting a background process with a servlet

M

Miguel De Anda

How can I use a servlet to start a background process that can be polled
anytime while its running? (I need to have a program run in the background
to verify data in a database to ensure that the links are up to date and a
few other administrative thigns.)

Also, how can I ensure that there is only one instance of it running?
 
J

John C. Bollinger

Miguel said:
How can I use a servlet to start a background process that can be polled
anytime while its running? (I need to have a program run in the background
to verify data in a database to ensure that the links are up to date and a
few other administrative thigns.)

Also, how can I ensure that there is only one instance of it running?

A servlet is not really the right tool for the job, although you will
find people who do recommend use of a servlet configured to be
initialized on webapp startup in the webapp's web.xml. In my opinion,
that's a hack. The better way to do it is to create a
ServletContextListener implementation that does the job for you, and
register that in your web.xml. That gives you a clean way to ensure
that your background process is started at webapp startup, and, if
desired, that it be signaled to stop at webapp shutdown. This sort of
job is precisely what ServletContextListeners are for.

Whether you do it by a servlet or by a ServletContextListener, the
actual details are similar. For a servlet you create the thread and
start it in the servlet's init() method; for a ServletContextListener
you do it in the contextInitialized(ServletContextEvent) method. If
this background process requires a clean shutdown then you put that code
in the servlet's destroy() method or the SCL's
contextDestroyed(ServletContextEvent) method; if an abrupt halt to the
background process is okay then you can just set the thread to be a
daemon thread before you start it -- but beware in that case, for you
will have problems the webapp is stopped and restarted without a full
shutdown / restart of the servlet container.

The servlet specification provides details on how to use these features,
and in particular on how to configure them in web.xml.

If you want only one instance of this thread for your webapp then either
of those approaches gives you that. If you want only one per your
entire application server then you need to get fancier and use a
Singleton object or something similar. If you want only one per your
database then you need to get fancy and rely on resources outside of the
JVM (perhaps in the database itself?) to enforce it.


John Bollinger
(e-mail address removed)
 
M

Mikkel Heisterberg

Depending on the app server / servlet container you are using I have a
suggestion. If running e.g. under Jetty or Tomcat under JBoss you could
write a MBean that could handle the maintenance for you. The MBean is
deployed separately and and be reached from the servlet(s). If you want
to get fancy the MBean could even implement JMX interfaces to let you
interact with the MBean from an external application.

lekkim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top