public abstract class ServiceImpl extends ServiceBaseImpl implements Service, Alerter.Listener
Implements common or default behaviors for service implementations.
| Modifier and Type | Field and Description |
|---|---|
static ElapsedTime |
DEFAULT_JOIN_TIMEOUT
Default join timeout.
|
static ElapsedTime |
DEFAULT_RESTART_DELAY
Default restart delay.
|
static ElapsedTime |
DEFAULT_STARTING_EXTEND
Default starting extend.
|
static ElapsedTime |
DEFAULT_STOPPING_EXTEND
Default stopping extend.
|
static String |
DEPENDENCY_PROPERTY
Contains the UUID of an other service upon which this one is dependent.
|
static String |
JOIN_TIMEOUT_PROPERTY
Join timeout.
|
static String |
MEMORY_LOG_INTERVAL_PROPERTY
Memory log interval property.
|
static String |
MONITOR_DISABLED_PROPERTY
Disables the service monitor.
|
static String |
RESTART_ALLOWED_PROPERTY
Allows restarts.
|
static String |
RESTART_DELAY_PROPERTY
Specifies a delay when the service is self-restarting.
|
static String |
RESTART_IGNORED_PROPERTY
Ignores the restart signal.
|
static String |
SERVICE_LOG_ID_PROPERTY
Used within a 'service' element to provide an identifying log ID to a
service.
|
static String |
SERVICE_UUID_PROPERTY
Used within a 'service' element to provide an identifying UUID to a
service.
|
static String |
STARTING_EXTEND_PROPERTY
The number of millis by which to extend the start time.
|
static String |
STARTUP_DELAY_PROPERTY
Specifies a delay before making the service available.
|
static String |
STATS_LOG_ENABLED_PROPERTY
Enables the logging of service stats.
|
static String |
STOPPING_EXTEND_PROPERTY
The number of millis by which to extend the stop time.
|
static String |
ZOMBIE_ENABLED_PROPERTY
Enables a 'zombie' state when the service fails to start.
|
MIDNIGHT_EVENT, PING_SIGNAL, PONG_EVENT, RESTART_NOW_SIGNAL, RESTART_SIGNAL, RESUME_SIGNAL, RESUMED_EVENT, STARTED_EVENT, STOP_NOW_SIGNAL, STOP_SIGNAL, STOPPED_EVENT, SUSPEND_SIGNAL, SUSPENDED_EVENT, WATCHDOG_EVENT, ZOMBIE_EVENT| Modifier | Constructor and Description |
|---|---|
protected |
ServiceImpl()
Creates a new service implementation.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
addAlertListener(Alerter.Listener listener)
Adds an alert listener.
|
static void |
cancelRestarters()
Cancels restarters.
|
protected ServiceStats |
createStats(StatsOwner statsOwner)
Creates a stats instance.
|
void |
disableSuspend()
Disables suspend.
|
protected boolean |
doPendingActions()
Does pending actions.
|
protected void |
doRun()
Does run.
|
protected abstract void |
doStart()
Does start.
|
protected abstract void |
doStop()
Does stop.
|
void |
enableSuspend()
Enables suspend.
|
boolean |
exportAgent()
Exports the JMX agent.
|
void |
fail()
Fails the service.
|
Alerter |
getAlerter()
Gets the alerter.
|
Config |
getConfig()
Gets the service config.
|
File |
getDataDir()
Gets the data directory.
|
Optional<String> |
getEntityName()
Gets the name of the associated entity.
|
long |
getJoinTimeout()
Gets the join timeout.
|
protected Object |
getMutex()
Gets the mutex.
|
Optional<UUID> |
getOptionalSourceUUID()
Gets the optional source UUID.
|
ServiceActivator |
getServiceActivator()
Gets the service activator owning this.
|
Optional<UUID> |
getServiceUUID()
Gets the service UUID.
|
UUID |
getSourceUUID()
Gets the source UUID.
|
boolean |
isJMXRegistrationEnabled()
Asks if the JMX registration is enabled.
|
boolean |
isRunning()
Asks if the service is running.
|
boolean |
isStarted()
Asks if the service has been started.
|
boolean |
isStopped()
Asks if the service has been stopped.
|
boolean |
isStopping()
Asks if the service is stopping.
|
boolean |
isWait()
Asks if the service waits until running.
|
boolean |
isZombie()
Asks if the service is a zombie.
|
void |
monitorService(Optional<String> name,
Optional<UUID> uuid,
Optional<String> reference)
Registers a service to be monitored.
|
boolean |
onAlert(Optional<Alert> optionalAlert)
Called on alert.
|
protected boolean |
onEvent(Event event)
Called when a event has been received.
|
protected void |
onServicesNotReady()
Acts when some services are not ready.
|
protected void |
onServiceStartFailure()
Called when the service start failed.
|
protected boolean |
onSignal(Signal signal)
Called when a signal has been received.
|
String |
registerServer(SessionFactory server,
String serverPath)
Registers a RMI server.
|
boolean |
removeAlertListener(Alerter.Listener listener)
Removes an alert listener.
|
void |
restart(boolean delayed)
Restarts the service.
|
void |
restoreConfigState()
Restores the config state.
|
void |
restoreMonitored()
Restores monitored services.
|
void |
resume()
Resumes.
|
void |
run()
Runs within the thread.
|
void |
saveConfigState()
Saves the config state.
|
void |
saveMonitored()
Saves monitored services.
|
void |
sendAlert(Alert alert)
Sends an alert.
|
void |
sendEvent(String name,
Optional<Object> info)
Sends an event.
|
void |
sendSignal(String name,
Optional<?> info)
Sends a signal.
|
boolean |
setRestartEnabled(boolean restartEnabled)
Sets the restart enabled indicator.
|
protected void |
setRestartSignaled(boolean restartSignaled)
Sets the restart signaled indicator.
|
void |
setSourceUUID(UUID sourceUUID)
Sets the source UUID.
|
protected boolean |
setUp()
Sets up the service thread.
|
void |
starting()
Informs that the start is progressing.
|
void |
stopping()
Informs that the stop is progressing.
|
void |
suspend()
Suspends.
|
protected void |
tearDown()
Tears down what has been set up.
|
protected void |
tearDownConfig()
Tears down the config.
|
boolean |
trySuspend(long timeout)
Tries to suspend.
|
void |
unregisterServer(String serverName)
Unregisters a RMI server.
|
addStats, closeSnoozeAlarm, getLogID, getServiceActivatorBase, getServiceName, getStats, getThisLogger, getThread, getTimer, getVersion, interrupt, isCurrentThread, isThreadStarted, join, logStats, scheduleMidnightLogger, setLogID, setServiceActivatorBase, setServiceName, snooze, starting, startThread, startTimer, stopping, stopTimer, toString, uncaughtException, updateStats, wakeUpclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitaddStats, getServiceName, getStats, getTimer, snooze, starting, stopping, wakeUppublic static final ElapsedTime DEFAULT_JOIN_TIMEOUT
public static final ElapsedTime DEFAULT_RESTART_DELAY
public static final ElapsedTime DEFAULT_STARTING_EXTEND
public static final ElapsedTime DEFAULT_STOPPING_EXTEND
public static final String DEPENDENCY_PROPERTY
public static final String JOIN_TIMEOUT_PROPERTY
public static final String MEMORY_LOG_INTERVAL_PROPERTY
public static final String MONITOR_DISABLED_PROPERTY
public static final String RESTART_ALLOWED_PROPERTY
public static final String RESTART_DELAY_PROPERTY
public static final String RESTART_IGNORED_PROPERTY
public static final String SERVICE_LOG_ID_PROPERTY
public static final String SERVICE_UUID_PROPERTY
public static final String STARTING_EXTEND_PROPERTY
public static final String STARTUP_DELAY_PROPERTY
public static final String STATS_LOG_ENABLED_PROPERTY
public static final String STOPPING_EXTEND_PROPERTY
public static final String ZOMBIE_ENABLED_PROPERTY
public static void cancelRestarters()
public boolean addAlertListener(Alerter.Listener listener)
addAlertListener in interface Servicelistener - The listener to be added.public void disableSuspend()
throws InterruptedException
disableSuspend in interface ServiceInterruptedException - When interrupted.public void enableSuspend()
enableSuspend in interface Servicepublic boolean exportAgent()
exportAgent in interface Servicepublic void fail()
This is called by the service implementation or one of its children when an unexpected condition is detected. It may be overridden.
fail in interface ServiceBasefail in class ServiceBaseImplpublic Alerter getAlerter()
getAlerter in interface Servicepublic final Config getConfig()
public final File getDataDir()
getDataDir in interface Servicepublic Optional<String> getEntityName()
getEntityName in interface Servicepublic long getJoinTimeout()
getJoinTimeout in interface Servicepublic Optional<UUID> getOptionalSourceUUID()
getOptionalSourceUUID in interface Servicepublic final ServiceActivator getServiceActivator()
getServiceActivator in interface Servicepublic final Optional<UUID> getServiceUUID()
getServiceUUID in interface Servicepublic UUID getSourceUUID()
getSourceUUID in interface Servicepublic final boolean isJMXRegistrationEnabled()
isJMXRegistrationEnabled in interface ServiceBaseisJMXRegistrationEnabled in class ServiceBaseImplpublic final boolean isRunning()
public final boolean isStarted()
public final boolean isStopped()
public final boolean isStopping()
isStopping in interface Servicepublic boolean isWait()
public final boolean isZombie()
public final void monitorService(Optional<String> name,
Optional<UUID> uuid,
Optional<String> reference)
Does nothing if the service name and UUID are both empty.
monitorService in interface Servicename - The optional service name.uuid - The optional UUID identifying the service.reference - An optional service reference.public boolean onAlert(Optional<Alert> optionalAlert)
onAlert in interface Alerter.ListeneroptionalAlert - The alert (empty on failure).public final String registerServer(SessionFactory server, String serverPath)
registerServer in interface Serviceserver - The server object.serverPath - The path to the server.public boolean removeAlertListener(Alerter.Listener listener)
removeAlertListener in interface Servicelistener - The listener to be removed.public final void restart(boolean delayed)
public final void restoreConfigState()
restoreConfigState in interface Servicepublic final void restoreMonitored()
restoreMonitored in interface Servicepublic final void run()
The objective of this method is to streamline the overall processing and exception handling for subclasses. Instead of implementing this method, subclass should implement or override the following methods:
run in interface Runnablepublic final void saveConfigState()
saveConfigState in interface Servicepublic final void saveMonitored()
saveMonitored in interface Servicepublic final void sendAlert(Alert alert)
public final void sendEvent(String name,
Optional<Object> info)
public final void sendSignal(String name,
Optional<?> info)
sendSignal in interface Servicename - The signal's name.info - Additional optional informations.public final boolean setRestartEnabled(boolean restartEnabled)
setRestartEnabled in interface ServicerestartEnabled - The restart enabled indicator.public final void setSourceUUID(UUID sourceUUID)
setSourceUUID in interface ServicesourceUUID - The source UUID.public final void starting()
public final void stopping()
public final void suspend()
throws InterruptedException
public final boolean trySuspend(long timeout)
throws InterruptedException
trySuspend in interface Servicetimeout - The timeout in milliseconds.InterruptedException - When interrupted.public final void unregisterServer(String serverName)
unregisterServer in interface ServiceserverName - The name of the server.protected ServiceStats createStats(StatsOwner statsOwner)
createStats in class ServiceBaseImplstatsOwner - The stats owner.@CheckReturnValue
protected boolean doPendingActions()
throws InterruptedException,
ServiceNotAvailableException
Caution: this is called while synchronized on mutex.
InterruptedException - When the service is stopped.ServiceNotAvailableException - When the service is not available.protected void doRun()
throws Exception
Exception - Handled by run().protected abstract void doStart()
throws Exception
Exception - Handled by run().protected abstract void doStop()
throws Exception
Exception - Handled by run().@Nonnull @CheckReturnValue protected final Object getMutex()
@CheckReturnValue
protected boolean onEvent(@Nonnull
Event event)
Caution: this is called while synchronized on mutex.
event - The signal.protected void onServiceStartFailure()
throws InterruptedException
Caution: this is called while synchronized on mutex.
InterruptedException - If the service is stopped while waiting.protected void onServicesNotReady()
Caution: this is called while synchronized on mutex.
This is called by the pending actions processing when it not known if all registered services are ready. This may be used by a service thread to trigger some state refresh when the pending actions processing are completed; at that time, all the registered services will be ready.
@CheckReturnValue
protected boolean onSignal(@Nonnull
Signal signal)
Caution: this is called while synchronized on mutex.
signal - The signal.protected void setRestartSignaled(boolean restartSignaled)
restartSignaled - The restart signaled indicator.protected boolean setUp()
Overriding subclasses must call this with success before proceeding.
setUp in class ServiceBaseImplprotected void tearDown()
tearDown in class ServiceBaseImplprotected void tearDownConfig()
Copyright © 2003-2019 Serge Brisson. All Rights Reserved.