5 Replies Latest reply on Apr 28, 2011 6:08 AM by zaquas

    EntitiManager and Observer("org.jboss.seam.postInitialization")

    marx3

      How to get entityManager in class which is run via
      @Observer(org.jboss.seam.postInitialization) ?
      I've tried


      @PersistenceContext
      private EntityManager em;
      


      but it doesn't work.


      I've tried


      @In private EntityManager entityManager;



      and


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



      but I have exception



      15:22:54,745 INFO  [Contexts] starting up: org.jboss.seam.security.facesSecurityEvents
      15:26:19,560 ERROR [[/smo]] Exception sending context initialized event to listener instance of class org.jboss.seam.servlet.SeamListener
      java.lang.IllegalArgumentException: EntityManagerFactory not found in JNDI : java:/>xxxEntityManagerFactory
              at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManagerFactoryFromJndiOrValueBinding(ManagedPersistenceContext.java:247)



      Components.xml and persistence.xml are seam-genned and seems  ok.


      Is it because of Seam not being completely initialised?

        • 1. Re: EntitiManager and Observer("org.jboss.seam.postInitialization")
          marx3

          Accordingly to this
          link
          injecting EntityManager should work, but it doesn't.
          Any help?

          • 2. Re: EntitiManager and Observer("org.jboss.seam.postInitialization")
            ignaciolarranaga.ignaciolarranaga.gmail.com

            I have the same error.


            May be this can help: http://relation.to/2640.lace

            • 3. Re: EntitiManager and Observer("org.jboss.seam.postInitialization")
              kyatim

              Don't forget put @Name annotation
              (it was may problem but now all work fine)

              • 4. Re: EntitiManager and Observer("org.jboss.seam.postInitialization")
                zaquas

                It appears not work with jboss as 6.0 final.


                I've try with this code:



                @Name(value="applicationstartup")
                public class ApplicationStartup {
                     
                     @In
                     EntityManager entityManager;
                     
                     @Observer(value="org.jboss.seam.postInitialization")
                     public  void startup(){
                          // my business code stay here...
                     }
                     
                }



                But I've only read in the console:


                Exception sending context initialized event to listener instance of class org.jboss.seam.servlet.SeamListener:
                java.lang.IllegalArgumentException: EntityManagerFactory not found in JNDI : java:/poplaEntityManagerFactory



                Have you some ideas?
                Thanks in advance
                Stefano






                • 5. Re: EntitiManager and Observer("org.jboss.seam.postInitialization")
                  zaquas

                  Hello,
                  I've investigated all this morning and I found that the same code work well with jboss as 5.1.


                  After that, I tried as suggest Pete in this post as here:



                  @Name(value = "applicationstartup")
                  @Transactional
                  public class ApplicationStartup {
                  
                       @In
                       AsynchronousProcessor asynchronousProcessor;
                  
                       @Observer(value = "org.jboss.seam.postInitialization")
                       public void startup() {
                  
                            System.out.println("wating for...");
                            asynchronousProcessor.runAction(30 * 1000l);
                  
                       }
                  
                  }
                  




                  @Name("asynchronousProcessor")
                  @AutoCreate
                  public class AsynchronousProcessor {
                  
                       @In EntityManager entityManager;
                       
                       @Asynchronous
                       public void runAction(@Duration long duration) {          
                            
                            System.out.println(entityManager.toString());               
                            
                       }
                  
                  }



                  And in this manner works! But, for some strange thing, if I put time at few millisecond, not work again...
                  Strange things!!!