eXo Kernel :: Component :: Common Service 2.5.0-Alpha2

org.exoplatform.services.rpc.impl
Class AbstractRPCService

java.lang.Object
  extended by org.exoplatform.services.rpc.impl.AbstractRPCService
All Implemented Interfaces:
RPCService, org.jgroups.blocks.RequestHandler, org.jgroups.MembershipListener, Startable
Direct Known Subclasses:
RPCServiceImpl

public abstract class AbstractRPCService
extends Object
implements RPCService, Startable, org.jgroups.blocks.RequestHandler, org.jgroups.MembershipListener

This class is a basic implementation of the RPCService, it is mainly based on the MessageDispatcher of JGroups. This implementation is not designed to give the best possible performances, it only aims to give a way to communicate with other nodes.

Version:
$Id$
Author:
Nicolas Filotto

Nested Class Summary
static class AbstractRPCService.MemberHasLeftException
           
static class AbstractRPCService.MessageBody
          This intern class will be used to
static class AbstractRPCService.State
          All the potential states of the RPCServiceImpl
 
Field Summary
protected  org.jgroups.Channel channel
          The JGroups Channel used to communicate with other nodes
protected static String CLUSTER_NAME
          The default value of the cluster name
protected  org.jgroups.conf.ProtocolStackConfigurator configurator
          The configurator used to create the JGroups Channel
protected  org.jgroups.Address coordinator
          The address of the current coordinator
protected static int DEFAULT_RETRY_TIMEOUT
          The value of the default retry timeout
protected static int DEFAULT_TIMEOUT
          The value of the default timeout
protected  org.jgroups.blocks.MessageDispatcher dispatcher
          The dispatcher used to launch the command of the cluster nodes
protected  boolean isCoordinator
          Indicates whether the current node is the coordinator of the cluster or not
protected  List<org.jgroups.Address> members
          The current list of all the members of the cluster
protected static String PARAM_ALLOW_FAILOVER
          The name of the parameter to allow the failover
protected static String PARAM_CLUSTER_NAME
          The name of the parameter for the name of the cluster.
protected static String PARAM_DEFAULT_TIMEOUT
          The name of the parameter for the default timeout
protected static String PARAM_JGROUPS_CONFIG
          The name of the parameter for the location of the JGroups configuration.
protected static String PARAM_RETRY_TIMEOUT
          The name of the parameter for the retry timeout
 
Fields inherited from interface org.exoplatform.services.rpc.RPCService
ACCESS_RPC_SERVICE_PERMISSION
 
Constructor Summary
AbstractRPCService(ExoContainerContext ctx, InitParams params, ConfigurationManager configManager)
          The public constructor
 
Method Summary
 void block()
          
protected abstract  org.jgroups.util.RspList castMessage(List<org.jgroups.Address> dests, org.jgroups.Message msg, boolean synchronous, long timeout)
          Cast a message to all the given members
protected abstract  org.jgroups.Channel createChannel()
          Create a channel
protected  List<Object> excecuteCommand(List<org.jgroups.Address> dests, RemoteCommand command, boolean synchronous, long timeout, Serializable... args)
          Execute the command on all the nodes corresponding to the list of destinations.
 List<Object> executeCommandOnAllNodes(RemoteCommand command, boolean synchronous, Serializable... args)
          Executes a command on all the cluster nodes.
 List<Object> executeCommandOnAllNodes(RemoteCommand command, long timeout, Serializable... args)
          Executes a command synchronously on all the cluster nodes.
protected  List<Object> executeCommandOnAllNodesMain(RemoteCommand command, boolean synchronous, long timeout, Serializable... args)
          Executes a command on all the cluster nodes.
 Object executeCommandOnCoordinator(RemoteCommand command, boolean synchronous, Serializable... args)
          Executes a command on the coordinator only.
 Object executeCommandOnCoordinator(RemoteCommand command, long timeout, Serializable... args)
          Executes a command synchronously on the coordinator only.
protected  Object executeCommandOnCoordinatorMain(RemoteCommand command, boolean synchronous, long timeout, Serializable... args)
          Executes a command on the coordinator only.
protected  String getClusterName()
          Gives the name of the cluster
protected  RemoteCommand getCommand(String commandId)
          Gives the RemoteCommand corresponding to the given id
protected  long getDefaultTimeout()
          Gives the value of the default timeout
protected abstract  org.jgroups.Address getLocalAddress()
          Returns the channel's own address.
protected abstract  List<org.jgroups.Address> getMembers(org.jgroups.View view)
          Returns a reference to the List of members (ordered) Do NOT change this list, hence your will invalidate the view Make a copy if you have to modify it.
protected  long getRetryTimeout()
          Gives the value of the retry timeout
 Object handle(org.jgroups.Message msg)
          
protected  boolean isAllowFailover()
          Indicates whether the failover capabilities are enabled or not
 boolean isCoordinator()
          Indicates whether the local node is the coordinator of the cluster
 RemoteCommand registerCommand(RemoteCommand command)
          Register a new RemoteCommand instance, it will be mapped to its id.
 void registerTopologyChangeListener(TopologyChangeListener listener)
          Register a new TopologyChangeListener
protected abstract  void setObject(org.jgroups.Message m, Object o)
          Takes an object and uses Java serialization to generate the byte[] buffer which is set in the message.
 void start()
          
 void stop()
          
 void suspect(org.jgroups.Address suspectedMbr)
          
 void unregisterCommand(RemoteCommand command)
          Unregister a RemoteCommand instance, if the id is known or the instance itself is known otherwise it will be ignored
 void unregisterTopologyChangeListener(TopologyChangeListener listener)
          Unregister a TopologyChangeListener if it exists
 void viewAccepted(org.jgroups.View view)
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PARAM_JGROUPS_CONFIG

protected static final String PARAM_JGROUPS_CONFIG
The name of the parameter for the location of the JGroups configuration.

See Also:
Constant Field Values

PARAM_CLUSTER_NAME

protected static final String PARAM_CLUSTER_NAME
The name of the parameter for the name of the cluster.

See Also:
Constant Field Values

PARAM_DEFAULT_TIMEOUT

protected static final String PARAM_DEFAULT_TIMEOUT
The name of the parameter for the default timeout

See Also:
Constant Field Values

PARAM_ALLOW_FAILOVER

protected static final String PARAM_ALLOW_FAILOVER
The name of the parameter to allow the failover

See Also:
Constant Field Values

PARAM_RETRY_TIMEOUT

protected static final String PARAM_RETRY_TIMEOUT
The name of the parameter for the retry timeout

See Also:
Constant Field Values

DEFAULT_TIMEOUT

protected static final int DEFAULT_TIMEOUT
The value of the default timeout

See Also:
Constant Field Values

DEFAULT_RETRY_TIMEOUT

protected static final int DEFAULT_RETRY_TIMEOUT
The value of the default retry timeout

See Also:
Constant Field Values

CLUSTER_NAME

protected static final String CLUSTER_NAME
The default value of the cluster name

See Also:
Constant Field Values

configurator

protected final org.jgroups.conf.ProtocolStackConfigurator configurator
The configurator used to create the JGroups Channel


channel

protected org.jgroups.Channel channel
The JGroups Channel used to communicate with other nodes


members

protected volatile List<org.jgroups.Address> members
The current list of all the members of the cluster


coordinator

protected volatile org.jgroups.Address coordinator
The address of the current coordinator


isCoordinator

protected volatile boolean isCoordinator
Indicates whether the current node is the coordinator of the cluster or not


dispatcher

protected org.jgroups.blocks.MessageDispatcher dispatcher
The dispatcher used to launch the command of the cluster nodes

Constructor Detail

AbstractRPCService

public AbstractRPCService(ExoContainerContext ctx,
                          InitParams params,
                          ConfigurationManager configManager)
The public constructor

Parameters:
ctx - the ExoContainerContext from which we will extract the corresponding ExoContainer
params - the list of initial parameters
configManager - the configuration manager used to get the configuration of JGroups
Method Detail

executeCommandOnAllNodes

public List<Object> executeCommandOnAllNodes(RemoteCommand command,
                                             boolean synchronous,
                                             Serializable... args)
                                      throws RPCException
Executes a command on all the cluster nodes. This method is equivalent to the other method of the same type but with the default timeout. The command must be registered first otherwise an RPCException will be thrown.

Specified by:
executeCommandOnAllNodes in interface RPCService
Parameters:
command - The command to execute on each cluster node
synchronous - if true, sets group request mode to GroupRequest.GET_ALL, and if false sets it to GroupRequest.GET_NONE.
args - an array of Serializable objects corresponding to parameters of the command to execute remotely
Returns:
a list of responses from all the members of the cluster. If we met an exception on a given node, the RPCException will be the corresponding response of this particular node
Throws:
RPCException - in the event of problems.

executeCommandOnAllNodes

public List<Object> executeCommandOnAllNodes(RemoteCommand command,
                                             long timeout,
                                             Serializable... args)
                                      throws RPCException
Executes a command synchronously on all the cluster nodes. The command must be registered first otherwise an RPCException will be thrown.

Specified by:
executeCommandOnAllNodes in interface RPCService
Parameters:
command - The command to execute on each cluster node
timeout - a timeout after which to throw a replication exception.
args - an array of Serializable objects corresponding to parameters of the command to execute remotely
Returns:
a list of responses from all the members of the cluster. If we met an exception on a given node, the RPCException will be the corresponding response of this particular node
Throws:
RPCException - in the event of problems.

executeCommandOnAllNodesMain

protected List<Object> executeCommandOnAllNodesMain(RemoteCommand command,
                                                    boolean synchronous,
                                                    long timeout,
                                                    Serializable... args)
                                             throws RPCException
Executes a command on all the cluster nodes. This method is equivalent to the other method of the same type but with the default timeout. The command must be registered first otherwise an RPCException will be thrown.

Parameters:
command - The command to execute on each cluster node
synchronous - if true, sets group request mode to GroupRequest.GET_ALL, and if false sets it to GroupRequest.GET_NONE.
timeout - a timeout after which to throw a replication exception.
args - an array of Serializable objects corresponding to parameters of the command to execute remotely
Returns:
a list of responses from all the members of the cluster. If we met an exception on a given node, the RPCException will be the corresponding response of this particular node
Throws:
RPCException - in the event of problems.

executeCommandOnCoordinator

public Object executeCommandOnCoordinator(RemoteCommand command,
                                          boolean synchronous,
                                          Serializable... args)
                                   throws RPCException
Executes a command on the coordinator only. This method is equivalent to the other method of the same type but with the default timeout. The command must be registered first otherwise an RPCException will be thrown.

Specified by:
executeCommandOnCoordinator in interface RPCService
Parameters:
command - The command to execute on the coordinator node
synchronous - if true, sets group request mode to GroupRequest.GET_ALL, and if false sets it to GroupRequest.GET_NONE.
args - an array of Serializable objects corresponding to parameters of the command to execute remotely
Returns:
the response of the coordinator.
Throws:
RPCException - in the event of problems.

executeCommandOnCoordinator

public Object executeCommandOnCoordinator(RemoteCommand command,
                                          long timeout,
                                          Serializable... args)
                                   throws RPCException
Executes a command synchronously on the coordinator only. The command must be registered first otherwise an RPCException will be thrown.

Specified by:
executeCommandOnCoordinator in interface RPCService
Parameters:
command - The command to execute on the coordinator node
timeout - a timeout after which to throw a replication exception.
args - an array of Serializable objects corresponding to parameters of the command to execute remotely
Returns:
the response of the coordinator.
Throws:
RPCException - in the event of problems.

executeCommandOnCoordinatorMain

protected Object executeCommandOnCoordinatorMain(RemoteCommand command,
                                                 boolean synchronous,
                                                 long timeout,
                                                 Serializable... args)
                                          throws RPCException
Executes a command on the coordinator only. This method is equivalent to the other method of the same type but with the default timeout. The command must be registered first otherwise an RPCException will be thrown.

Parameters:
command - The command to execute on the coordinator node
synchronous - if true, sets group request mode to GroupRequest.GET_ALL, and if false sets it to GroupRequest.GET_NONE.
timeout - a timeout after which to throw a replication exception.
args - an array of Serializable objects corresponding to parameters of the command to execute remotely
Returns:
the response of the coordinator.
Throws:
RPCException - in the event of problems.

excecuteCommand

protected List<Object> excecuteCommand(List<org.jgroups.Address> dests,
                                       RemoteCommand command,
                                       boolean synchronous,
                                       long timeout,
                                       Serializable... args)
                                throws RPCException
Execute the command on all the nodes corresponding to the list of destinations.

Parameters:
dests - the list of members on which the command needs to be executed
command - the command to execute
synchronous - if true, sets group request mode to GroupRequest.GET_ALL, and if false sets it to GroupRequest.GET_NONE.
timeout - a timeout after which to throw a replication exception.
args - the list of parameters
Returns:
a list of responses from all the targeted members of the cluster.
Throws:
RPCException - in the event of problems.

handle

public Object handle(org.jgroups.Message msg)

Specified by:
handle in interface org.jgroups.blocks.RequestHandler

block

public void block()

Specified by:
block in interface org.jgroups.MembershipListener

suspect

public void suspect(org.jgroups.Address suspectedMbr)

Specified by:
suspect in interface org.jgroups.MembershipListener

viewAccepted

public void viewAccepted(org.jgroups.View view)

Specified by:
viewAccepted in interface org.jgroups.MembershipListener

registerCommand

public RemoteCommand registerCommand(RemoteCommand command)
Register a new RemoteCommand instance, it will be mapped to its id. If a command with the same Id has already been registered, a warning will be printed into the log file and the new command will replace the old one.

Specified by:
registerCommand in interface RPCService
Parameters:
command - the instance of the RemoteCommand to register
Returns:
the command itself if it could be registered null otherwise

unregisterCommand

public void unregisterCommand(RemoteCommand command)
Unregister a RemoteCommand instance, if the id is known or the instance itself is known otherwise it will be ignored

Specified by:
unregisterCommand in interface RPCService
Parameters:
command - the command to unregister

isCoordinator

public boolean isCoordinator()
                      throws RPCException
Indicates whether the local node is the coordinator of the cluster

Specified by:
isCoordinator in interface RPCService
Returns:
true if the coordinator is the coordinator, false otherwise throws RPCException in case the RPCService is in an illegal state
Throws:
RPCException

registerTopologyChangeListener

public void registerTopologyChangeListener(TopologyChangeListener listener)
                                    throws SecurityException
Register a new TopologyChangeListener

Specified by:
registerTopologyChangeListener in interface RPCService
Parameters:
listener - the listener to be registered
Throws:
SecurityException - if the SecurityManager is installed and the call method doesn't have the RuntimePermission ACCESS_RPC_SERVICE_PERMISSION

unregisterTopologyChangeListener

public void unregisterTopologyChangeListener(TopologyChangeListener listener)
                                      throws SecurityException
Unregister a TopologyChangeListener if it exists

Specified by:
unregisterTopologyChangeListener in interface RPCService
Parameters:
listener - the listener to unregister
Throws:
SecurityException - if the SecurityManager is installed and the call method doesn't have the RuntimePermission ACCESS_RPC_SERVICE_PERMISSION

getCommand

protected RemoteCommand getCommand(String commandId)
Gives the RemoteCommand corresponding to the given id

Parameters:
commandId - the command id of the command to retrieve
Returns:
the corresponding RemoteCommand

start

public void start()

Specified by:
start in interface Startable

stop

public void stop()

Specified by:
stop in interface Startable

getDefaultTimeout

protected long getDefaultTimeout()
Gives the value of the default timeout

Returns:
the default timeout

getClusterName

protected String getClusterName()
Gives the name of the cluster

Returns:
the name of the cluster

getRetryTimeout

protected long getRetryTimeout()
Gives the value of the retry timeout

Returns:
the value of the retry timeout

isAllowFailover

protected boolean isAllowFailover()
Indicates whether the failover capabilities are enabled or not

Returns:
true if the failover capabilities are allowed, false otherwise

getLocalAddress

protected abstract org.jgroups.Address getLocalAddress()
Returns the channel's own address. The result of calling this method on an unconnected channel is implementation defined (may return null). Calling this method on a closed channel returns null. Successor to #getAddress(). Addresses can be used as destination in the send() operation.

Returns:
The channel's address (opaque) or null if it cannot be found

castMessage

protected abstract org.jgroups.util.RspList castMessage(List<org.jgroups.Address> dests,
                                                        org.jgroups.Message msg,
                                                        boolean synchronous,
                                                        long timeout)
                                                 throws Exception
Cast a message to all the given members

Parameters:
dests - The members to which the message is to be sent.
msg - The message to be sent to the members.
synchronous - Indicates whether the message must be sent in synchronous or asynchronous mode.
timeout - If 0: wait forever. Otherwise, wait for responses or timeout time.
Returns:
A list of responses. Each response is an Object and associated to its sender.
Throws:
Exception - if any error occur while casting the message

createChannel

protected abstract org.jgroups.Channel createChannel()
                                              throws Exception
Create a channel

Returns:
An initialized channel
Throws:
Exception - if any error occur while creating the channel

getMembers

protected abstract List<org.jgroups.Address> getMembers(org.jgroups.View view)
Returns a reference to the List of members (ordered) Do NOT change this list, hence your will invalidate the view Make a copy if you have to modify it.

Returns:
a reference to the ordered list of members in this view

setObject

protected abstract void setObject(org.jgroups.Message m,
                                  Object o)
Takes an object and uses Java serialization to generate the byte[] buffer which is set in the message.


eXo Kernel :: Component :: Common Service 2.5.0-Alpha2

Copyright © 2013 eXo Platform SAS. All Rights Reserved.