public abstract class AbstractNurseryService extends GenericService implements NurseryService
A starting point for NurseryService
implementations. It provides a NurseryService
with the following characteristics:
enabled
running state
with a message
Subclasses must implement startService()
and stopService()
in order to provide
their own logic for startup and shutdown.
The GenericService.mLogger
is available to subclasses for logging messages, if required.
A subclass should only carry out whatever operation it is intended to perform if startup is successful.
Subclasses that need to execute operations at regular intervals should create a new thread in their
startService()
implementation and remove it in their stopService()
implementation.
GenericService.isServiceRunning()
can be used to check whether or not the service is running.
$class=org.sample.MyService serviceEnabled=true
Any service you create should be registered in the GlobalBus nursery
startup service (Initial.properties
).
It will then be started together with all other services at startup.
GenericService.StartCommand, GenericService.StopCommand
Modifier and Type | Field and Description |
---|---|
static String |
CLASS_VERSION
The version of this class.
|
mLogger, state
Modifier | Constructor and Description |
---|---|
protected |
AbstractNurseryService()
Default constructor
|
Modifier and Type | Method and Description |
---|---|
void |
doStartService()
This method is called after a
NurseryService has been created, placed in Nursery , and initialized with its configured property values. |
void |
doStopService()
This method is called whenever
Nursery determines that a NurseryService must be shut down. |
protected abstract void |
startService()
Is called by
doStartService() when the NurseryService is being started. |
protected void |
stopIdleService()
Is called by
doStopService() when the service is being shut down and GenericService.isServiceIdle() returns true . |
protected abstract void |
stopService()
Is called by
doStopService() when the service is being shut down. |
assertServiceRunning, getServiceDescription, getServiceMessage, getServiceState, isServiceEnabled, isServiceFailed, isServiceIdle, isServiceRunning, isServiceShutdownFailed, isServiceStopped, setServiceDescription, setServiceEnabled, start, stop, validateIsTrue, validateNotNull
public static final String CLASS_VERSION
protected abstract void startService() throws IllegalStateException, IllegalArgumentException, Exception
Is called by doStartService()
when the NurseryService
is being started. Exceptions thrown by this
method are logged by doStartService()
.
Subclasses must implement this method to bootstrap the service.
Exception
- when something goes wrong during startup. GenericService.isServiceRunning()
will return false
and GenericService.isServiceFailed()
will return true
.IllegalArgumentException
- when the configuration of the service
is invalid. GenericService.isServiceRunning()
will return false
and GenericService.isServiceFailed()
will return true
.IllegalStateException
- when this service is in the wrong state for startup. Used to signal to doStartService()
that it is OK to try to starting
this service
later. GenericService.isServiceRunning()
returns false
and GenericService.isServiceIdle()
returns true
.doStartService()
public final void doStartService() throws Exception
This method is called after a NurseryService
has been created, placed in Nursery
, and initialized with its configured property values.
The NurseryService
can override this method to start any processes it requires.
This method is also called when a NurseryService
is reconfigured. In such cases, NurseryService.doStopService()
is called, then the appropriate properties are
set with the new configuration values, and NurseryService.doStartService()
is called.
This implementation only tries to start the NurseryService
if it is not already running and if it is enabled. It calls startService()
to start the service. It
then sets the NurseryService
in a running state.
doStartService
in interface NurseryService
IllegalStateException
- when GenericService.isServiceRunning()
returns true
.IllegalArgumentException
- when the configuration of the service
is invalid.Exception
- when the call to startService()
throws an Exception
. The Throwable.getCause()
contains
the root cause of the Exception
.startService()
,
NurseryService.doStartService()
protected void stopIdleService() throws IllegalStateException, Exception
Is called by doStopService()
when the service
is being shut down and GenericService.isServiceIdle()
returns true
.
Exceptions thrown by this method are logged by doStopService()
.
Subclasses might provide an implementation of this method to stop an idling service
.
IllegalStateException
- when this service is in the wrong state for shutdown. Used to signal to doStopService()
that it is OK to try stopping this
service
later. GenericService.isServiceRunning()
returns true
.Exception
- when something goes during shutdown. GenericService.isServiceRunning()
returns false
and GenericService.isServiceShutdownFailed()
returns
true
.public final void doStopService() throws Exception
This method is called whenever Nursery
determines that a NurseryService
must be shut down. This may happen due to an explicit
command from the administrator, or it may happen because Nursery
is shutting down. The NurseryService
should respond by stopping
any processes that it has started.
This method is also called when a NurseryService
is reconfigured. In such cases, NurseryService.doStopService()
is called, then the appropriate properties are
set with the new configuration values, and NurseryService.doStartService()
is called.
This implementation only tries to stop the NurseryService
if it is running. It calls stopService()
to
stop the service. It then sets the NurseryService
in a stopped state.
doStopService
in interface NurseryService
IllegalStateException
- when GenericService.isServiceRunning()
returns false
.Exception
- when the call to stopService()
throws an Exception
. The Throwable.getCause()
contains
the root cause of the Exception
.stopService()
,
NurseryService.doStopService()
protected abstract void stopService() throws IllegalStateException, Exception
Is called by doStopService()
when the service
is being shut down. Exceptions thrown by this
method are logged by doStopService()
.
Subclasses must implement this method to shut down the service.
IllegalStateException
- when this service is in the wrong state for shutdown. Used to signal to doStopService()
that
it is OK to try stopping this service
later. GenericService.isServiceRunning()
returns true
.Exception
- when something goes during shutdown. GenericService.isServiceRunning()
returns false
and
GenericService.isServiceShutdownFailed()
returns true
.doStopService()
© 1998- 2018 Stibo DX