13 Replies Latest reply on Apr 16, 2009 12:28 AM by swd847

    Seam and asynchronous threading

      I'm using JBoss 4.2.2GA and Spring/Seam 2.0.2.  I'd like to control the number of threads Seam uses to handle asynchronous requests from JMS.  The requests are being handled by a consumer service which creates events that are handled by these threads as ScheduledFutureTasks.


      The reason I'd like to do this is because I cannot control the number of database connections (for non-technical reasons).  The problem is that if I have 10 threads and 10 database connections, if all of the threads are busy and one needs to commit, another database connection is required to generate IDs.  This causes things to deadlock until a timeout occurs.


      If there is a way to control the number of threads that seems like the best option.  If I can do it by not requiring a new connection for the transaction or some other way, I'm open to ideas there.


      Thanks!


      Ken

        • 1. Re: Seam and asynchronous threading

          Sorry for double posting, but I neglected to mention that the maximum size of the thread pool in which these threads are being created defaults to 10.  I've changed the JBoss settings (jboss-service.xml) and the settings in quartz.properties to see if that would help, but it did not have any effect on this particular thread pool.


          Thanks again,


          Ken

          • 2. Re: Seam and asynchronous threading
            hcgpragt
            • 3. Re: Seam and asynchronous threading

              Hi Hugo,


              Thanks for the suggestion.  FWIW this is in EJB3.  I will try the annotation angle, as perhaps I've missed something there.


              I can see that the single thread that runs the DefaultMessageListenerContainer.  When it receives a message, it calls something that causes what I'll call a handler thread to be created.  It will create up to 10 of these as subsequent requests arrive.  I have found no configuration for this, so I fear it is a default setting buried somewhere in the bowels of the code (perhaps in the concurrency library itself).  What I don't know is how to get a hook into it to change it or if that is even possible.


              Thanks again,


              Ken

              • 4. Re: Seam and asynchronous threading
                gonorrhea

                don't forget jboss.xml:


                http://docs.jboss.org/ejb3/app-server/reference/build/reference/en/html/jbossdeploymentdescriptor.html


                from JBoss in Action:



                JBoss provides default behavior for most things, keeping vendor specific configuration to a minimum. But for the few things that need to be configured, JBoss provides a proprietary deployment descriptor called jboss.xml.
                • 5. Re: Seam and asynchronous threading

                  Let me ask this a little more intelligently now that I've dug into the Seam Framework code a bit and see where we get.  From the looks of things, Seam is using its

                  org.jboss.seam.async.ThreadPoolDispatcher

                  class to create the listener threads I mentioned earlier.  Note the threadPoolSize is hard-wired to 10.  This is what I want to configure.  That code is as follows:


                  public class ThreadPoolDispatcher extends AbstractDispatcher<Future, TimerSchedule>
                  {
                     private int threadPoolSize = 10; 
                     
                     private ScheduledExecutorService executor = Executors.newScheduledThreadPool(threadPoolSize);
                  ...
                  
                     public void setThreadPoolSize(int threadPoolSize)
                     {
                        this.threadPoolSize = threadPoolSize;
                     }
                  }
                  



                  Other than changing the Seam code and rebuilding, is there a way to configure this value?  As you can see, this class does have a

                  setThreadPoolSize

                  method, but how do I get a hook into Seam somewhere to set that value in my application for the pool that Seam is using to handle asynchronous events?


                  Thanks!

                  • 6. Re: Seam and asynchronous threading
                    kapitanpetko

                    Haven't tried this but


                    <async:thread-pool-dispatcher thread-pool-size="20" />



                    in components.xml should work.

                    • 7. Re: Seam and asynchronous threading

                      I must admit, this was the most promising suggestion I've seen, but it had no noticable effect.  I can see in the logs that it did pick up the change:


                      '
                      2009-04-15 15:02:40,541 DEBUG [org.jboss.seam.Component] org.jboss.seam.async.dispatcher.threadPoolSize=5
                      '


                      However, when the pool is created it grows to 10 threads, adding a thread at a time per asynchronous event.  Looking at the http://www.jboss.com/products/seam/async-2.0.xsd, this appears to be the right object to configure, but I'm guessing some code changes have to be made to limit the maximum size of the pool and make it configurable.  Given that the previously posted ThreadPoolDispatcher code has it hard-wired, I don't see a way around this with the current version.

                      • 8. Re: Seam and asynchronous threading
                        swd847

                        Is using quartz an option? If so you can control the number of threads using seam.quartz.properties.

                        • 9. Re: Seam and asynchronous threading

                          I've been able to successfully modify the maximum number of threads Quartz uses in the past, so I will see how feasible this is to the project higher ups (I just got on the project) to use Quartz instead.  I think the preference will be to stick with what is already in place and get it to work.  It will come down to how important it is to be able to configure the maximum size of the async thread pool.


                          Great suggestion, thanks!

                          • 10. Re: Seam and asynchronous threading
                            swd847

                            The reason why it is not working is as follows:


                            From ThreadPoolDispatcher:


                            private int threadPoolSize = 10; 
                               
                            private ScheduledExecutorService executor = Executors.newScheduledThreadPool(threadPoolSize);
                            



                            What this means is that the Executor is created when the object is initilised, and before seam has has a chance to change the default value, I will file a JIRA and attach a patch.

                            • 11. Re: Seam and asynchronous threading
                              swd847
                              • 12. Re: Seam and asynchronous threading

                                That's awesome. Thanks Stuart, that confirms what I was thinking...now I don't feel quite so dumb.  :)

                                • 13. Re: Seam and asynchronous threading
                                  swd847

                                  For now if you need an interim solution you can copy/paste the code for ThreadPoolDispatcher, remove the @Install annotation and set threadPoolSize to whatever you want.