13 Replies Latest reply on Dec 19, 2012 9:05 AM by rodakr

    Creating Threads inside JBoss AS 7.1.1

    nnanda

      Hi All,

       

      We are using JBoss 7.1.1 as our app server. For one of our use cases, we need to spawn threads inside the application (the application is deployed a WAR file). Since creating own threads inside a Java EE container is not an advisable idea, we are trying to evaluate different options for this like:

      - Use work manager api to spawn threads.

      - Use Executor Service to create threads.

       

      There are some confusions and I need your help in that.

       

      1. How can I use WorkManager API inside JBoss 7 container. I found several posts, but not a single one shows how exactly to do that. What all configurations we should do and what all things we need to take care inside our program.

       

      2. As per JSR 236, creating threads using ExecutorService inside a Java EE 6 certified container will borrow threads from JBoss managed pool. Is this statement correct? If so, can we use this instead of WorkManager? Essentially both of these advocates about inehriting managed threads from container instead of creating your own.

       

      Any suggestions?

       

      Thanks,

      Niranjan

        • 1. Re: Creating Threads inside JBoss AS 7.1.1
          jbertram

          As far as a "WorkManager" goes, the only such implementation available is the JCA WorkManager and you'll need to write a JCA Resource Adapter to use that one.  There was a JSR (237, I believe) for a generic WorkManager awhile back but it was withdrawn over 4 years ago and hasn't been resurrected.

           

          JSR-236 appears to be in the early draft stage so I doubt there are any implementations on any platform, much less AS7.

           

          Why do you need to spawn threads in the first place?

          • 2. Re: Creating Threads inside JBoss AS 7.1.1
            nnanda

            Hi Justin,

             

            Thanks for quick reply.

             

            Yes, you are correct; JSR 237 is already withdrawn in favor of JSR 236 and JCA WorkManager.

             

            In JBoss 5.1, we are using following code to get hold of WorkManager:

             

            final Properties properties = new Properties();
            properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
            properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
            properties.put(Context.PROVIDER_URL, "jnp://myapp.corp.apple.com:1202");
            
            final Context context = new InitialContext(properties);
            final MBeanServerConnection mconn = (MBeanServerConnection)  context.lookup("jmx/rmi/RMIAdaptor");
            final ObjectName objectName = new ObjectName("jboss.jca:service=WorkManager");
            final JBossWorkManagerMBean jwm = (JBossWorkManagerMBean)
            final MBeanServerInvocationHandler.newProxyInstance(mconn,objectName,JBossWorkManagerMBean.class,false);
            final WorkManager workmanager = jwm.getInstance();
            

             

            Can you tell me if I can use this mechanism to get hold of WorkManager in JBoss 7?

             

            Thanks,

            Niranjan

            • 3. Re: Creating Threads inside JBoss AS 7.1.1
              jbertram

              Can you tell me if I can use this mechanism to get hold of WorkManager in JBoss 7?

              As I already said, if you want to use the JCA WorkManager you will have to write a JCA Resource Adapter.  You can't use code like that anymore to get an instance of the JCA WorkManager.

               

              Furthermore, if I had to guess, most users were probably abusing the JCA WorkManager in previous versions of JBoss AS.

              • 4. Re: Creating Threads inside JBoss AS 7.1.1
                nnanda

                Hi Justin,

                 

                Can you provide some reference on how to implement JCA Resource Adapter in the context of WorkManager?

                 

                Thanks,

                Niranjan

                • 5. Re: Creating Threads inside JBoss AS 7.1.1
                  nickarls
                  • 6. Re: Creating Threads inside JBoss AS 7.1.1
                    nnanda

                    Hi Nick,

                     

                    I was going through those discussions. But there following road blocks:

                    1. JCA itself is not that easy.

                    2. There is no straight forward example, sample available.

                     

                    Anyways, all I need is to spawn some threads for my requirement. And, instead of spawning my own thread (which are unmanaged within a JavaEE container), I am trying to figure out how can I borrow some managed threads which are already created by JBoss. Thats it!!

                     

                    Can you provide some help here?

                     

                    Thanks,

                    Niranjan

                    • 7. Re: Creating Threads inside JBoss AS 7.1.1
                      nnanda

                      Hi Justin,

                       

                      Here is my use case why I need to spawn my own threads:

                       

                      1. I have a use case wherein I have to poll an Oracle AQ in a fixed scheduled time to look if a new data is available or not.

                      2. Here in my current project, there is a remote queue system (not like JMS queue, but a system which provides queue features) and I have to poll it regularly.

                       

                      I am using Spring and it is capable of scheduling tasks; but I do not want Spring to create threads. I was interested in borrowing threads from JBoss (because those are managed)!!

                       

                      Any help/suggestion for me?

                       

                      Thanks,

                      Niranjan

                      • 8. Re: Creating Threads inside JBoss AS 7.1.1
                        jaikiran

                        NIranjan Nanda wrote:

                         

                        I was interested in borrowing threads from JBoss (because those are managed)!!

                         

                        Just to understand why you are looking for JBoss managed threads - what exact "management" are you looking for, for such threads?

                        • 9. Re: Creating Threads inside JBoss AS 7.1.1
                          nickarls

                          Can't a @Scheduled @Startup @Singleton do that?

                          • 10. Re: Creating Threads inside JBoss AS 7.1.1
                            nnanda

                            Hi Jaikiran,

                             

                            I am not picky about borrowing JBoss managed threads; all I need is, I have a WAR file deployed in JBoss and for my following use cases, I need to spawn threads.

                             

                            1. I have a use case wherein I have to poll an Oracle AQ in a fixed scheduled time to look if a new data is available or not.

                            2. Here in my current project, there is a remote queue system (not like JMS queue, but a system which provides queue features) and I have to poll it regularly.

                             

                            Now, I have an understanding that I should not spawn my own threads within a Java EE container. Because my own threads are not managed by the container and hence, may cause some issues to my app server.

                             

                            Please let me know if my above understanding is correct or not.

                             

                            If I am correct, then what solution I should follow for my use cases?

                             

                            Thanks,

                            Niranjan

                            • 11. Re: Creating Threads inside JBoss AS 7.1.1
                              nnanda

                              Hi Nick,

                               

                              I am not sure on these. Any example?

                               

                              Thanks,

                              Niranjan

                              • 12. Re: Creating Threads inside JBoss AS 7.1.1
                                nickarls

                                Something like (simplified example) this should start up automagically and do work every 5 minutes.

                                 

                                @Singleton
                                @Startup
                                public class Poller
                                {
                                 
                                   // @Inject your interface to DB here 
                                
                                
                                   @PostConstruct
                                   public void init()
                                   {
                                      System.out.println("Poller started");
                                   }
                                
                                
                                   @Schedule(minute = "*/5", hour = "*", persistent = false)
                                   public void poll()
                                   {
                                       System.out.println("Polling...");
                                       // call the DB interface here
                                   }
                                }
                                
                                
                                • 13. Re: Creating Threads inside JBoss AS 7.1.1
                                  rodakr

                                  ... if you realy need to start Threads your self, you could also do it from web container... :-)