public abstract class GenericService extends Object
A starting point for GenericService
implementations. A GenericService
is an object with a well defined life-cycle:
If you have
It provides a GenericService
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 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.
isServiceRunning()
can be used to check whether or not the service is running.
Modifier and Type | Class and Description |
---|---|
protected static interface |
GenericService.StartCommand
Description of StartCommand.
|
protected static interface |
GenericService.StopCommand
Description of StopCommand.
|
Modifier and Type | Field and Description |
---|---|
static String |
CLASS_VERSION
The version of this class.
|
protected org.apache.log4j.Logger |
mLogger
The
Logger that is available for all sub classes. |
protected neo.nursery.NurseryServiceState |
state |
Constructor and Description |
---|
GenericService() |
Modifier and Type | Method and Description |
---|---|
protected void |
assertServiceRunning()
A method that throws an
IllegalStateException when the service is not running. |
String |
getServiceDescription() |
String |
getServiceMessage()
Describes the current state of this service.
|
protected neo.nursery.NurseryServiceState |
getServiceState() |
boolean |
isServiceEnabled()
Indicates whether or not this
GenericService may be started. |
boolean |
isServiceFailed()
The current failure state of this
NurseryService . |
boolean |
isServiceIdle()
The current idle state of this
NurseryService . |
boolean |
isServiceRunning()
The current running state of this
NurseryService . |
boolean |
isServiceShutdownFailed()
The current shutdown state of this
NurseryService . |
boolean |
isServiceStopped()
The current shutdown state of this
NurseryService . |
void |
setServiceDescription(String pServiceDescription) |
void |
setServiceEnabled(boolean pEnabled)
Determines whether or not this
GenericService may be started. |
protected void |
start(GenericService.StartCommand pCommand)
This implementation only tries to start the
NurseryService if it is not already running and if it is enabled. |
protected void |
stop(GenericService.StopCommand pCommand)
This implementation only tries to stop the
NurseryService if it is running. |
protected void |
validateIsTrue(boolean pExpression,
String pMessage)
Validates that the given expression is true, and throws an appropriate exception with a consistent error message if not.
|
protected void |
validateNotNull(Object pPropertyValue,
String pPropertyName)
Validates that the given property value is not null, and throws an appropriate exception with a consistent error message.
|
public static final String CLASS_VERSION
protected final org.apache.log4j.Logger mLogger
Logger
that is available for all sub classes. It will use the name
of your subclass as the name of the Logger.protected neo.nursery.NurseryServiceState state
public final void setServiceEnabled(boolean pEnabled)
GenericService
may be started. The default value is true
.
This method is usually invoked by Nursery
when configuring this GenericService
.pEnabled
- set to true
if you want to make this GenericService
startable.public final boolean isServiceEnabled()
GenericService
may be started. This method is invoked by the
#doStartService()
method to see whether or not it may be started.true
if this GenericService
is enabled, otherwise false
.public String getServiceDescription()
public void setServiceDescription(String pServiceDescription)
protected neo.nursery.NurseryServiceState getServiceState()
public final boolean isServiceRunning()
NurseryService
.true
if this NurseryService
is running, otherwise false
.public final boolean isServiceFailed()
NurseryService
.true
if starting the service failed, otherwise false
.public final boolean isServiceIdle()
NurseryService
.true
if the service is idleing, otherwise false
.public final boolean isServiceShutdownFailed()
NurseryService
.true
if the service failed in shutdown, otherwise false
.public final boolean isServiceStopped()
NurseryService
.true
if the service is stopped, otherwise false
.public final String getServiceMessage()
null
.protected final void start(GenericService.StartCommand pCommand) throws Exception
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.
IllegalStateException
- when 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 final void stop(GenericService.StopCommand pCommand) throws Exception
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.
IllegalStateException
- when 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 final void assertServiceRunning()
IllegalStateException
when the service is not running. Useful as a guard for individual methods of Manager type classes.
This will help subclasses to make sure that your object is in a correct state when invoked.IllegalStateException
- when isServiceRunning()
returns false
, with an message giving the reasonprotected void validateNotNull(Object pPropertyValue, String pPropertyName)
pPropertyValue
- the value we want to validate that is not nullpPropertyName
- the name of the property we are validatingprotected void validateIsTrue(boolean pExpression, String pMessage)
pExpression
- the value we want to validate that is truepMessage
- a message explaining what needs to be true© 1998- 2018 Stibo DX