Service

To receive the status change event of Service, or control the Service status


Introduction

To integrate with Service is not necessary.

If you want to control the Service status, or receive the status change event, you can do the integration by the API. On Linux, there is no PAUSE status for generated exe, so 'pause' and 'continue' command are not supported on Linux.


API Classes

There are two API classes to integrate with Service:

public class ServiceStatusManager;
public interface ServiceStatusHandler;

The first class ServiceStatusManager is to control the Service status:

public class ServiceStatusManager
{
    /**
     * Whether runs as service, will be set by frame, do not modify it by yourself
     */
    public static boolean isRunAsService;

    /**
     * Set service status, this will not trigger onPause/onContinue event.
     * On Linux, because PAUSE is not supported, the method has no use.
     */
    public static void setServiceStatus(int status);

    /**
     * Set status callback handler
     */
    public static void setServiceStatusHandler(ServiceStatusHandler handler);
}

The interface ServiceStatusHandler is to receive status change event:

public interface ServiceStatusHandler
{
    /**
     * Tell the service manager whether PAUSE/CONTINUE is supported.
     * This is supported on Windows only.
     *
     * @return true means this service can be paused
     */
    public boolean canPause();

    /**
     * Pause/continue event handle methods.
     * This is supported on Windows only.
     *
     * @return must return true if pause successfully
     */
    public boolean onPause();

    /**
     * Pause/continue event handle methods.
     * Thi is supported on Windows only.
     *
     * @return must return true if continue successfully
     */
    public boolean onContinue();

    /**
     * Stop service event handle method
     *
     * @return the return value is ignored
     */
    public boolean onStop();
}

On Linux, because PAUSE status is not supported, all supported is the "onStop()" event.


Demo Code

To set a status change event handler:

ServiceStatusManager.setServiceStatusHandler(new ServiceStatusHandler() {
    
    /**
     * This is to tell windows, PAUSE supported
     */
    public boolean canPause() {
        return true;
    }
    
    /**
     * When System Administrator run "net continue ServiceName" command
     */
    public boolean onContinue() {
        // Do something to continue
        return true;
    }
    
    /**
     * When System Administrator run "net pause ServiceName" command
     */
    public boolean onPause() {
        // Do something to be paused
        return true;
    }
    
    /**
     * When System Administrator run "net stop ServiceName" command
     * or "service ProgramName stop" on Linux
     */
    public boolean onStop() {
        // Do something clearing
        return true;
    }
    
});

See Also

Add new comment