7 Replies Latest reply on Jan 4, 2010 3:20 AM by kapitanpetko

    back group threads without user interactions

      I have some back group threads to run when application starts up. These threads are moving from old code and are doing a lot of works without user interactions. So, I think there is no seam event here. How can I use seam context, especially entity manager?
        Thanks in advance!


      Below is my sample code.


      public class MyThread extends Thread {
           @Override
           public void run() {
           //TODO:
           //there is not seam context here. how can I use entity manager here ? 
           
           for(;;){
                //do routine job .....
           }
           
           }
      }
      
      
      @Scope(ScopeType.APPLICATION)
      @Name("backgroupPool")
      @Startup
      public class BackgroupPool extends Thread {
      
      /*
           EntityManagerFactory entityManagerFactory;
           public EntityManagerFactory getEntityManagerFactory() {
                org.jboss.seam.contexts.Context applicationContext = (org.jboss.seam.contexts.Context) Contexts
                          .getApplicationContext();
                if (applicationContext != null) {
                     org.jboss.seam.persistence.EntityManagerFactory entityManagerFactory3 = (org.jboss.seam.persistence.EntityManagerFactory) applicationContext
                               .get("dbTestEntityManagerFactory");
                     if (entityManagerFactory3 != null) {
      
                          entityManagerFactory = entityManagerFactory3
                                    .getEntityManagerFactory();
      
                     }
                }
      
           }
      */     
           @Override
           public void run() {
      
                for (int i = 0; i < 10; i++) {
                     MyThread thread = new MyThread();
                     thread.start();
                }
      
           }
           
           @Observer("org.jboss.seam.postInitialization")
           public void init() {
                //getEntityManagerFactory
                this.startup();
      
      
           }                                   
      
      




        • 1. Re: back group threads without user interactions
          germanescobar

          Use Lifecycle.beginCall() at the start of each thread and Lifecycle.endCall() at the end. Then you can have access to the contexts.


          Hope it helps!

          • 2. Re: back group threads without user interactions
            kapitanpetko

            German Escobar wrote on Dec 21, 2009 03:39:


            Use Lifecycle.beginCall() at the start of each thread and Lifecycle.endCall() at the end. Then you can have access to the contexts.



            That would probably work, but you should consider using asynchronous methods instead of 'raw' threads. That gives you thread pooling and management for free. It is also integrated with Seam, so you get access to contexts (application and event only)..

            • 3. Re: back group threads without user interactions

              Thanks for replying, MyThread must be Seam componet if using Lifecycle.beginCall(). But MyThread is dynamice created, and have many instance. I think this method does not work.

              • 4. Re: back group threads without user interactions

                Thanks for replying. But MyThread is dynamice created, and have many instances. Can you give me any examples?
                  Backgroup thread is widely used. And many should be integrated into seam. Can some one give me any such example.



                Nikolay Elenkov wrote on Dec 21, 2009 07:22:



                German Escobar wrote on Dec 21, 2009 03:39:


                Use Lifecycle.beginCall() at the start of each thread and Lifecycle.endCall() at the end. Then you can have access to the contexts.



                That would probably work, but you should consider using asynchronous methods instead of 'raw' threads. That gives you thread pooling and management for free. It is also integrated with Seam, so you get access to contexts (application and event only)..



                Click HELP for text formatting instructions. Then edit this text and check the preview.

                • 5. Re: back group threads without user interactions
                  kapitanpetko

                  tiger zhao wrote on Dec 23, 2009 03:56:


                  Thanks for replying. But MyThread is dynamice created, and have many instances. Can you give me any examples?
                    Backgroup thread is widely used. And many should be integrated into seam. Can some one give me any such example.



                  Read about it here: Asynchronicity

                  • 6. Re: back group threads without user interactions

                    Nikolay Elenkov wrote on Dec 24, 2009 03:36:



                    tiger zhao wrote on Dec 23, 2009 03:56:


                    Thanks for replying. But MyThread is dynamice created, and have many instances. Can you give me any examples?
                      Backgroup thread is widely used. And many should be integrated into seam. Can some one give me any such example.



                    Read about it here: Asynchronicity


                    As to a single thread, it works. But There are many same thread. I have not seen such examples before. Really need help!

                    • 7. Re: back group threads without user interactions
                      kapitanpetko

                      If you call an asynchronous method multiple times that will create multiple background threads.