From 9a570bfe626eb32980f2bb37ce9467ab16f2d940 Mon Sep 17 00:00:00 2001 From: Dmitry Trofimov Date: Mon, 9 Jul 2012 17:07:32 +0200 Subject: [PATCH] Correct shutdown. --- .../org/apache/xmlrpc/IdeaAwareWebServer.java | 140 +++++++++++------- 1 file changed, 83 insertions(+), 57 deletions(-) diff --git a/platform/platform-impl/src/org/apache/xmlrpc/IdeaAwareWebServer.java b/platform/platform-impl/src/org/apache/xmlrpc/IdeaAwareWebServer.java index 6114d831a1a2..b27ebc89a227 100644 --- a/platform/platform-impl/src/org/apache/xmlrpc/IdeaAwareWebServer.java +++ b/platform/platform-impl/src/org/apache/xmlrpc/IdeaAwareWebServer.java @@ -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 runner 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 socket. * - * @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 runner back into {@link #threadpool}. - * - * @param runner The instance to reclaim. + * Delegates to con.run(). */ - 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 socket. - * - * @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 con.run(). - */ - 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 waitingForExecution = threadPool.shutdownNow(); + + for (Runnable r : waitingForExecution) { + if (r instanceof MyRunner) { + ((MyRunner)r).shutdown(); + } + } + } }