How to keep a class monitoring evey now and then


General J2EE: How to keep a class monitoring evey now and then

  1. Hi,

    I'd like to hear oppinions about how could be best designed a way to have a method running every second to check for a certain condition and preform some action, ina J2EE envirionment. This monitoring code should begin running at the application server startup and cleanup with its shutdown; it would also be nice being able to stop and restart it without doing the same on the server.

    I'm guessing if opening a thread from a startup servlet that gets into a loop sleeping for a second in each iteration would interfere with the container management and bring problems.

  2. Oh dear you've spotted the missing link, J2EE timers, and the limitation of the request/response model. The only solution at present is to fudge it. I can think of a couple of options:

    a) If you have JMS then you could have a Message Driven Bean (MDB) that does some processing and then sends a message to itself. Kick this off from the ejbCreate method of the MDB which should be called when the MDB instance pool is initialised. Starting / Stopping separately from the Server would probably need a enable / disable flag somewhere.

    b) If you haven't got access to JMS then you're going to have to do similiar to a) but use an HTTP request. You can either send this from a spawned thread, but this'll be hard to control and won't be monitored by the app server, or again from the recipient servlet / EJB.

    Basically what ever you do won't be elegant.


  3. Hi,

    What you could do is have your start up class call a static class that creates a new thread and adds it keeps it as a member variable. That way you can get a reference to the thread to stop it or start it. Destroy it or create a new one.

    We are using a similar design in our code to report on the threads we have started and it works well.