1 Reply Latest reply on Jun 29, 2010 10:19 PM by kapitanpetko

    Quartz, Application Scope, Injection

    fender963

      Hello,


      I am setting up a quartz job to run in the background of my application. I can get the simple job to run just fine until I need to do anything generally conversation scoped in Seam.  Here are my files:


      
      @Name( "statusScheduleController" )
      @Scope( ScopeType.APPLICATION )
      @Startup( depends =
          {
              "quartzDispatcher"
          } )
      @AutoCreate
      public class StatusScheduleController
      {
      
          private QuartzTriggerHandle quartzTriggerHandle;
      
          @In( value = "statusSheduleProcessor")
          private StatusScheduleProcessor processor;
      
          private static final String KEY_STATUS_SCHEDULE_CRON_INTERVAL = "status.schedule.cron.interval";
      
          @Create
          public void startup()
          {
      
                  String cronInterval = PropertyUtil.getProperty( KEY_STATUS_SCHEDULE_CRON_INTERVAL );
      
                  quartzTriggerHandle = processor.createQuartzTriggerHandle( new Date(), cronInterval );
      
          }
      
      }
      
      



      @Name("statusScheduleProcessor")
      @Scope(ScopeType.APPLICATION)
      @AutoCreate
      public class StatusScheduleProcessor
      {
      
          @Asynchronous
          @Transactional
          public QuartzTriggerHandle createQuartzTriggerHandle( @Expiration Date when, @IntervalCron String interval )
          {
      
      
              MyEntity ent = new MyEntity();
              ent.setProperty("asdf");
              EntityManager entityManager = (EntityManager) Component.getInstance("entityManager");
              entityManager.persist(ent);        
      
              // just return null, seam will intercept and return appropriate QuartzTriggerHandle automatically.
              return null;
          }
      
      }
      


      Executing the code above throws:


      IllegalStateException: No active application scope 
      



      This is expected but my question is how do I work around this? How do I work generally done in conversation scoped context from an Application Scope?


      Thanks for the help. Sorry if anything is unclear or sounds stupid, I'm new to Seam.

        • 1. Re: Quartz, Application Scope, Injection
          kapitanpetko

          Unclear, it is, yes :) Basically you have a chicken-egg problem. When StatusScheduleController.create is called, there is no application context active (yet), so you can't inject the StatusScheduleProcessor. Make the processor STATELESS and it should work. Btw, the safer way to do stuff only once at Seam app start up is to write an @Observer for org.jboss.seam.postInitialization.


          What do you mean by 'conversation scoped context'? I don't see anything conversation related in your code.


          HTH