6 Replies Latest reply on Jun 7, 2016 3:22 AM by chrigri

    Invoking a session bean method on each cluster node

    simplex-software

      Hello, I know that this is a very unusual case but, for some reason, I need that, when the user decides by clicking a button inthe web tier, to invoke a given method on a stateless bean on each node of the cluster. So, say that my cluster is formed by Node1 and Node2 and that the session bean MyBean is symetrically deployed on them, when the user clicks this button, I need to invoke the method m1() on the instance of MyBean() deployed on Node1 and Node2. What would be the simplest way to implement such an uncommon case ?

       

      Many thanks in advance,

       

      Nicolas

        • 1. Re: Invoking a session bean method on each cluster node
          pferraro

          As of WildFly 8, you can use the clustering API for this purpose, specifically the CommandDispatcher.  This facility allows you to execute commands on any or all nodes in the cluster.  The requisite modules are already exported to the classloader of a deployment, so long as the JGroups and Infinispan subsystems are installed.

          The complete API is here:

          wildfly/clustering/api/src/main/java/org/wildfly/clustering/dispatcher at master · wildfly/wildfly · GitHub

          There is a test in the clustering testsuite that demonstrates a simple use case:

          wildfly/testsuite/integration/clustering/src/test/java/org/jboss/as/test/clustering/cluster/dispatcher/bean at master · …

           

          In your case, you would use your @Local @Stateless EJB as the command context.

          • 2. Re: Invoking a session bean method on each cluster node
            simplex-software

            Hi Paul,

             

            Many thanks for your reply. I need something that would work for EAP 6.2 and that is documented, such that I could see how to use it. As far as I have seen, the GIT repository to which you've sent links to me only contains the source code. I have googled around but didn't find any comprehensible documentation. Do you have anything which could help ? By the way, I'd be using @Stateless.

             

            Many thanks in advance,

             

            Nicolas

            • 3. Re: Re: Invoking a session bean method on each cluster node
              pferraro

              I see.  The code to which I directed you was first introduced in WildFly 8, and thus doesn't exist in EAP 6.2.  This is a WildFly forum, hence my assumptions...

              You can do something similar in EAP 6, but it would require private modules (i.e. org.jboss.as.clustering.api and org.jboss.as.server) and would leverage the more primitive GroupRpcDispatcher.  Because these modules are private, they are undocumented/unsupported apart from the source/javadoc itself.  You should be able to do something like:

              e.g.

              ServiceRegistry registry = org.jboss.as.server.CurrentServiceContainer.getServiceContainer();  // Easiest way to access the MSC service registry
              org.jboss.as.clustering.GroupRpcDispatcher dispatcher = (org.jboss.as.clustering.GroupRpcDispatcher) registry.getService(ServiceName.JBOSS.append("cluster", "ejb")).getValue(); // Gets the group rpc dispatcher that uses the JGroups channel for EJBs.
              dispatcher.registerRPCHandler("foo", mySLSB);  // Registers your @Stateless EJB as handler for remote rpcs
              
              

               

              You can then dispatch RPCs to your @Stateless EJBs on each node via:

               

              Object[] args = ...;
              Class[] types = ...;
              boolean excludeSelf = ...;
              dispatcher.callMethodOnCluster("foo", "methodName", args, types, excludeSelf);
              
              

               

              The dispatcher will send the method signature and its arguments to each node, where that method on your SLSB (local to each node) will be invoked via reflection.

              • 4. Re: Re: Invoking a session bean method on each cluster node
                simplex-software

                Hi Paul,

                 

                Many thanks again for your reply. Sorry about the confusion. Yes, I know it's a Wildfly site but I thought that EAP and Wildfly have a common code base and we get more pro-active and faster results from this site then from the RedHat customer support, who rejects most of tickets as "unsupported".

                 

                This is a very interesting and elegant solution. I'll definetly giez it a try. Just for the records, the statement

                 

                org.jboss.as.clustering.GroupRpcDispatcher dispatcher = (org.jboss.as.clustering.GroupRpcDispatcher) registry.getService(ServiceName.JBOSS.append("cluster", "ejb").getValue(); // Gets the group rpc dispatcher that uses the JGroups channel for EJBs. 


                should be:


                org.jboss.as.clustering.GroupRpcDispatcher dispatcher = (org.jboss.as.clustering.GroupRpcDispatcher) registry.getService(ServiceName.JBOSS.append("cluster", "ejb")).getValue(); // Gets the group rpc dispatcher that uses the JGroups channel for EJBs. 


                right ?


                Many thanks for your help.

                 

                Nicolas

                • 5. Re: Re: Invoking a session bean method on each cluster node
                  pferraro

                  Correct.  I just edited that post.

                  • 6. Re: Invoking a session bean method on each cluster node
                    chrigri

                    Hi Paul,

                     

                    Do you have any examples of how to do:

                    "In your case, you would use your @Local @Stateless EJB as the command context."

                     

                    I'm having problem calling my SessionBeans from within the execute() method of an Command class.

                    I get Not Allowed execptions.

                     

                    --

                    Christer