Class AbstractRPCService
- java.lang.Object
-
- org.exoplatform.services.rpc.impl.AbstractRPCService
-
- All Implemented Interfaces:
RPCService,org.jgroups.blocks.RequestHandler,org.jgroups.MembershipListener,org.picocontainer.Startable
- Direct Known Subclasses:
RPCServiceImpl
public abstract class AbstractRPCService extends Object implements RPCService, org.picocontainer.Startable, org.jgroups.blocks.RequestHandler, org.jgroups.MembershipListener
This class is a basic implementation of theRPCService, it is mainly based on theMessageDispatcherof 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
Nested Classes Modifier and Type Class Description static classAbstractRPCService.MemberHasLeftExceptionstatic classAbstractRPCService.MessageBodyThis intern class will be used tostatic classAbstractRPCService.StateAll the potential states of theRPCServiceImpl
-
Field Summary
Fields Modifier and Type Field Description protected org.jgroups.ChannelchannelThe JGroups Channel used to communicate with other nodesprotected static StringCLUSTER_NAMEThe default value of the cluster nameprotected org.jgroups.conf.ProtocolStackConfiguratorconfiguratorThe configurator used to create the JGroups Channelprotected org.jgroups.AddresscoordinatorThe address of the current coordinatorprotected static intDEFAULT_RETRY_TIMEOUTThe value of the default retry timeoutprotected static intDEFAULT_TIMEOUTThe value of the default timeoutprotected org.jgroups.blocks.MessageDispatcherdispatcherThe dispatcher used to launch the command of the cluster nodesprotected booleanisCoordinatorIndicates whether the current node is the coordinator of the cluster or notprotected static org.exoplatform.services.log.LogLOGConnection logger.protected List<org.jgroups.Address>membersThe current list of all the members of the clusterprotected static StringPARAM_ALLOW_FAILOVERThe name of the parameter to allow the failoverprotected static StringPARAM_CLUSTER_NAMEThe name of the parameter for the name of the cluster.protected static StringPARAM_DEFAULT_TIMEOUTThe name of the parameter for the default timeoutprotected static StringPARAM_JGROUPS_CONFIGThe name of the parameter for the location of the JGroups configuration.protected static StringPARAM_RETRY_TIMEOUTThe name of the parameter for the retry timeout-
Fields inherited from interface org.exoplatform.services.rpc.RPCService
ACCESS_RPC_SERVICE_PERMISSION
-
-
Constructor Summary
Constructors Constructor Description AbstractRPCService(org.exoplatform.container.ExoContainerContext ctx, org.exoplatform.container.xml.InitParams params, org.exoplatform.container.configuration.ConfigurationManager configManager)The public constructor
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidblock()protected abstract org.jgroups.util.RspListcastMessage(List<org.jgroups.Address> dests, org.jgroups.Message msg, boolean synchronous, long timeout)Cast a message to all the given membersprotected abstract org.jgroups.ChannelcreateChannel()Create a channelprotected 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.ObjectexecuteCommandOnCoordinator(RemoteCommand command, boolean synchronous, Serializable... args)Executes a command on the coordinator only.ObjectexecuteCommandOnCoordinator(RemoteCommand command, long timeout, Serializable... args)Executes a command synchronously on the coordinator only.protected ObjectexecuteCommandOnCoordinatorMain(RemoteCommand command, boolean synchronous, long timeout, Serializable... args)Executes a command on the coordinator only.protected StringgetClusterName()Gives the name of the clusterprotected RemoteCommandgetCommand(String commandId)Gives theRemoteCommandcorresponding to the given idprotected longgetDefaultTimeout()Gives the value of the default timeoutabstract StringgetHostAddress()Returns the IP address of current channel.protected abstract org.jgroups.AddressgetLocalAddress()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 longgetRetryTimeout()Gives the value of the retry timeoutObjecthandle(org.jgroups.Message msg)protected booleanisAllowFailover()Indicates whether the failover capabilities are enabled or notbooleanisCoordinator()Indicates whether the local node is the coordinator of the clusterRemoteCommandregisterCommand(RemoteCommand command)Register a newRemoteCommandinstance, it will be mapped to its id.voidregisterTopologyChangeListener(TopologyChangeListener listener)Register a newTopologyChangeListenerprotected abstract voidsetObject(org.jgroups.Message m, Object o)Takes an object and uses Java serialization to generate the byte[] buffer which is set in the message.voidstart()voidstop()voidsuspect(org.jgroups.Address suspectedMbr)voidunregisterCommand(RemoteCommand command)Unregister aRemoteCommandinstance, if the id is known or the instance itself is known otherwise it will be ignoredvoidunregisterTopologyChangeListener(TopologyChangeListener listener)Unregister aTopologyChangeListenerif it existsvoidviewAccepted(org.jgroups.View view)
-
-
-
Field Detail
-
LOG
protected static final org.exoplatform.services.log.Log LOG
Connection logger.
-
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(org.exoplatform.container.ExoContainerContext ctx, org.exoplatform.container.xml.InitParams params, org.exoplatform.container.configuration.ConfigurationManager configManager)The public constructor- Parameters:
ctx- theExoContainerContextfrom which we will extract the correspondingExoContainerparams- the list of initial parametersconfigManager- 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 anRPCExceptionwill be thrown.- Specified by:
executeCommandOnAllNodesin interfaceRPCService- Parameters:
command- The command to execute on each cluster nodesynchronous- if true, sets group request mode to org.jgroups.blocks.GroupRequest#GET_ALL, and if false sets it to org.jgroups.blocks.GroupRequest#GET_NONE.args- an array ofSerializableobjects 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 anRPCExceptionwill be thrown.- Specified by:
executeCommandOnAllNodesin interfaceRPCService- Parameters:
command- The command to execute on each cluster nodetimeout- a timeout after which to throw a replication exception.args- an array ofSerializableobjects 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 anRPCExceptionwill be thrown.- Parameters:
command- The command to execute on each cluster nodesynchronous- if true, sets group request mode to org.jgroups.blocks.GroupRequest#GET_ALL, and if false sets it to org.jgroups.blocks.GroupRequest#GET_NONE.timeout- a timeout after which to throw a replication exception.args- an array ofSerializableobjects 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 anRPCExceptionwill be thrown.- Specified by:
executeCommandOnCoordinatorin interfaceRPCService- Parameters:
command- The command to execute on the coordinator nodesynchronous- if true, sets group request mode to org.jgroups.blocks.GroupRequest#GET_ALL, and if false sets it to org.jgroups.blocks.GroupRequest#GET_NONE.args- an array ofSerializableobjects 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 anRPCExceptionwill be thrown.- Specified by:
executeCommandOnCoordinatorin interfaceRPCService- Parameters:
command- The command to execute on the coordinator nodetimeout- a timeout after which to throw a replication exception.args- an array ofSerializableobjects 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 anRPCExceptionwill be thrown.- Parameters:
command- The command to execute on the coordinator nodesynchronous- if true, sets group request mode to org.jgroups.blocks.GroupRequest#GET_ALL, and if false sets it to org.jgroups.blocks.GroupRequest#GET_NONE.timeout- a timeout after which to throw a replication exception.args- an array ofSerializableobjects 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 executedcommand- the command to executesynchronous- if true, sets group request mode to org.jgroups.blocks.GroupRequest#GET_ALL, and if false sets it to org.jgroups.blocks.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:
handlein interfaceorg.jgroups.blocks.RequestHandler
-
block
public void block()
- Specified by:
blockin interfaceorg.jgroups.MembershipListener
-
suspect
public void suspect(org.jgroups.Address suspectedMbr)
- Specified by:
suspectin interfaceorg.jgroups.MembershipListener
-
viewAccepted
public void viewAccepted(org.jgroups.View view)
- Specified by:
viewAcceptedin interfaceorg.jgroups.MembershipListener
-
registerCommand
public RemoteCommand registerCommand(RemoteCommand command)
Register a newRemoteCommandinstance, 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:
registerCommandin interfaceRPCService- Parameters:
command- the instance of theRemoteCommandto register- Returns:
- the command itself if it could be registered null otherwise
-
unregisterCommand
public void unregisterCommand(RemoteCommand command)
Unregister aRemoteCommandinstance, if the id is known or the instance itself is known otherwise it will be ignored- Specified by:
unregisterCommandin interfaceRPCService- Parameters:
command- the command to unregister
-
isCoordinator
public boolean isCoordinator() throws RPCExceptionIndicates whether the local node is the coordinator of the cluster- Specified by:
isCoordinatorin interfaceRPCService- Returns:
trueif the coordinator is the coordinator,falseotherwise throws RPCException in case theRPCServiceis in an illegal state- Throws:
RPCException
-
registerTopologyChangeListener
public void registerTopologyChangeListener(TopologyChangeListener listener) throws SecurityException
Register a newTopologyChangeListener- Specified by:
registerTopologyChangeListenerin interfaceRPCService- Parameters:
listener- the listener to be registered- Throws:
SecurityException- if theSecurityManageris installed and the call method doesn't have theRuntimePermissionACCESS_RPC_SERVICE_PERMISSION
-
unregisterTopologyChangeListener
public void unregisterTopologyChangeListener(TopologyChangeListener listener) throws SecurityException
Unregister aTopologyChangeListenerif it exists- Specified by:
unregisterTopologyChangeListenerin interfaceRPCService- Parameters:
listener- the listener to unregister- Throws:
SecurityException- if theSecurityManageris installed and the call method doesn't have theRuntimePermissionACCESS_RPC_SERVICE_PERMISSION
-
getCommand
protected RemoteCommand getCommand(String commandId)
Gives theRemoteCommandcorresponding to the given id- Parameters:
commandId- the command id of the command to retrieve- Returns:
- the corresponding
RemoteCommand
-
start
public void start()
- Specified by:
startin interfaceorg.picocontainer.Startable
-
stop
public void stop()
- Specified by:
stopin interfaceorg.picocontainer.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:
trueif the failover capabilities are allowed,falseotherwise
-
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 thesend()operation.- Returns:
- The channel's address (opaque) or null if it cannot be found
-
getHostAddress
public abstract String getHostAddress()
Returns the IP address of current channel.- Returns:
- The Host's address 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
Objectand associated to its sender. - Throws:
Exception- if any error occur while casting the message
-
createChannel
protected abstract org.jgroups.Channel createChannel() throws ExceptionCreate 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.
-
-