Determining service state

The jagtool getservicestate command returns the state of service components executing in the server. You must code your service component to implement the methods of the CtsServices::ExtendedService interface to allow users to query the component state with jagtool.

This interface extends CtsServices::GenericServices, and adds one method:

long getServiceState()

This method must return one of the constants listed in Table 33-1 to describe the state of the service. These constants are defined in module CtsServices.

Table 33-1: Service states

State

Description

UNKNOWN

The state is unknown.

STARTING

The service is starting. The start method has been called, but has not returned.

STARTED

The service is started, but not yet running. The start method has returned, but run has not been called.

RUNNING

The service is running, that is, executing the run method.

FINISHED

The service is finished processing. The run method has returned. This state applies only to services that do not run continuously until stopped.

STOPPING

The service is stopping. The stop method has been called, and is still running.

STOPPED

The service is stopped. The stop method has been called and has returned.

The following Java example shows service component code that determines and returns state:

import CtsServices.*;

...

public class MyService
{
    private static boolean _starting = false;
    private static boolean _running = false;
    private static boolean _stopping = false;
    private static boolean _stop = false;
    private static boolean _runHasBeenCalled = false;
    private static Object _lock = new Object();
    public void start()
    {
        _starting = true;
        // Perform initialization
        _starting = false;
    }
    public void stop()
    {
        _stopping = true;
        _running = false;
        _stop = true;
        synchronized (_lock)
        {
            _lock.notifyAll();
        }
        // Perform cleanup
        _stopping = false;
    }
    public void run()
    {
        _runHasBeenCalled = true; 
        // Perform per-thread initialization here.
        _running = true;
        while (! _stop)
        {
            try
            {
                // do whatever this service does on 
                // each iteration
                synchronized(_lock)
                {
                    _lock.wait(100000);
                }
            }
            catch (InterruptedException ie)
            {
                _stop = true;
            }
        }
        // Perform per-thread cleanup here.
        _running = false;
    }
    public int getServiceState()
    {
        if (_starting)
        {
            return SERVICE_STATE_STARTING.value;
        }
        else if (! _runHasBeenCalled)
        {
            return SERVICE_STATE_STARTED.value;
        }
        else if (_stopping)
        {
            return SERVICE_STATE_STOPPING.value;
        }
        else if (_stop)
        {
            return SERVICE_STATE_STOPPED.value;
        }
        else if (_running)
        {
            return SERVICE_STATE_RUNNING.value;
        }
        else
        {
            return SERVICE_STATE_FINISHED.value;
        }
    }
}