5 Replies Latest reply on Feb 7, 2013 6:36 AM by rhusar

    How to invoke a local EJB from SingletonService client?

    cmilu

      Hello,

       

      What I'm trying to achiebe is a cluster-wide singleton. I've successfuly created a Service implementing the Service<MyBusinessInterface> interface. When the Service is started, it lookups a @Singleton bean at stores the ejb locator in a field.  When I invoke a getValue() on it from my SLSB (lets name it AccesBean), I get EJB locator, and I can successfully call it. The problem is that the service works on Node1 (its currently elected on Node1) and the SLSB is on Node 2 (which has its own @Singleton bean). getValue() called from Node2 is invoked on Node1, but the ejbLocator passed to the Node2 points to the @Singleton on Node2 - I want it to point to the node, on which the lookup occured (Node1).

       

      Any ideas, how it should be done? Currently the lookup is a simple (Single is the @Singleton bean implementing the NodeNameServiceIf):

       

       

      public class NodeNameServiceBean implements Service<NodeNameServiceIf> {
      (...)
      private NodeNameServiceIf singleton;
      (...)
      @Override
      public void start(StartContext arg0) throws StartException {
      if (!started.compareAndSet(false, true)) {
       throw new StartException("The service is still started!");
       }
      try { 
      InitialContext ic = new InitialContext(p);
      singleton =  (NodeNameServiceIf) ic.lookup("ejb:sample-jboss-ear/sample-jboss-ejb-0.0.1-SNAPSHOT/Single!dNodeNameServiceIf");               
      } catch (NamingException e) {
      e.printStackTrace();
       }
      }
      
      @Override
      public NodeNameServiceIf getValue() {
      return singleton;
      }
      }
      
        • 1. Re: How to invoke a local EJB from SingletonService client?
          rhusar

          Hello Cmilu, good question -- I have created a quickstart that demonstrates how to do this. I ll get it to a presentable form and share a link.

           

          The theory goes like this: the service wrapped in singleton service looks up one @Remote @Clustered SFSB. Then the services wanting to use cluster-wide singleton get the reference from the service. If its not running on the same node, the remote EJB invocation is done.

          • 2. Re: How to invoke a local EJB from SingletonService client?
            cmilu

            Hello Radoslav,

             

            Thank you for your answer. It's great, that there will be a quickstart concerning this matter - I'm looking forward to see it. I never tought of using a SFSB instead of a singleton in this case. If I understand it correctly, every service in the cluster (even if one fails, and another will be activated on some other node) will share the session state of the SFSB? Or it only guarantees me a cluster wide lock for the SFSB methods (that only one node at time will be handling the SFSB requests), and I have to manually manage my cluster-wide data with a custom inifnispan cache?

            • 3. Re: How to invoke a local EJB from SingletonService client?
              pferraro

              I don't think that is going to work.  Everything will appear to work fine, but as soon as there is a failover of the singleton service, a new SFSB will be looked up and any state will have been lost.

              Until we properly support @Clustered @Singleton beans, you're going to have to use an separate Infinispan cache to store shared singleton state (in which case, you can just have the singleton service return the remote stub of a SLSB).

              • 4. Re: How to invoke a local EJB from SingletonService client?
                joseotavio

                We are facing a similar issue. We are migrating from JBoss 4.2.3 to 7.1.3 and previously we had services (annotated with @Service) which we have now changed to @Singleton beans, but we also need these singleton beans to work correctly on a cluster environment (only one node will have the active instance, the instance in another node will be activated if the master fails, etc).

                 

                In some cases, we are using Infinispan caches to replicate state across the nodes but that will not work well in all cases for us. So it is not currently possible to invoke methods on a cluster-wide singleton bean and be sure that it will be invoked only on the node which has the current active instance?

                • 5. Re: How to invoke a local EJB from SingletonService client?
                  rhusar

                  Guys,

                   

                  here is the POC for the cluster-wide Singleton Bean. There are things that need changing (see readme file) but might help you get started with your own implementation:

                   

                  https://github.com/rhusar/jboss-as-quickstart/tree/singleton_qs/cluster-singletonbean

                   

                  Of course, nothing beats proper support for @Clustered @Singleton beans but meanwhile...