5 Replies Latest reply on Oct 14, 2010 5:28 PM by assem

    how can i call a thread in seam that use entityManager of seam

    assem

      Hi All,
      i used :
      EntityManager entityManager = (EntityManager) Component.getInstance("entityManager"); in the run() of the thread.
      the problem is that the thread could not see the entitymanager from the contexte.
      so from a seam Component, how can i call a thread that use entityManager of seam ;
      thank You all,
      Assem





        • 1. Re: how can i call a thread in seam that use entityManager of seam
          lvdberg

          Hi,


          Why aren't you using a Asynchrous method in a Seam component, that's also running in a separate Thread and you get entityManager access for free (simply @In) ?


          Leo

          • 2. Re: how can i call a thread in seam that use entityManager of seam
            assem

            Thanks Leo,
            i tested the Asynchrous method, and i dont get multithreading






            public method1()
            ...
            for (Integer i : result1) {// list of returned results
            run(); // i have loop call to the Asynchronous method                           
            ...
            }
            
            @Asynchronous
            public run()
            {
            for(int i=0;i<500;i++)
                            {
                                    try {
                                            Thread.sleep(5);
                                            System.out.println("XXXXX"+Thread.currentThread().getName()+"XXXXX");
                                    } catch (InterruptedException e) {
                                            e.printStackTrace();
                                    }
                                    
                            }
            }






            and the Result is :




            "XXXXX Thread 1 XXXXX"
            "XXXXX Thread 1 XXXXX"
            "XXXXX Thread 1 XXXXX"
            "XXXXX Thread 1 XXXXX"
            ..
            "XXXXX Thread 2 XXXXX"
            "XXXXX Thread 2 XXXXX"
            "XXXXX Thread 2 XXXXX"
            "XXXXX Thread 2 XXXXX"
            ..
            "XXXXX Thread 3 XXXXX"
            "XXXXX Thread 3 XXXXX"
            "XXXXX Thread 3 XXXXX"
            ..






            so it works in a sequential way so no Multithreading thats why i turned to java.lang.Thread and in that case the only problem is  i cant get my EntityManager in that Thread


            Thanks,
            Assem.



            • 3. Re: how can i call a thread in seam that use entityManager of seam
              mwohlf

              like seam's in/outjection this probably doesn't work since you call the async method from the same component, try to inject the component and call the async method from a different component, there is an example in the docs:

              http://docs.jboss.org/seam/2.2.1.CR1/reference/en-US/html_single/

              • 4. Re: how can i call a thread in seam that use entityManager of seam
                lvdberg

                Hi,


                You're working with Seam and intercepted methods, so the first call method1 is intercepted. But the internal call to run NOT, meaning the Asynchronous annotation is not seen/used.


                If you want to do something Async processing, call another bean and it will be intercepted correctly. So something like:




                @Name("bean1")
                class Bean1
                
                @In Bean2 bean2;
                
                public void method(){
                
                bean2.run();
                }
                
                @Name("bean1")
                class Bean2
                
                @Asynchronous
                public void run(){
                 // Do what you want to do
                }
                
                }
                
                A much cleaner method is calling the second bean with an Event, this enables you to separate both beans, and you don´t need any injecting.
                
                Leo
                
                



                • 5. Re: how can i call a thread in seam that use entityManager of seam
                  assem

                  Thank you so Much Leo, both method worked very well, really thanks to you.
                  Have sweet day
                  Assem