2 Replies Latest reply on Dec 6, 2007 12:16 PM by hypher

    Quartz Asynchronous events at startup

    hypher

      I have been running into problems with Quartz Asynchronous events which are scheduled to fire when the server is shut down. When the server is started again, the events get fired before seam is fully initialized. This occurs because the QuartzDispatcher is marked @Startup and it will cause all post-dated events to be fired as soon as it is created. The component that is handing the asynchronous event relies on other components which are marked @Startup. Since startup order is arbitrary, some of those components won't have been created when the event is fired, causing errors.

      The solution I have found to this issue is to override the QuartzDispatcher and wait until seam is initialized to initialize the scheduler:

      @Startup
      @Scope(ScopeType.APPLICATION)
      @Name("org.jboss.seam.async.dispatcher")
      @Install(value=false, precedence=APPLICATION)
      @BypassInterceptors
      public class CustomQuartzDispatcher extends QuartzDispatcher
      {
       @Override
       public void initScheduler()
       {
       /*
       * We don't actually want anything to happen on startup,
       * this will be handled by the post-init startScheduler method
       */
       }
      
       @Observer("org.jboss.seam.postInitialization")
       public void startScheduler()
       {
       super.initScheduler();
       }
      }
      


      This approach works perfectly for events which were scheduled before the server went down, and for events scheduled after seam is initialized. However, if a seam component tries to schedule an event during startup, it will fail because the scheduler has not been initialized.

      This could be fixed by keeping a queue of events to-be-scheduled in the QuartzDispatcher, and scheduling them with the scheduler once it is initialized.

      Does anyone have any ideas about this? I'll create JIRA issue about the startup issue soon.

        • 1. Re: Quartz Asynchronous events at startup

          Hypher,

          This is cool. I will add this to the QuartzDispatcher code. If you created a JIRA issue already, can you send me the link?

          As for your second point, I personally do not like the idea of keeping an event queue in QuartzDispatcher to just keep track of startup dependencies. Perhaps, we can just keep a boolean flag in QuartzDispatcher and other Seam components are responsible for checking that flag before scheduling tasks during startup?

          cheers
          Michael

          • 2. Re: Quartz Asynchronous events at startup
            hypher

            The JIRA issue is http://jira.jboss.com/jira/browse/JBSEAM-2353?page=all.

            A boolean flag would work as well. In my application, my custom dispatcher fires a "dispatcherInitialized" event when it did initialize, so components can observe that if they need to schedule an event near startup.

            Thanks