7 Replies Latest reply on Jul 12, 2016 4:07 PM by jseanjensen

    Why is my EntityManager null in my Quartz Job on Wildfly 10?

    jseanjensen

      I'm trying to implement an upload job in my app running on Wildfly 10. I want to create a Quartz job that will download some files and load them in the database. However when my job runs the entitymanager is always null. How can I get my entity manger injected in this case? I wrote the following code that simplifies my situation as much as possible. Can anyone tell me where I've gone wrong?

       

      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import org.quartz.Job;
      import org.quartz.JobExecutionContext;
      import org.quartz.JobExecutionException;

      @Stateless
      public class DownloadService implements Job {

        @PersistenceContext
        private EntityManager entityManager;

        @Override
        public void execute(JobExecutionContext arg0) throws JobExecutionException {
        if (entityManager == null) {
        System.out.println("############## entityManager is null ####");
        } else
        System.out.println("************** WORKING ***************");

        }
      }

        • 1. Re: Why is my EntityManager null in my Quartz Job on Wildfly 10?
          ctomc

          as quartz manages lifecycle not EE container.

           

          you should use Java EE native @Scheduled Schedule (Java(TM) EE 7 Specification APIs)

          for your timer jobs

          • 2. Re: Why is my EntityManager null in my Quartz Job on Wildfly 10?
            jseanjensen

            Thanks for replying and I'll check out the link and see if I can use it. 

            I would like to understand why what I was trying to do doesn't work though.  Is there a way to make what I was trying to do work?  Is there some documentation that would explain it to me like I'm 5? 

            • 3. Re: Why is my EntityManager null in my Quartz Job on Wildfly 10?
              smarlow

              Regarding the entityManager being null, I would look for a few possible causes.  Does your application contain a persistence.xml file?  Does the persistence.xml have exactly one persistence unit defined in it or multiple persistence units?  If you have multiple persistence units, you need to identify one of them as default (set persistence unit hint "wildfly.jpa.default-unit" to true) or change the application code to identify the persistence unit name:

               

              @PersistenceContext(unitName = "yourPersistenceUnitName")
              private EntityManager entityManager;
              

               

              You can also look in the application server console during deployment, to see if the persistence.xml was deployed as well.  To make it easier to tell what is going on, you can enable TRACE logging for the JPA deployer, by following steps described here.  You might not need this though, just try answering the above questions first.

              • 4. Re: Why is my EntityManager null in my Quartz Job on Wildfly 10?
                jseanjensen

                I have a persistance.xml and it only has one unit defined.  I've tried the unitName option too.  The peristance.xml is being deployed.

                • 5. Re: Why is my EntityManager null in my Quartz Job on Wildfly 10?
                  smarlow

                  Can you try enabling the JPA (org.jboss.as.jpa) trace output that I mentioned in my last comment.  The trace logging output will be written to the app server console (and wildfly/standalone/logs/server.log file).  Then look in the trace output that contains "pu search for name", that should appear when the JPA deployer is trying to find the persistence unit that goes with the @PersistenceContext injection, in your application.  The trace output following the "pu search for name", will indicate whether the persistence unit was found or not, which might give a clue as to what is going on.  Give that a try.

                  • 6. Re: Why is my EntityManager null in my Quartz Job on Wildfly 10?
                    brantdolling

                    Using the Java EE Scheduler should be used and it should look something like this:

                     

                     

                    import javax.ejb.Schedule;

                    import javax.ejb.Stateless;

                    import javax.persistence.EntityManager;

                    import javax.persistence.PersistenceContext;

                     

                     

                    @Stateless

                    public class Scheduler{

                     

                     

                    @PersistenceContext

                    private EntityManager entityManager;

                     

                     

                     

                    @Schedule(second = "0", minute = "1/30", hour = "*")

                    public void executeScheduler() {

                    if (entityManager == null)

                    {

                     

                        System.out.println("############## entityManager is null #######");

                    }

                     

                    else

                    {

                    System.out.println("************** WORKING ***************");

                      }

                     

                    }

                    }

                     

                    • 7. Re: Why is my EntityManager null in my Quartz Job on Wildfly 10?
                      jseanjensen

                      So I was on the right track but lacked the Schedule annotation.  I guess this means that the attribute controls the creation time and since it exists the entityManager gets injected.

                       

                      Thanks for posting your reply.