1 Reply Latest reply on Dec 23, 2013 4:31 PM by itays100

    Inject Seam Component To Quartz Job

    itays100 Newbie

      Hi All,

       

      I've successfully integrated seam with Quartz using the interface org.quartz.Job as describe in the following link:

      http://oreilly.com/pub/a/java/archive/quartz.html?page=2

       

      I need to access the database through the Job class. In order to do so I annotate the Job class with

      seam annotations and injected a DAO object as follow:

       

      @Name("myJob")

      @Scope(ScopeType.EVENT)

      @AutoCreate

      public class MyJob implements org.quartz.Job {    

      @In

        private MyDaoHibernateImpl myDao;

      }

       

      The problem is that the myDao is null when executing the code.

      "myDao" is also the name of the component as required by seam.

       

      The scheduler is created exactly as described in the link above with 1 different, it is an application scope component:

      @Name("controller")

      @Scope(ScopeType.APPLICATION)

      @AutoCreate

      @Startup

      public class ScheduleController implements Serializable {

      @Create

          public void scheduleTimer() {

        

          try {

               SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();

               Scheduler sched = schedFact.getScheduler();

               sched.start();

               JobDetail jobDetail = new JobDetail(

                ...

                ...

      If you have any experience with injecting a seam component into a Job object please share.

       

      Thanks.

        • 1. Re: Inject Seam Component To Quartz Job
          itays100 Newbie

          Hello,

           

          I would like to share my solution for this problem.

          Because there is an issue to inject seam component into Quartz job I have added the following code

          to the execute method as follow:

           

          public void execute(JobExecutionContext arg0) throws JobExecutionException {

            

                  Lifecycle.beginCall(); 

                  EntityManager entityManager = (EntityManager) Component.getInstance("entityManager");

                  entityManager.getTransaction().begin();

                  MyDao myDao = (MyDao)org.jboss.seam.Component.getInstance("myDao"); 

                  dataFeedDao.setEntityManager(entityManager);

           

          The MyDao is used as a Seam component and being injected to other Seam component and

          the only time I used  dataFeedDao.setEntityManager(entityManager) is in Quartz job.

           

          Hope this help someone.