Product Home

Usage

Demo

Download

Purchase

Support

Feedback

 

Demo 4: Window NT Service supports PAUSE

Function: Convert JAR archive into Executive.


Part 1: Code Example

1. Download the interface code and compile:

Download: [Interface Download] - 4kb


2. Compose a java program D:\demo\DemoPauseService.java:

    The function of the demo service: Listen on a port (for example 1234). Create a new thread when a client connects. Then, the service thread will echo back whatever the client send, until 'q' to to stop serve:

package demo;

import java.io.*;
import java.net.*;
import java.util.*;

import com.regexlab.j2e.*;

public class DemoPauseService implements ServiceStatusHandler, Runnable
{
    // public used
    static Vector  all_clients = new Vector();
    static boolean is_paused   = false;

    // private used by each client
    PrintWriter out    = null;
    Socket      socket = null;

    // public used broadcast
    static void broadcast(String msg)
    {
        for(int i=0; i<all_clients.size(); i++)
        {
            DemoPauseService client =
                (DemoPauseService)all_clients.get(i);

            client.out.println(msg);
            client.out.flush();
        }
    }

    // client socket
    public DemoPauseService( Socket sock )
    {
        socket = sock;
    }

    // tell windows service manager that PAUSE/CONTINUE supported
    public boolean canPause() {
        return true;
    }

    // when paused
    public boolean onPause() {
        broadcast("--- PAUSED ---");
        is_paused = true;
        return true;
    }

    // when continued
    public boolean onContinue() {
        broadcast("--- CONTINUED ---");
        is_paused = false;
        return true;
    }

    // before service stopped
    public boolean onStop() {
        broadcast("--- STOPPED ---");
        return true;
    }

    // thread run
    public void run()
    {
        try
        {
            BufferedReader in  = new BufferedReader(
                new
InputStreamReader(socket.getInputStream())
            );
            out = new PrintWriter(socket.getOutputStream());

            if( is_paused )
            {
                out.println("--- Service is PAUSED now ---");
                out.flush();

                return;
            }

            // add(this) in order to be broadcasted
            all_clients.add(this);

            // welcome
            out.println("---- Welcome! Type 'q' to EXIT: ----");
            out.flush();

            // loop
            String line;
            while( (line = in.readLine()) != null )
            {
                if( line.equalsIgnoreCase("q") )
                    break;

                if( is_paused )
                {
                    out.println("--- Service is PAUSED now. ---");
                    out.flush();
                    continue;
                }

                out.println(line);
                out.println();

                out.flush();
            }
        }
        catch(Exception e)
        {
            e.printStackTrace(System.err);
        }
        finally
        {
            all_clients.remove(this);
            try { socket.close(); } catch (Exception e) {}
        }
    }

    // main method
    public static void main(String[] args) throws Exception
    {
        // use to accept events
        ServiceStatusManager.setServiceStatusHandler(
            new
DemoPauseService(null) // instance used as handler
        );

        // listen 1234
        ServerSocket listen = new ServerSocket(1234);
        System.out.println("Service started...");

        while(true)
        {
            // accept, and start
            new Thread(new DemoPauseService(listen.accept())).start();
        }
    }
}


Part 2: Compile and Run

1. Refer to "Demo 3" to Compile And Run.

2. Download the program and result exe in this demo:

[demopause.zip] - 23kb


More Demos:

Demo 1: How to generate a CONSOLE application in java?

Demo 2: How to generate a Windows GUI application in java?

Demo 3: How to build and create a Window NT Service in java?

Demo 5: System Taskbar Tray Icon and Event Log

 

 

RegExLab.com © 2005 - All Rights Reserved