views:

670

answers:

3

I have a server thread with this code:

public void run() {
    try {
        ServerSocket server;
        EneaLog.printLog("Server is running.");
        server = new ServerSocket(this.portnumber);

        while (true) {
            new EneaServerConnection(server.accept(), this.project,stopped).start();
            if (stopped) {
                EneaLog.printLog("Server safe-shutdown completed.");
                EneaLog.printLog("Hi!");
                server.close();
                return;
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(EneaServer.class.getName()).log(Level.SEVERE, null, ex);
        project.getExceptionHandler().handler(ex);
    }
}

and a shutdown method like this:

public void shutdown() {
    EneaLog.printLog("Server shutdown NOW!");
    stopped = true;
}

I want that shutdown can unblock thread that are waiting on server.accept() otherwise I must wait for connection before server shutdown.

I can't do server.close() in shutdown() because I must signal to registered client that server is coming down.

Any ideas?

A: 

Have you tried Thread.interrupt() ?

If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.

If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.

Brian Agnew
I've tried with this.interrupt(); in shutdown() but doesn't work...
THeK3nger
You need to call it on the thread you want to interrupt (i.e. the blocked one)
Brian Agnew
+1  A: 

I try to design my code so that it can be "shutdown" with an interrupt. Mainly, this is because the Executor framework in Java's concurrency package uses interrupt to cancel running tasks. Also, the "shutdown" task doesn't have to know any internals of the task being killed.

However, a call to accept will not respond to an interrupt unless it is created from a ServerSocketChannel. A server created with a ServerSocket constructor will ignore interrupts, and I haven't found a way to reconfigure this.

If you can't change the code that creates the server, arrange for another thread to call close on the server socket. This will also raise an exception in thread blocked on accept, regardless of the method used to create the server socket.

This turns out to be a really big pain when using SSL. A JSSE socket is not created from an InterruptibleChannel, and won't respond to a simple interrupt on the thread.


I just noticed that the question says that the server can't be closed without notifying the client. Successfully interrupting a socket results in its closure.

On a call to accept this shouldn't be a problem, since the client is not connected if the server socket is blocked in accept. That should only be an issue for Socket instances, that represent current connections.

If that doesn't satisfy the notification requirements, a rework to use NIO's ServerSocketChannel in non-blocking mode may be necessary.

erickson
mmm good answer.. Maybe I change approach to problem. I manage server shutdown in client-side with try-catch.
THeK3nger
A: 

You should be able to close the socket from another thread.

Tom Hawtin - tackline