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 {
              private Logger                      log;
              private MyDAO                       myDAO;
                  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


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

              public interface MyDAO {
                  public Word findRandomWord();

              import javax.ejb.Stateless;
              import javax.inject.Named;
              import javax.persistence.EntityManager;
              import javax.persistence.PersistenceContext;
              public class MyDAOBean implements MyDAO {
                  private EntityManager entityManager;
                  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.