1.16. RPC Service

1.16.1. Configuration
1.16.2. Single Method Call Command

Warning

You are looking at documentation for an older release. Not what you want? See the current release documentation.

The RPCService is only needed in a cluster environment that is used to communicate with other cluster nodes. It allows executing a command on all the cluster nodes or on a coordinator, for example, the oldest node in the cluster. The RPCService has been designed to rely on JGroups capabilities and should not be used for heavy load. It can be used, for example, to notify other nodes that something happened or to collect some information from the other nodes.

The RPCService relies on 3 main interfaces:

The arguments that will be given to the RemoteCommand must be Serializable and its return type also in order to prevent any issue due to the serialization. To prevent the execution of any RemoteCommand that could be malicious and to allow non-Serializable command, you need to register the command first before using it. Since the service will keep only one instance of RemoteCommand per command Id, the implementation of the RemoteCommand must be thread safe.

To be usable, all the RemoteCommands must be registered before being used on all the cluster nodes, which means the command registration must be done in the constructor of your component. If you try to launch a command that has been registered but the RPCService is not yet launched, you will get an RPCException due to an illegal state. You will be able to execute a command only once your component has been started.

See an example below:

public class MyService implements Startable

{
   private RPCService rpcService;
   private RemoteCommand sayHelloCommand;
   
   public MyService(RPCService rpcService)
   {
      this.rpcService = rpcService;
      // Register the command before that the RPCService is started
      sayHelloCommand = rpcService.registerCommand(new RemoteCommand()
      {
         public Serializable execute(Serializable[] args) throws Throwable
         {
            System.out.println("Hello !");
            return null;
         }
         public String getId()
         {
            return "hello-world-command";
         }
      });
   }
   public void start()
   {
      // Since the RPCService is a dependency of RPCService, it will be started before
      // so I can execute my command
      try
      {
         // This will make all the nodes say "Hello !"
         rpcService.executeCommandOnAllNodes(sayHelloCommand, false);
      }
      catch (SecurityException e)
      {
         e.printStackTrace();
      }
      catch (RPCException e)
      {
         e.printStackTrace();
      }
   }
   public void stop()
   {
   }
}

In this example, we register the sayHelloCommand command in the constructor of MyService and we execute this command in the start method.

Note

We expect to have one RPCService instance per PortalContainer.

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus