4 Replies Latest reply on Oct 1, 2011 10:05 PM by Taimur Mirza

    Cron job and managed bean?

    Taimur Mirza Newbie

      How can I invoke my managed bean in cron method? I want to fetch some data at particular time when cron job reaches to this method it throws weird exceptions.


      Any idea?

        • 1. Re: Cron job and managed bean?
          Taimur Mirza Newbie

          Let me explain more.


          public class EmailScheduler {
          
              @Inject
              private Logger                      log;
          
              @Inject
              private MyDAO                       myDAO;
          
                  @Inject
                  EmailService                                    emailService;
          
                  public void deliverEmail(@Observes @AtMidNight Trigger trigger){
                     log.info("Scheduled job is triggered");
                     Collection<MyObject> = myDAO.findAllObjects();
                  }
          }



          It throws exception when I access MyDAO bean inside this triggered job. Any idea what I am not understanding?


          • 2. Re: Cron job and managed bean?
            Sebastian Sachtleben Novice
                    public void deliverEmail(@Observes @AtMidNight Trigger trigger, Logger log, MyDAO myDAO, EmailService emailService){
                       ...
                    }



            Should be fine too. Please post your MyDAO class.

            • 3. Re: Cron job and managed bean?
              Taimur Mirza Newbie

              Sebastian,


              Thank you so much for your reply, I am copying my MyDAO class here




              @Local
              public interface MyDAO {
              
                  public Word findRandomWord();
              }





              import javax.ejb.Stateless;
              import javax.inject.Named;
              import javax.persistence.EntityManager;
              import javax.persistence.PersistenceContext;
              
              @Stateless
              @Named("myDAO")
              public class MyDAOBean implements MyDAO {
              
                  @PersistenceContext
                  private EntityManager entityManager;
              
                  @Override
                  public Word findRandomWord() {
                      return (Word)entityManager.find(Word.class, 1);
                  }
              }
              





              • 4. Re: Cron job and managed bean?
                Taimur Mirza Newbie

                I have tried this way too but it is throwing the same exceptions like myDAO object is not getting properly instantiated and throwing connection error.
                Though myDAO object works fine if I invoke from any other action and it fetches data from database. It only doesn't work if it is getting used by cron method.