Package me.legrange.service
Class Service<Conf>
- java.lang.Object
-
- me.legrange.service.Service<Conf>
-
- Type Parameters:
Conf- The type of the configuration class for this service.
public abstract class Service<Conf> extends Object
- Author:
- gideon
-
-
Constructor Summary
Constructors Constructor Description Service()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <C extends Component>
CgetComponent(Class<C> clazz)Get the running component for the given component class.protected ConfgetConfig()Return the service configuration.booleanisRunning()static voidmain(String... args)protected voidonStop()Stop the service.<C extends Component>
CrequireComponent(Class<C> clazz)voidsleep(int seconds)protected abstract voidstart()Start the service.voidstart(Conf config)voidstop()voidsubmit(Runnable task)Submit a task for running to the thread pool.
-
-
-
Method Detail
-
main
public static void main(String... args)
-
start
public final void start(Conf config) throws ServiceException
- Throws:
ServiceException
-
stop
public final void stop() throws ServiceException- Throws:
ServiceException
-
getComponent
public final <C extends Component> C getComponent(Class<C> clazz) throws ComponentNotFoundException
Get the running component for the given component class.- Type Parameters:
C- The type of the component- Parameters:
clazz- The class representing the component- Returns:
- The component
- Throws:
ComponentNotFoundException- Thrown if the component requested cannot be found
-
requireComponent
public final <C extends Component> C requireComponent(Class<C> clazz) throws ServiceException
- Throws:
ServiceException
-
isRunning
public final boolean isRunning()
-
start
protected abstract void start() throws ServiceExceptionStart the service. This needs to be implemented by the implementation subclass to get work done.- Throws:
ServiceException- Thrown if the service cannot be started
-
onStop
protected void onStop() throws ServiceExceptionStop the service. This can be implemented by the implementation subclass to release resources, do cleanup or accomplish other shut down tasks.- Throws:
ServiceException- Thrown if there is a problem stopping the service
-
getConfig
protected Conf getConfig()
Return the service configuration.- Returns:
- The configuration object
-
submit
public void submit(Runnable task)
Submit a task for running to the thread pool.- Parameters:
task- The task to run.
-
sleep
public final void sleep(int seconds)
-
-