1

我想关闭HTTP 线程池服务器。为此,我制作了一个正在监听 STDIN 的线程。如果我键入shutdown,它会关闭服务器。

代码是

public class WebServerShutdown extends Thread{

    Logger log = Logger.getLogger( WebServerShutdown.class.getName() );

    @Override
    public void run() {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            //ThreadPool threadpool = new ThreadPool();
            String s;
            while ((s = in.readLine()) != null && s.length() != 0) {
                System.out.println(s);
                if(s.equalsIgnoreCase("shutdown")) {
                    ThreadPool.isStopped = true;
                    break;
                }
            }
        } catch (IOException ex) {
            log.info(new Date() + " Problem in reading Input from keyboard");
        }
        System.out.println("Thread comes out");
    }
 }

服务器监听的代码

    while( !(ThreadPool.isStopped) ) {
        Socket client_socket = null;
        try {
            System.out.println("try to accept socket");
            client_socket = serversocket.accept();
        } catch(IOException ie) {
            log.info(new Date() + " Server Stopped");
        }
        ThreadPool.threads.execute(new Worker(client_socket));          
    }

    System.out.println("Reached here");

ThreadPool.threads.shutdown();
serversocket.close();

log.info(new Date() + " WEBSERVER STOPPED");

但是有一个问题,当我写关闭时,当时服务器进入循环并监听客户端的请求。因此,当我发出任何客户端请求时,它会关闭服务器。

但这并不好。我希望每当我写关闭时,它都不需要任何其他客户端请求。

帮助我使上面的代码正确。

4

2 回答 2

0

您必须调用Close()ServerSocket 的方法,但要在该线程之外。当您调用该方法时,将在 IOException 中抛出一个 IOException accept(),您将捕获该异常,然后调用break;以停止该循环。

     if(s.equalsIgnoreCase("shutdown")) {
                                ThreadPool.isStopped = true;
                                ClassListening.ServerSocket.close();
                                break;
        }

// Break the loop where the ServerSocket was listening

            try {
                        System.out.println("try to accept socket");
                        client_socket = serversocket.accept();
                    } catch(IOException ie) {
                        log.info(new Date() + " Server Stopped");
                        break;
                    }
于 2013-05-06T12:37:49.977 回答
0

serversocket closes套接字时,accept异常应该被捕获,SocketException如下所示,

WebServerShutdown 类

@Override
public void run() {
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = in.readLine()) != null) {
            System.out.println(s);
            if(s.equalsIgnoreCase("shutdown")) {
                ThreadPool.isStopped = true;
                break;
            }
        }
    } catch (IOException ex) {
        log.info(new Date() + " Problem in reading Input from keyboard");
    }
    System.out.println("Thread comes out");
    try {
        WebServer.serversocket.close();
    } catch(IOException ie) {
        log.info(new Date() + " Problem in closing socket");
    }
}

服务器监听的代码

     while( !(ThreadPool.isStopped) ) {
            Socket client_socket = null;
            try {
                client_socket = serversocket.accept();

            } catch(SocketException se) {
                log.info(new Date() + " WEBSERVER STOPPED");
            }

            if(ThreadPool.isStopped) break;
            ThreadPool.threads.execute(new Worker(client_socket));          
     }
于 2013-05-10T05:08:52.817 回答