10 Replies Latest reply on Dec 9, 2010 8:44 AM by Alessandro Novarini

    Clustering an application that uses Spring singletons

    Alessandro Novarini Newbie

      Hello all,

       

      As the title says, I need to make an application deployed on a JBoss 5.1 ready for a cluster environment.

      We're facing a problem with some stateless ejb, because they have actually a "state", a field that works like an internal cache for some data.

       

      This field is injected by Spring, and it's configured as a singleton, so that every stateless bean instantiated keep the reference to the same object.

      Now developers asked me advices about clustering this bean, so that that cache will be shared between all the ejbs on all nodes.

       

      How would you solve this problem?

      Making the state a stateful bean doesn't obviously work, and we don't like to put this cache on db because we have caching enabled for entity beans.

       

      Thanks in advance

      Ale

        • 1. Re: Clustering an application that uses Spring singletons
          Wolf-Dieter Fink Master

          What EJB version do you use 2.x or 3.x? It will help for an answer.

           

          Best try is to avoid such 'stateful' behaviour ;-(

          For our application (do not use spring) we found out that it will work without some caching more stable and without a performance drawback.

          We have entities with a high change frequency and the data in cache are ruled out and must be loaded anyhow.

           

          Solutions for you might be

          - JBoss(Tree)Cache, replicate the state in the cluster (time gap possible because of async replication after commit)

          - Special entity without caching or with the invalidation feature (EJB2 entities)

          - your own implementation and a signal if it must be refreshed (we do this for master data via JMS)

           

          Also you should think about the load-balancing, it is not ensured that two calls of SLSB in a sequence will reach the same JBoss instance in a cluster, this might produce problems if the cache-data will be not the same ...

          • 2. Re: Clustering an application that uses Spring singletons
            Alessandro Novarini Newbie

            I'm really sorry, I forgot to say that we're using EJB3 and annotations.

             

            Our cluster will be load balanced, and one of the problems related to this sort of things is what you mentioned.

            About your solutions I must say that about the cache we're thinking about it and there's another open discussion on this forum for another problem we're having;

            The second one has been also considered but we're keeping it as last resort.

             

            Thanks a lot

            Ale

            • 3. Re: Clustering an application that uses Spring singletons
              Wolf-Dieter Fink Master

              With EJB3 entities there is only a transactional caching (no 2nd level cache like EJB2).

              Are you sure that the caching will spare significant duration or cpu time ?

              A good principle will be 'only think about cache if you measure that it will bring benefit'

              • 4. Re: Clustering an application that uses Spring singletons
                Alessandro Novarini Newbie

                Without going deep in details, the field (an hashmap) of the stateless ejb contains data fetched from rss, forecasts and other data taken from internet that are loaded once a day.

                 

                Clients can't connect directly to internet, and this kind of data is not storable into the application database.

                 

                Loading and collecting information from these sources would be very intensive if we didn't cache in some way; that's the reason for this cache.

                 

                Thanks

                Ale

                • 5. Re: Clustering an application that uses Spring singletons
                  Wolf-Dieter Fink Master

                  Ok,

                  I understand the reason for the cache (was just food for thought)

                   

                  But another one ...

                  If this data is loaded only once a day it might aceptable to load local on each cluster node at the same time,

                  because if the time is in sync the difference is only a few seconds ...

                  • 6. Re: Clustering an application that uses Spring singletons
                    Alessandro Novarini Newbie

                    Yes, probably we could in some way.

                     

                    Googling we found this link, maybe a bit old but we would like to give it a chance anyway:

                    http://javabeans.asia/2008/05/16/jboss_clustering_shared_state_across_cluster_partition.html

                     

                    So now we're thinking about having our "cache" in a hasingleton, every ejb pointing to it, and when a node goes down, the shared state will grant that no data is lost.

                     

                    What do you think?

                     

                    Thanks

                    Ale

                    • 7. Re: Clustering an application that uses Spring singletons
                      Wolf-Dieter Fink Master

                      Also a solution.

                      AFAIK the HA-singleton will start only once in a cluster (like JMS JBossMQ). In case of node crash there will be a gap al long as one other instance provide the service.

                      • 8. Re: Clustering an application that uses Spring singletons
                        Alessandro Novarini Newbie

                        Just to tell you how the developers decided to solve this "issue".

                         

                        Basically, the injected singleton became a service, so singleton on every node, with a dependency to the HASingletonDeployer, just like the following:

                         

                        {code}@Service(objectName="jboss.myApp:service=myService")

                        @Depends("jboss.ha:service=HASingletonDeployer,type=Barrier")

                        {code}

                         

                        In this way the bean will be deployed just once for all the cluster instances.

                        From now on, for referencing your bean, you can use the Depends annotation on the field:

                         

                        {code}@Depends("jboss.myApp:service=myService")

                        private MyService myService;

                        {code}

                         

                        In our scenario, since the data are loaded once in a day and put into the cache, we're fine even if we experience same gap before the service starts again on another node.

                         

                        Thanks

                        Ale

                        • 9. Re: Clustering an application that uses Spring singletons
                          Ghazanfer Ali Newbie

                          I have tried your solution its works for the first node of my jboss 5.1 cluster, but when i start my 2nd node it does not deploy my ear and throws following error:--

                           

                          DEPLOYMENTS MISSING DEPENDENCIES:
                            Deployment "jboss.j2ee:ear=vtp.ear,jar=vtpEnterpriseBeans.jar,name=JobService,service=EJB3" is missing the following dependencies:
                              Dependency "<UNKNOWN jboss.j2ee:ear=vtp.ear,jar=vtpEnterpriseBeans.jar,name=JobService,service=EJB3>" (should be in state "Described", but is actually in st
                          ate "** UNRESOLVED Demands 'jboss.ha:service=HASingletonDeployer,type=Barrier' **")
                            Deployment "jboss.j2ee:ear=vtp.ear,jar=vtpEnterpriseBeans.jar,name=JobService,service=EJB3_endpoint" is missing the following dependencies:
                              Dependency "jboss.j2ee:ear=vtp.ear,jar=vtpEnterpriseBeans.jar,name=JobService,service=EJB3" (should be in state "Configured", but is actually in state "PreI
                          nstall")

                           

                          DEPLOYMENTS IN ERROR:
                            Deployment "<UNKNOWN jboss.j2ee:ear=vtp.ear,jar=vtpEnterpriseBeans.jar,name=JobService,service=EJB3>" is in error due to the following reason(s): ** UNRESOLVE
                          D Demands 'jboss.ha:service=HASingletonDeployer,type=Barrier' **

                           

                          can u please help with this?

                          ghazanfer

                          • 10. Re: Clustering an application that uses Spring singletons
                            Alessandro Novarini Newbie

                            I suspect this is related to an issue already registered in Jira (https://issues.jboss.org/browse/JBAS-7096).

                             

                            Anyway, that looks like the one I got, but the whole thing works fine once you stop the active node. After that, you should see in the log of the new active node the registration of the ejb.

                             

                            Please let me know

                             

                            Thanks

                            Ale

                             

                            Message was edited by: Alessandro Novarini, added link to Jira issue