starting a background process with a servlet

Discussion in 'Java' started by Miguel De Anda, Jul 1, 2003.

  1. 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?
     
    Miguel De Anda, Jul 1, 2003
    #1
    1. Advertising

  2. 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
     
    John C. Bollinger, Jul 2, 2003
    #2
    1. Advertising

  3. 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?
    >
    >
     
    Mikkel Heisterberg, Jul 2, 2003
    #3
  4. create a singleton thread object
     
    Ivan S Kirkpatrick, Jul 3, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. circuit_breaker
    Replies:
    2
    Views:
    2,081
    Jack Jia
    Apr 4, 2004
  2. robert
    Replies:
    6
    Views:
    1,044
    Bryce
    Oct 25, 2004
  3. Paul Hampton
    Replies:
    0
    Views:
    354
    Paul Hampton
    Jul 3, 2003
  4. Replies:
    1
    Views:
    442
    Fredrik Lundh
    Jun 7, 2006
  5. Replies:
    14
    Views:
    264
    Martijn Lievaart
    Aug 15, 2009
Loading...

Share This Page