7 Replies Latest reply on Aug 16, 2009 8:05 PM by Diego Coronel

    Problems with Quartz and the Startup

    Héctor r Newbie

      Hy, I'm trying to create a schedule task with Quartz in the startup and it's impossible! I follow other topics, books, Seam documentation and it doesn't work.


      I add the code:


      package org.domain.meteo.controllers;
      
      import java.util.Calendar;
      import java.util.Date;
      
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Observer;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.Startup;
      import org.jboss.seam.annotations.async.Asynchronous;
      import org.jboss.seam.annotations.async.Expiration;
      import org.jboss.seam.annotations.async.FinalExpiration;
      import org.jboss.seam.annotations.async.IntervalDuration;
      import org.jboss.seam.async.QuartzTriggerHandle;
      
      @Name("iniciador")
      @Scope(ScopeType.APPLICATION)
      @Startup(depends={"quartzDispatcher"})
      public class Iniciador {
      
      
              @Observer("org.jboss.seam.postInitialization")
              public void realizarMedidasProgramadas() { 
               
                System.out.println("Enter" );
                
                
                Calendar iCalendar = Calendar.getInstance();
              iCalendar.set(2009, Calendar.AUGUST, 13, 18, 26 );
                
              Calendar fCalendar = Calendar.getInstance();
              fCalendar.set(2009, Calendar.AUGUST, 13, 18, 30 );
                
              
      
           QuartzTriggerHandle handle = medidasProgramadas(iCalendar.getTime(), 1000l, fCalendar.getTime());
          
           }
           
           @Asynchronous
           public QuartzTriggerHandle medidasProgramadas(@Expiration Date cuando, @IntervalDuration Long intervalo, @FinalExpiration Date fechaFinal ){               
           
                     
                     System.out.println("TASK" );
                     
      
                return null;
           }
           
           
      }
      
      
      



      <?xml version="1.0" encoding="UTF-8"?>
      <components xmlns="http://jboss.com/products/seam/components"
                  xmlns:core="http://jboss.com/products/seam/core"
                  xmlns:persistence="http://jboss.com/products/seam/persistence"
                  xmlns:drools="http://jboss.com/products/seam/drools"
                  xmlns:bpm="http://jboss.com/products/seam/bpm"
                  xmlns:security="http://jboss.com/products/seam/security"
                  xmlns:mail="http://jboss.com/products/seam/mail"
                  xmlns:web="http://jboss.com/products/seam/web"
                  xmlns:async="http://jboss.com/products/seam/async"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation=
                      "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.1.xsd
                       http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.1.xsd
                       http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.1.xsd
                       http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.1.xsd
                       http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.1.xsd
                       http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.1.xsd
                       http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.1.xsd
                       http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.1.xsd
                       http://jboss.com/products/seam/async http://jboss.com/products/seam/async-2.1.xsd" >
      
         <core:init debug="true" jndi-pattern="@jndiPattern@"/>
      
         <core:manager concurrent-request-timeout="500"
                       conversation-timeout="120000"
                       conversation-id-parameter="cid"
                       parent-conversation-id-parameter="pid"/>
      
         <!-- Make sure this URL pattern is the same as that used by the Faces Servlet -->
         <web:hot-deploy-filter url-pattern="*.seam"/>
      
         <persistence:entity-manager-factory name="meteoEntityManagerFactory" 
                                 persistence-unit-name="meteo" installed="false"/>
                                 
         <persistence:managed-persistence-context name="entityManager"
                                 auto-create="true"
                                 entity-manager-factory="#{meteoEntityManagerFactory}" 
                                 persistence-unit-jndi-name="java:/meteoEntityManagerFactory"/>
      
      
         <drools:rule-base name="securityRules">
            <drools:rule-files><value>/security.drl</value></drools:rule-files>
         </drools:rule-base>
      
         <security:rule-based-permission-resolver security-rules="#{securityRules}"/>
      
         <security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true"/>
      
         <event type="org.jboss.seam.security.notLoggedIn">
            <action execute="#{redirect.captureCurrentView}"/>
         </event>
         <event type="org.jboss.seam.security.loginSuccessful">
            <action execute="#{redirect.returnToCapturedView}"/>
         </event>
      
         <mail:mail-session host="localhost" port="25"/>
         
       <async:quartz-dispatcher />
      
         <!-- For use with jBPM pageflow or process management -->
         <!--
         <bpm:jbpm>
            <bpm:process-definitions></bpm:process-definitions>
            <bpm:pageflow-definitions></bpm:pageflow-definitions>
         </bpm:jbpm>
         -->
      
      </components>
      
      
      



      Thanks

        • 1. Re: Problems with Quartz and the Startup
          lance stine Newbie

          Maybe you should return a QuartzTriggerHandle instead of null in your medidasProgramadas function.


          like this:

          QuartzTriggerHandle handle = new QuartzTriggerHandle("MedidasProgramadas");
          return handle;

          • 2. Re: Problems with Quartz and the Startup
            Héctor r Newbie

            Thanks but it doesn't work...


            In the book Seam Framework: Experience the Evolution of Java EE (2nd Edition) (JBoss Series) says:


            Notice that the method declares that it returns a QuartzTriggerHandle object, but we
            do not construct that object in the method. We merely return a null value. Seam intercepts
            the method and returns an appropriate QuartzTriggerHandle automatically to
            its caller.


            • 3. Re: Problems with Quartz and the Startup
              Héctor r Newbie

              The methos is executed once at the Startup (not in the date and hour that I want) but it doesn't repeat the call... I don't know what is happening.

              • 4. Re: Problems with Quartz and the Startup
                lance stine Newbie
                Although I don't have the book you are quoting from, I have had success with Quartz. Here are two classes that work well together. Maybe using this pattern will help.

                the observer

                @Name("quartzObserver")
                public class SCSQuartzObserver {
                     
                     @In(create=true) SCSQuartzTask quartzTask;
                     
                     @SuppressWarnings("unused")
                     @Observer("org.jboss.seam.postInitialization")
                     public void observe()
                     {
                     try
                     {
                          Calendar cal = Calendar.getInstance ();
                          cal.set (2040, Calendar.MAY, 10);
                          QuartzTriggerHandle handle = quartzTask.performTask(new Date(), 86400000l, cal.getTime());
                     }
                     catch(Exception e)
                     {
                          e.printStackTrace();
                     
                          }
                     }
                }


                and the task


                @Name("quartzTask")
                @AutoCreate
                public class SCSQuartzTask {
                     
                     @Asynchronous
                     public QuartzTriggerHandle performTask(@Expiration java.util.Date when,
                     @IntervalDuration long duration,
                     @FinalExpiration java.util.Date endDate)
                     {
                     //do stuff
                     QuartzTriggerHandle handle = new QuartzTriggerHandle("SCSQuartzTask");
                     return handle;
                     }
                }

                • 5. Re: Problems with Quartz and the Startup
                  Héctor r Newbie

                  I can't belive it!! You save me!! Youhoooooo ;)


                  Thanks it works perfectly.

                  • 6. Re: Problems with Quartz and the Startup
                    Yasser Hawari Newbie

                    if u need both to do this in the same class u should do the following:


                    ...
                    
                    Iniciador obj = (Iniciador) Component.getInstance("iniciador", ScopeType.EVENT);
                    
                     QuartzTriggerHandle handle = obj.medidasProgramadas(iCalendar.getTime(), 1000l, fCalendar.getTime());
                    
                    ...
                    
                    



                    Seam in action book by Dan Allen explains why u should do this in chapter 6 section 6.4.1.


                    in short, internal method calls within the same class do not trigger bijection :), thus u could not have ur asynchronous function.

                    • 7. Re: Problems with Quartz and the Startup
                      Diego Coronel Apprentice

                      Quartz/Mail/Async is working for me in seam 2.1.2.GA without problem, just, no workaround was necessary