Correct shutdown.

This commit is contained in:
Dmitry Trofimov
2012-07-09 17:07:32 +02:00
parent 02e4e932db
commit 9a570bfe62
@@ -18,6 +18,7 @@ package org.apache.xmlrpc;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
@@ -27,74 +28,99 @@ import java.util.concurrent.ThreadFactory;
*
* @author Maxim.Mossienko
*/
public class IdeaAwareWebServer extends WebServer
{
private static final ExecutorService threadPool = Executors.newFixedThreadPool(2, new ThreadFactory() {
public Thread newThread(final Runnable r) {
return new Thread(r, "WebServer thread pool");
}
});
public class IdeaAwareWebServer extends WebServer {
private static final ExecutorService threadPool = Executors.newFixedThreadPool(2, new ThreadFactory() {
public Thread newThread(final Runnable r) {
return new Thread(r, "WebServer thread pool");
}
});
/**
* Creates a web server at the specified port number and IP
* address.
*/
public IdeaAwareWebServer(int port, InetAddress addr, XmlRpcServer xmlrpc)
{
super(port, addr, xmlrpc);
}
* Creates a web server at the specified port number and IP
* address.
*/
public IdeaAwareWebServer(int port, InetAddress addr, XmlRpcServer xmlrpc) {
super(port, addr, xmlrpc);
}
/**
* @return
*/
protected Runner getRunner() {
return new MyRunner();
}
/**
* Put <code>runner</code> back into {@link #threadpool}.
*
* @param runner The instance to reclaim.
*/
void repoolRunner(Runner runner) {
}
/**
* Responsible for handling client connections.
*/
class MyRunner extends Runner {
private Socket mySocket;
/**
* Handles the client connection on <code>socket</code>.
*
* @return
* @param socket The source to read the client's request from.
*/
protected Runner getRunner()
{
return new MyRunner();
public synchronized void handle(Socket socket) throws IOException {
mySocket = socket;
con = new Connection(socket);
count = 0;
try {
threadPool.submit(this);
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* Put <code>runner</code> back into {@link #threadpool}.
*
* @param runner The instance to reclaim.
* Delegates to <code>con.run()</code>.
*/
void repoolRunner(Runner runner)
{
public void run() {
try {
con.run();
}
finally {
Thread.interrupted(); // reset interrupted status
}
}
/**
* Responsible for handling client connections.
*/
class MyRunner extends Runner
{
/**
* Handles the client connection on <code>socket</code>.
*
* @param socket The source to read the client's request from.
*/
public synchronized void handle(Socket socket) throws IOException
{
con = new Connection(socket);
count = 0;
try {
threadPool.submit(this);
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* Delegates to <code>con.run()</code>.
*/
public void run()
{
try {
con.run();
} finally {
Thread.interrupted(); // reset interrupted status
}
}
public void shutdown() {
try {
mySocket.close();
}
catch (IOException e) {
//pass
}
}
}
@Override
public void shutdown() {
super.shutdown();
try {
serverSocket.close();
}
catch (IOException e) {
//pass
}
List<Runnable> waitingForExecution = threadPool.shutdownNow();
for (Runnable r : waitingForExecution) {
if (r instanceof MyRunner) {
((MyRunner)r).shutdown();
}
}
}
}