public abstract class AbstractService extends Object implements Service, Runnable
| Modifier and Type | Field and Description |
|---|---|
protected Object |
syncObject
Object used for synchronization.
|
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractService()
Constructs a new abstract service with an anonymous name.
|
protected |
AbstractService(String name)
Constructs a new abstract service with the given name.
|
protected |
AbstractService(String name,
boolean registerShutdownHook)
Constructs a new abstract service with the given name.
|
protected |
AbstractService(String name,
boolean registerShutdownHook,
ThreadFactory threadFactory)
Constructs a new abstract service with the given name.
|
protected |
AbstractService(String name,
ThreadFactory threadFactory)
Constructs a new abstract service with the given name.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addServiceListener(ServiceListener serviceListener)
Adds the given service listener to the list of service listeners that
will be notified on service events.
|
void |
destroy()
Destroys this service, freeing all used resources.
|
String |
getAction()
Returns the current action of the service.
|
String |
getName()
Returns the name of this service.
|
Object |
getServiceAttribute(String attributeName)
Returns the value of the service attribute with the given name.
|
State |
getState()
Returns the current state of this service.
|
String |
getStateReason()
Deprecated.
|
boolean |
hasServiceAttribute(String attributeName)
Returns whether this service contains a service attribute with the given
name.
|
void |
init()
Initializes this services.
|
protected void |
notifySyncObject()
|
void |
removeServiceListener(ServiceListener serviceListener)
Removes the given service listeners from the list of service listeners.
|
void |
run()
Runner for the main
serviceRun() method. |
protected void |
serviceDestroy()
Empty implementation.
|
protected void |
serviceInit()
Empty initialization.
|
protected void |
serviceRun()
Empty implementation.
|
protected void |
serviceStart()
Empty implementation.
|
protected void |
serviceStop()
Empty implementation.
|
protected void |
setAction(String action)
Sets the action of the service.
|
void |
setServiceAttribute(String attributeName,
Object attributeValue)
Sets the service attribute with the given name to the given value.
|
protected void |
setState(State newState)
Sets the new state of this service without changing the action.
|
protected void |
setState(State newState,
String action)
Sets the new state of this service.
|
protected void |
setStateReason(String action)
Deprecated.
Use
setAction(String) instead |
void |
setThreadFactory(ThreadFactory threadFactory)
Sets the thread factory that this service uses to spawn new threads.
|
protected boolean |
shouldStop()
Returns whether this service should stop.
|
protected void |
sleep()
Sleeps until
syncObject gets Object.notify()'ed. |
protected void |
sleep(long timeout)
Sleeps until
syncObject gets Object.notify()'ed or the
specified timeout (in milliseconds) has elapsed. |
void |
start()
Attempts to start this service.
|
void |
stop()
Stops this service.
|
String |
toString() |
protected final Object syncObject
protected AbstractService()
protected AbstractService(String name)
name - The name of the serviceprotected AbstractService(String name, ThreadFactory threadFactory)
name - The name of the servicethreadFactory - The thread factory used to create the service threadprotected AbstractService(String name, boolean registerShutdownHook)
name - The name of the serviceregisterShutdownHook - true to register shutdown hook for this service,
false to not register a shutdown hookprotected AbstractService(String name, boolean registerShutdownHook, ThreadFactory threadFactory)
name - The name of the serviceregisterShutdownHook - true to register shutdown hook for this service,
false to not register a shutdown hookthreadFactory - The thread factory used to create the service threadpublic void addServiceListener(ServiceListener serviceListener)
addServiceListener in interface ServiceserviceListener - The service listener to addService.addServiceListener(net.pterodactylus.util.service.ServiceListener)public void removeServiceListener(ServiceListener serviceListener)
removeServiceListener in interface ServiceserviceListener - The service listener to removeService.removeServiceListener(net.pterodactylus.util.service.ServiceListener)public void setThreadFactory(ThreadFactory threadFactory)
threadFactory - The thread factory for new threadspublic State getState()
getState in interface ServicegetState in interface ServiceMBeanService.getState()@Deprecated public String getStateReason()
null.getStateReason in interface Servicepublic String getAction()
getAction in interface ServicegetAction in interface ServiceMBeanServiceMBean.getAction()public String getName()
getName in interface ServiceService.getName()public String toString()
toString in class ObjectObject.toString()public Object getServiceAttribute(String attributeName)
ServicegetServiceAttribute in interface ServiceattributeName - The name of the attributeService.getServiceAttribute(java.lang.String)public boolean hasServiceAttribute(String attributeName)
ServicehasServiceAttribute in interface ServiceattributeName - The name of the attributetrue if this service has a service attribute with
the given name, false otherwiseService.hasServiceAttribute(java.lang.String)public void setServiceAttribute(String attributeName, Object attributeValue)
ServicesetServiceAttribute in interface ServiceattributeName - The name of the attributeattributeValue - The value of the attributeService.setServiceAttribute(java.lang.String,
java.lang.Object)protected void setState(State newState)
setState(newState, null).newState - The new state of this servicesetState(State, String)@Deprecated protected void setStateReason(String action)
setAction(String) insteadaction - The new action of the service, or null to not
change the actionprotected void setAction(String action)
action - The new action of the service, or null to not
change the actionprotected void setState(State newState, String action)
Service-State-Changed event is fired. If the given action is
null, the current action will not be changed.newState - The new state of the serviceaction - The current action of the service, or null if the
action should not be changedprotected boolean shouldStop()
true if this service should stop, false
otherwiseprotected void serviceInit()
init()public final void init()
state of this service is not
State.offline. Otherwise serviceInit() is called for the
real initialization of this service.init in interface ServiceserviceInit(),
Service.init()protected void serviceStart()
start()public final void start()
state is not State.offline.
Otherwise serviceStart() is called for further post-init,
pre-start initialization.start in interface Servicestart in interface ServiceMBeanserviceStart(),
Service.start()protected void serviceRun()
sleep() until
stop() is called.run()public final void run()
serviceRun() method. This method sets the
state to State.online and fires a
Service-Started event
before serviceRun() is called. When the serviceRun()
method terminates the state is set to State.offline
without changing the action. If the serviceRun() method threw an
exception state is set to State.offline with the
message of the exception as action. In
both cases a Service-Stopped event is fired.protected void serviceStop()
stop()public final void stop()
state is not State.online or State.starting.
Otherwise shouldStop is set to true,
serviceStop() is called, and sleeping threads (if any) are
notified.stop in interface Servicestop in interface ServiceMBeanService.stop()protected void serviceDestroy()
destroy()public final void destroy()
state is not State.offline.
Otherwise serviceDestroy() is called and internal resources are
freed.destroy in interface ServiceService.destroy()protected void sleep()
syncObject gets Object.notify()'ed.protected void sleep(long timeout)
syncObject gets Object.notify()'ed or the
specified timeout (in milliseconds) has elapsed. This method will return
immediately if the service has already been told to stop().timeout - The number of milliseconds to waitprotected void notifySyncObject()
Copyright © 2026. All rights reserved.