Class DefaultCacheServer

java.lang.Object
com.tangosol.util.Base
com.tangosol.net.DefaultCacheServer

public class DefaultCacheServer extends Base
DefaultCacheServer is a simple command line facility and convenience API that starts all services that are declared as requiring an "autostart" in the configurable factory XML descriptor.

DefaultCacheServer can also monitor services it started to ensure they exist. Monitoring services is enabled by default.

Since:
Coherence 2.2
Author:
gg/yc/hr
  • Field Details

    • DEFAULT_WAIT_MILLIS

      public static final long DEFAULT_WAIT_MILLIS
      The default number of milliseconds between checks for service restart.
      See Also:
    • m_serviceMon

      protected volatile ServiceMonitor m_serviceMon
      The ServiceMonitor used to monitor services.
    • m_fShutdown

      protected volatile boolean m_fShutdown
      Flag that indicates whether of not the shutdown was initiated.
    • m_fServicesStarted

      protected boolean m_fServicesStarted
      Flag that indicates whether this instance of DefaultCacheServer has called start on the associated services.
      See Also:
    • m_threadShutdown

      protected Thread m_threadShutdown
      Shutdown hook thread.
    • m_listLifecycleListener

      protected List<com.tangosol.application.LifecycleListener> m_listLifecycleListener
      The LifecycleListeners to be notified of lifecycle events for this DefaultCacheServer.

      The LifecycleListeners will be discovered and loaded using the ServiceLoader.

  • Constructor Details

    • DefaultCacheServer

      public DefaultCacheServer(ConfigurableCacheFactory factory)
      Construct a DefaultCacheServer using the provided factory.
      Parameters:
      factory - the ConfigurableCacheFactory
  • Method Details

    • addLifecycleListener

      public void addLifecycleListener(com.tangosol.application.LifecycleListener listener)
      Add a LifecycleListener that will be notified of lifecycle events for this DefaultCacheServer.
      Parameters:
      listener - the listener to add
    • removeLifecycleListener

      public void removeLifecycleListener(com.tangosol.application.LifecycleListener listener)
      Remove a LifecycleListener so that it will no longer be notified of lifecycle events for this DefaultCacheServer.

      Listeners are stored in a List and will be removed based on a simple object equality check.

      Parameters:
      listener - the listener to remove
    • startAndMonitor

      public void startAndMonitor(long cWaitMillis)
      Start the cache server and check the service status periodically, restarting any stopped services.

      This method will block the calling thread.

      Parameters:
      cWaitMillis - the number of milliseconds between checks
    • startDaemon

      public void startDaemon(long cWaitMillis)
      Start the cache server and asynchronously check the service status periodically, restarting any services that have stopped.
      Parameters:
      cWaitMillis - the number of milliseconds between checks
      Since:
      Coherence 12.1.2
    • startServices

      public List<Service> startServices()
      Start all services that are declared as requiring an "autostart" in the configurable factory XML descriptor.
      Returns:
      list of services that have been successfully started
    • shutdownServer

      public void shutdownServer()
      Shutdown the DefaultCacheServer and Coherence cluster.
    • stop

      public void stop()
      Stop this DefaultCacheServer and dispose the ConfigurableCacheFactory that this server wraps.
      See Also:
    • waitForServiceStart

      public void waitForServiceStart()
      Block the calling thread until DefaultCacheServer has called start on all associated services. Upon control being returned services may not be running due to possible error on start or concurrent shutdown.
      Throws:
      IllegalStateException - iff the services were not attempted to be started, which could be due to concurrent shutdown
    • markServicesStarted

      protected void markServicesStarted()
      Set the "services started" flag.
    • shutdown

      public static void shutdown()
      Shutdown the cache server.
      Since:
      Coherence 3.2
    • startDaemon

      public static void startDaemon()
      Deprecated.
      Start the cache server on a dedicated daemon thread. This method is intended to be used within managed containers.
      Since:
      Coherence 3.2
    • startServerDaemon

      public static DefaultCacheServer startServerDaemon()
      Start the cache server on a dedicated daemon thread, using default ConfigurableCacheFactory. This method is intended to be used within managed containers.
      Returns:
      the instance of the DefaultCacheServer started
      Since:
      Coherence 12.1.2
    • startServerDaemon

      public static DefaultCacheServer startServerDaemon(ConfigurableCacheFactory ccf)
      Start the cache server on a dedicated daemon thread, using specified ConfigurableCacheFactory. This method is intended to be used within managed containers.
      Parameters:
      ccf - the ConfigurableCacheFactory to use
      Returns:
      the instance of the DefaultCacheServer started
      Since:
      20.06
    • main

      public static void main(String[] asArg)
      Entry point: start the cache server under two possible contexts:
      1. With a "coherence-cache-config.xsd" compliant configuration file.
      2. With a GAR file or a directory containing the contents of a GAR file and optionally an application name and comma-delimited list of tenant names.
      If both are provided the latter takes precedence. Additionally DefaultCacheServer accepts a numeric value to monitor the service status (keep alive interval).

      Default configuration file is "coherence-cache-config.xml"; default keep alive interval is 5 sec.

       Example:
         java -server -Xms512m -Xmx512m com.tangosol.net.DefaultCacheServer cache-config.xml 5
       GAR Example:
         java -server -Xms512m -Xmx512m com.tangosol.net.DefaultCacheServer my-app.gar MyApp 5
       
      Parameters:
      asArg - the command line arguments
    • start

      public static List start()
      Start all services that are declared as requiring an "autostart" in the default configurable factory XML descriptor.

      This method will not create a ServiceMonitor for started services.

      Returns:
      list of services that have been successfully started
    • start

      public static List start(ConfigurableCacheFactory factory)
      Start all services that are declared as requiring an "autostart" in the configurable factory XML descriptor.

      This method will not create a ServiceMonitor for started services.

      Parameters:
      factory - ConfigurableCacheFactory to use
      Returns:
      list of services that have been successfully started
    • stopMonitoring

      protected boolean stopMonitoring()
      Ensures the DCS instance has shutdown and the associated ServiceMonitor is no longer monitoring services. return true if the monitoring was active and has been stopped as a result of this call; false if the monitoring was not active
    • stopServiceMonitor

      protected void stopServiceMonitor()
      Stop the ServiceMonitor.
    • notifyShutdown

      protected boolean notifyShutdown()
      Ensure the DCS instance has shutdown. return true if DCS instance was called to start on the associated services
    • initialStartServices

      protected void initialStartServices(long cWaitMillis)
      Setup any necessary resources and start all services.
      Parameters:
      cWaitMillis - the interval, in milliseconds, to monitor services
    • ensureLifecycleListeners

      protected List<com.tangosol.application.LifecycleListener> ensureLifecycleListeners()
      Returns the list of LifecycleListeners registered for this DefaultCacheServer.

      If the list of listeners does not yet exist it will be created and initially populated using the ServiceLoader to discover and load listeners.

      Returns:
      the list of LifecycleListeners registered for this DefaultCacheServer
    • reportStarted

      protected void reportStarted()
      Log the start message.
    • reportStarted

      protected void reportStarted(Collection<Service> colServices)
      Log the start message.
      Parameters:
      colServices - the collection of started services
    • startServicesInternal

      protected Map<Service,String> startServicesInternal()
      Starts the services marked with autostart returning a map of a service name to the started service.
      Returns:
      a map holding all started services keyed by names
    • monitorServices

      protected void monitorServices(long cWaitMillis)
      Check the ServiceMonitor status ensuring it is monitoring services. This method blocks the caller's thread until the DefaultCacheServer is shutdown and monitoring stops.
      Parameters:
      cWaitMillis - the number of milliseconds between checks
    • handleEnsureServiceException

      protected void handleEnsureServiceException(RuntimeException e)
      Invoked if starting a service raises an exception. Re-throws the exception. Override, for example, to log the exception and continue starting services.
      Parameters:
      e - the RuntimeException
      See Also:
    • getServiceBanner

      protected String getServiceBanner(Collection<Service> colService)
      Return a service banner for a collection of services.
      Parameters:
      colService - the collection of services
      Returns:
      a service banner for a collection of services
    • isMonitorStopped

      public boolean isMonitorStopped()
      Return the ServiceMonitor used by the cache server.
      Returns:
      the ServiceMonitor
    • isMonitoringServices

      public boolean isMonitoringServices()
      Return true if this DefaultCacheServer is monitoring services.
      Returns:
      true if this DefaultCacheServer is monitoring services
    • startDaemon

      protected static void startDaemon(DefaultCacheServer dcs)
      Start the provided cache server on a dedicated daemon thread. This method is intended to be used within managed containers.
      Parameters:
      dcs - the cache server to start
    • getConfigurableCacheFactory

      protected static ConfigurableCacheFactory getConfigurableCacheFactory()
      Gets a ConfigurableCacheFactory based on the default configuration.
      Returns:
      a ConfigurableCacheFactory
    • ensureInstance

      protected static DefaultCacheServer ensureInstance(ConfigurableCacheFactory factory)
      Returns the DefaultCacheServer singleton, creating it if necessary.
      Parameters:
      factory - the CacheFactory to use
      Returns:
      the DefaultCacheServer singleton
    • getInstance

      public static DefaultCacheServer getInstance()
      Returns the DefaultCacheServer created by a previous invocation of ensureInstance(ConfigurableCacheFactory). Will throw an IllegalStateException if there is no instance.
      Returns:
      the DefaultCacheServer