6 Replies Latest reply on Jun 22, 2011 9:50 PM by Benniu Ji

    How to persist an object in Asynchronous method in a cron job

    Benniu Ji Newbie

      I tried to persist an object in an Asynchronous method, but it failed.




      public void schedule(@Observes @Every(nth=10, value=Interval.SECOND) Trigger t){
                
          for (TestRunner runner: pendingRunners){
           
           Executor e = new Executor();
           e.run(runner);
           }
          }
      }





      @Transactional @Dependent
      public class Executor implements Serializable {
           
           /**
            * 
            */
           private static final long serialVersionUID = 5413892488805663161L;
           
           @PersistenceContext
           EntityManager em;
           
           
           
           @Asynchronous
           public void run(TestRunner runner){
           
                try {
                     
                     TestAgent agent = runner.getTestAgent();
                     
                     if (agent!=null){
                          
                                      runner.setStartTime(new Date());
                          em.merge(runner);
                                em.flush();
                          
                          Thread.sleep(30000);
                          //TO-DO
                          //Call STAF lib to run test script
                          
                                      runner.setStopTime(new Date());
                          em.merge(runner);
                                em.merge(agent);
                                em.flush();
                          
                     
                     }
                } catch (Exception e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }
      }




      After running the cron job, I got following error:



      17:28:06,279 ERROR [STDERR] java.lang.NullPointerException




      It seems entitymanager is null.
      Can anyone help on this?