Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > starting a background process with a servlet

Reply
Thread Tools

starting a background process with a servlet

 
 
Miguel De Anda
Guest
Posts: n/a
 
      07-01-2003
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?


 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      07-02-2003
Miguel De Anda wrote:
> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Mikkel Heisterberg
Guest
Posts: n/a
 
      07-02-2003
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

Miguel De Anda wrote:
> 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?
>
>


 
Reply With Quote
 
Ivan S Kirkpatrick
Guest
Posts: n/a
 
      07-03-2003
create a singleton thread object

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C++ 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C Programming 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui Python 0 04-27-2009 12:46 PM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
Starting and stopping a background process Paul Hampton Python 0 07-03-2003 09:58 AM



Advertisments