com.syncbuilder.service
Class ServiceBase

java.lang.Object
  |
  +--com.syncbuilder.service.ServiceBase
Direct Known Subclasses:
SyncServer

public abstract class ServiceBase
extends java.lang.Object
implements ServiceControlListener, java.lang.Runnable

This class is meant to be a suitable basis for most implementations of a service that implements ServiceControlListener.
You will only have to implement the service()-method, which will be invoked continuously in the service's main loop.
You may also implement the init()-method which will be invoked anytime the service is started, and the destroy()-method which will be invoked anytime the service is stopped.

See the Java Tutorial from JavaSoft if you wish to understand the way in which I start, stop, suspend, and resume the Thread.

Another very valuable facility of ServiceBase is its time-slicing facility. Any thread you register with the ServiceBase through the addThread()-method will be added to a pool of threads which are controlled through a time-slicing scheduler. This will avoid thread-starvation on systems which use green-threads.


Constructor Summary
ServiceBase()
           
 
Method Summary
 void addServiceStateListener(ServiceStateListener sscl)
           
 void addThread(java.lang.Thread thread)
          Add a thread to the time-slicing scheduler.
protected  void destroy()
          This method is invoked anytime the service is stopped.
protected  void finalize()
           
protected  void init()
          This method is invoked anytime the service is started.
 void removeServiceStateListener(ServiceStateListener sscl)
           
 void resume()
          Resume operation of the service after it has been suspended.
 void run()
          This is the implementation of the run()-method from the Runnable-interface.
It will go through three distinct stages:
1.
protected abstract  void service()
          This method is invoked periodically while the service is running.
 void start()
          Start the service.
 void stop()
          Stop the service.
 void suspend()
          Temporarily halt the service.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServiceBase

public ServiceBase()
Method Detail

run

public void run()
This is the implementation of the run()-method from the Runnable-interface.
It will go through three distinct stages:
1. Initialization (invocation of init())
2. Providing service, until stopped (continuous invocations of service())
3. Release of occupied resources (invocation of destroy())
Specified by:
run in interface java.lang.Runnable

suspend

public void suspend()
Temporarily halt the service. This will not release any resources and the service will be able to resume operation very quickly.

resume

public void resume()
Resume operation of the service after it has been suspended. This is a very quick operation, because all the resources that the service needs are still allocated.

start

public void start()
Start the service. This will switch the service into a fully operational state.
Starting a service will reserve all resources for that service and might therefore be an expensive operation.

stop

public void stop()
Stop the service. This will switch the service into a completely unready state. It will release all allocated resources and it will have to be restarted in order to run again. Restarting the service from a stopped state may be an expensive operation.

addThread

public void addThread(java.lang.Thread thread)
Add a thread to the time-slicing scheduler. The time-slicer will take complete control over the threads priority, and you should NOT touch the priority after adding the thread to the time-slicer.

addServiceStateListener

public void addServiceStateListener(ServiceStateListener sscl)

removeServiceStateListener

public void removeServiceStateListener(ServiceStateListener sscl)

init

protected void init()
             throws java.lang.Exception
This method is invoked anytime the service is started. It should allocate all the resources which the service needs.

destroy

protected void destroy()
                throws java.lang.Exception
This method is invoked anytime the service is stopped. It should free all the resources which the service held.

service

protected abstract void service()
                         throws java.lang.Exception
This method is invoked periodically while the service is running. You need to implement the services functionality here.
The method should be blocking for a limited amount of time (e.g. 500ms) and then return to the caller.
If you are blocking for too short the service will hog CPU cycles, if you are blocking for too long the service will become unresponsive to control-events.

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class java.lang.Object


This material is Copyrighted (C) 1999 by Tilo Christ. All Rights Reserved.