5 Replies Latest reply on Mar 29, 2008 3:19 PM by pmuir

    Best practice for reading properties file?

    benmoore

      Hi,


      I'd like to periodically read a properties file containing application-wide settings. Is imagine using the timer service is the best approach, and that this is very common need. Is using a timer the best way?

        • 1. Re: Best practice for reading properties file?
          kahliburke.kahli.burke.wintermutesys.com

          We tried to use a timer for a similar purpose and found that for background tasks like these the timer service is annoying.  The issue is in starting it up and stopping it.  Because the timer service creates persistent timers, you start it up once and it will persist across server restarts, so if you always start it you end up with a bunch of timers.  There are ways of working around this like checking whether the timer is already there and only starting it isn't present, but an easier way is just to start your own thread.  You can hook up an observer to the start of the application with:


          @Observer("org.jboss.seam.postInitialization")
          public void someComnponentMethod() ...
          



          and then start your thread there.  The easiest way to get a thread up and running was something to create a Runnable with something like this:


             /**
               * Calls within seam contexts.
               */
              private void run()
              {
                  Lifecycle.beginCall();
                  try {
                      Events.instance().raiseEvent(eventName, this);
                  } catch (Throwable t)
                  {
                      if (t instanceof ThreadDeath)
                          throw (ThreadDeath)t;
                      
                      Logger.getLogger(SeamThread.class).error("Error while running seam background task for event " + eventName + ": ", t);
                      errorCount ++;
                  }
                  
                  Lifecycle.endCall();
              }
          



          We created a base SeamThread class that would take in an event name and trigger it with a specified interval.  This would in turn just call Events.raiseEvent and you can have a component that observes the event perform the task of reading the properties file.  Make sure you keep thread safety issues in mind if you are using an application scoped component!

          • 2. Re: Best practice for reading properties file?
            benmoore

            Hm, if you're going to do it that way, why not just use java.util.Timer and java.util.TimerTask, with the run() implementation reloading the properties (instead of raising an event)?

            • 3. Re: Best practice for reading properties file?
              mmichalek.mmichalek.micros-retail.com

              Sounds like a Timer or plain old thread would do the trick.  But isn't it generally a bad idea to spin up daemon threads which are outside of the container's control?


              Speaking of the container, does it give you anything you could leverage - like maybe scheduled JMX call?

              • 4. Re: Best practice for reading properties file?
                benmoore

                Mark Michalek wrote on Mar 27, 2008 04:48 PM:


                Sounds like a Timer or plain old thread would do the trick.  But isn't it generally a bad idea to spin up daemon threads which are outside of the container's control?


                I think you're thinking of spawning threads within EJBs. There's nothing wrong with spawning threads (Timer or traditional) from within an application server.

                • 5. Re: Best practice for reading properties file?
                  pmuir

                  I would do it using Seam's support for timers, initialized by the org.jboss.seam.postInitialization event. Use the Quartz dispatcher and you can then you have proper control over whether the timer is persisted.