8 Replies Latest reply on Jan 15, 2008 6:29 AM by cavani

    Start quartz cron when Seam apps is up

    thejavafreak

      Dear all,

      Is it possible that the quartz cron started when Seam apps is up? I get the impression in order to run the cron it must be triggerred from the screen and then call the method that is annotated with @Asynchronous.

      But there are times I need a cron to run not to be called from screen but when the apps is up. Is this possible with Seam quartz support?

      Thanks in advance

        • 1. Re: Start quartz cron when Seam apps is up
          nickarls

          You could probably place an observer on org.jboss.seam.postInitialization and tickle it from there.

          • 2. Re: Start quartz cron when Seam apps is up
            marx3

            or maybe use servlet with
            <load-on-startup>1</load-on-startup> ?
            What's better?

            • 3. Re: Start quartz cron when Seam apps is up
              nickarls

               

              "Marx3" wrote:
              or maybe use servlet with
              <load-on-startup>1</load-on-startup> ?
              What's better?


              If you are using seam-stuff in your job it is probably best waiting for seam to signal that the init is complete (and it saves you from writing a servlet and mapping it)

              • 4. Re: Start quartz cron when Seam apps is up
                thejavafreak

                 

                "nickarls" wrote:
                You could probably place an observer on org.jboss.seam.postInitialization and tickle it from there.


                Hi Nick,

                Thanks for the hint. I'm going to read the document and try it first.

                Thanks,

                • 5. Re: Start quartz cron when Seam apps is up
                  cavani

                  I am using this:

                  
                  @Name("bootstrap")
                  @Scope(ScopeType.APPLICATION)
                  @BypassInterceptors
                  @Startup
                  public class Bootstrap
                  {
                  
                   @Create
                   public void start() throws Exception
                   {
                   Events.instance().raiseTimedEvent("comeEvent", new CronSchedule((Long) null, "0 0 6 * * ?"));
                   }
                  
                   @Destroy
                   public void stop()
                   {
                   }
                  
                  }
                  


                  It's working, but may not be exactly what you want.

                  Other way (once setted you can't get cron trigger anymore, so...):

                  @Name("synchronizationTimer")
                  @Scope(ScopeType.APPLICATION)
                  @Startup
                  public class SynchronizationTimer
                  {
                  
                   private static final String defaultTrigger = "0 0 3 * * ?";
                  
                   @Logger
                   private Log log;
                  
                   private QuartzTriggerHandle handle;
                  
                   private String trigger = defaultTrigger;
                  
                   @Create
                   public void start() throws Exception
                   {
                   // uncomment this if you want set timer on startup
                   setTrigger(defaultTrigger);
                   }
                  
                   public void restoreDefault()
                   {
                   setTrigger(defaultTrigger);
                   }
                  
                   public void setTrigger(String trigger)
                   {
                   try
                   {
                   QuartzTriggerHandle handle = QuartzDispatcher.instance().scheduleTimedEvent("someEvent", new CronSchedule((Long) null, trigger));
                  
                   if (this.handle != null)
                   this.handle.cancel();
                  
                   this.handle = handle;
                   this.trigger = trigger;
                   }
                   catch (Exception e)
                   {
                   log.error("Erro configurando timer: #0", e, trigger);
                   }
                   }
                  
                   public String getTrigger()
                   {
                   return trigger;
                   }
                  
                   public void cancel()
                   {
                   try
                   {
                   if (handle != null)
                   handle.cancel();
                   handle = null;
                   trigger = null;
                   }
                   catch (Exception e)
                   {
                   log.error("Erro configurando timer: #0", e, trigger);
                   }
                   }
                  
                   @Destroy
                   public void stop()
                   {
                   cancel();
                   }
                  
                  }
                  


                  Thanks,

                  • 6. Re: Start quartz cron when Seam apps is up
                    cavani

                    Sorry by portuguese log, but it is very easy to undestand.

                    Second, I put the comment but didn't comment the code on start method, so...

                    Both are Seam components and it is interesting to use:

                    @In
                    private SynchronizationTimer synchronizationTimer;
                    


                    to control the timer.

                    Thanks,

                    • 7. Re: Start quartz cron when Seam apps is up
                      thejavafreak

                      I've got it working now. I was just curious why didn't you have an Observer method for the comeEvent?

                      • 8. Re: Start quartz cron when Seam apps is up
                        cavani

                        sorry, I show you half side of code (timed event generator), so yes... and this name was a typo.... should be "someEvent"... you must set observer over some public method on any seam component.

                        In my case, it is on another component that it is SLSB:

                        @Local
                        public interface Handler
                        {
                         void doSomething();
                        }
                        
                        @Name("xxx")
                        @Stateless
                        public HandlerBean implements Handler
                        {
                        
                         // ...
                        
                         @Observer("someEvent")
                         public void doSomething()
                         {
                         // ...
                         }
                        }
                        


                        That way, the business logic code stay with EJB...

                        Thanks,