Class ClusterService

  • Direct Known Subclasses:
    ImportEntryClusterService

    @ApplicationScoped
    public abstract class ClusterService
    extends Object
    This class will be extended by those classes that implements a service which supports working in a clustered environment.
    • Constructor Detail

      • ClusterService

        public ClusterService()
    • Method Detail

      • init

        protected boolean init​(String currentNodeId,
                               String currentNodeName)
        Initializes the cluster service, including all its settings.
        Parameters:
        currentNodeId - The identifier of the current cluster node
        currentNodeName - The name of the current cluster node
        Returns:
        true if the service is initialized properly, false otherwise.
      • getTimeout

        protected Long getTimeout()
        Returns:
        the frequency (timeout) which the ping will be done for this service.
      • getThreshold

        protected Long getThreshold()
        Returns:
        the threshold used for this service. This is an extra amount of time added to the timeout that helps to avoid unnecessarily switching the node that should handle a service on every ping round.
      • getNextPing

        protected Long getNextPing()
        Returns:
        the timestamp representing the time of the next ping scheduled for this service.
      • setNextPing

        protected void setNextPing​(Long nextPing)
        Sets the next time which the ping should be done for this service.
        Parameters:
        nextPing - The timestamp of the next ping for this service
      • isInitialized

        protected boolean isInitialized()
        Returns:
        true if the service has been initialized, false otherwise.
      • isHandledInCurrentNode

        public boolean isHandledInCurrentNode()
        Returns:
        true if the current cluster node should handle this service, false otherwise. Note that if we are not in a clustered environment, this method is always returning true.
      • getIdentifierOfNodeHandlingService

        public String getIdentifierOfNodeHandlingService()
        Returns:
        a String with the identifier of the cluster node currently handling the service.
      • setUseCache

        protected void setUseCache​(boolean useCache)
        Used to define if the service should check into the database whether the current node is handling the service.
        Parameters:
        useCache - If true, then the service will check into the database if the current node is handling the service. If false, then it will use the last read value.
      • isDisabled

        protected boolean isDisabled()
        Returns:
        true if the service is disabled, false otherwise.
      • setDisabled

        protected void setDisabled​(boolean isDisabled)
        Disables the current service. This means that the ping will not be performed for this service in the current node.
        Parameters:
        isDisabled - If true, then the service will be disabled. If false, then it will be enabled.
      • startProcessing

        public void startProcessing()
        This method is used to set the current cluster service into an state that indicates that it is currently processing its tasks.
      • endProcessing

        public void endProcessing()
        This method is used to restore the cluster service into an state which indicates that it is currently not processing any task.
      • deregister

        protected void deregister()
        Disables the service and in case it is being handled in the current node, then the node is deregistered to force the election of a new node to handle the service. If the service is currently processing its tasks, then all the actions will be postponed until the processing finishes.
      • getNodeIdentifier

        public String getNodeIdentifier()
        Returns:
        a String with the identifier of the current cluster node.
      • prepareForNewNodeInCharge

        protected void prepareForNewNodeInCharge()
        Executes the actions that should be done right before the cluster node is prepared to be selected as the node in charge of handling the service.
      • getServiceName

        protected abstract String getServiceName()
        Returns:
        a String that uniquely identifies the service.
      • isAlive

        protected abstract boolean isAlive()
        Returns:
        true if the service currently is running in the present cluster node, false otherwise.
      • isEnabled

        protected abstract boolean isEnabled()
        Returns:
        true if it is allowed to execute this service in the present cluster node, false otherwise.