5 Replies Latest reply on Jan 28, 2013 6:42 PM by trungsi

    Multi-threading -- using the Java Callable interface with Seam

    jfalek
      We have a Seam component which contains and another Seam component (using @In (required=true) ).  We decided to break apart the activities in one of the methods in the first component into four concurrent threads to improve performance using the Callable/Future/ExecutorService framework of Java 1.5.

      We noticed that the "inner" Seam component was not always being created by Seam and therefore, we were getting null pointer errors (the code assumes that Seam creates all @In components).  We've been told that each thread causes a new Seam context to be created and therefore we would not be able to simply create a new Callables and calling get() on their associated Future objects. 

      Has anyone come across this problem of making a Seam component multi-threaded and are there any recommendations or examples on how to implement Callable(s) on a Seam component?

      Thank you,
        James
        • 1. Re: Multi-threading -- using the Java Callable interface with Seam
          pmuir

          Use Seam's built in async support

          • 2. Re: Multi-threading -- using the Java Callable interface with Seam
            jfalek

            I've been reading up on the @Asynchronous and Quartz but I haven't come across any examples where I could start two (or more) threads and wait for them to finish and then receive their results (like I can using the Callable/Future/ExecutorService framework. 


            Do you know of an example or could you supply one --- or just give me some pointers in that direction?


            Thank you,
               James

            • 3. Re: Multi-threading -- using the Java Callable interface with Seam
              hurzeler

              Have you found a solution to your multi threading issue using @Asynchronous?

              • 4. Re: Multi-threading -- using the Java Callable interface with Seam
                amiliosanchez

                This problem still exists, you should be able to somehow maintain the various contexts across threads in someway but seeing how they are threadlocal what is the best approach if any?

                • 5. Re: Multi-threading -- using the Java Callable interface with Seam
                  trungsi

                  Hello,

                   

                  I'd like resurrect this thread. Because I can't find anything about Seam and multi-threading. I have to divide a processing to several chunks in order to process them in parallel. The results are aggregated then returned to the client. So the async method does not help. I thought of using ExecutorService/Callable/Future to get things done but had threading issues with EntityManager. Here's the code :

                   

                  @Scope(STATELESS)

                  public class TheService {

                      public List<Result> process(List<Param> params)  {

                         for (Param p : params) {

                            executorService.submit(new Callable<List<Result>>() {

                               public List<Result> call() {

                                 return internalProcess(p);

                              }

                           });

                        }

                     }

                   

                     private List<Result> internalProcess(Param p) {

                         // process using EntityManager used indirectly by other components injected into TheService instance

                     }

                  }

                   

                  Do you know if it's possible to use plain threads mechanism within Seam application? If yes, how to do it correctly? If not, is there other workarounds or alternatives?

                  I'm also interrested if you have any pointer to the subject.

                   

                  Thanks for your help.