2012-07-11 33 views
1

我一直在玩Java Java httpServer類。當我運行我的測試應用程序時,它將以正確的方式到達最後一行(一個println),但在關閉應用程序之前它會在那裏停留大約45秒。它爲什麼這樣做,是否有辦法讓它更快結束?由於帶有httpServer的Java應用程序在退出前掛起45秒

public class TestTester 
{ 
    public static void main(String[] args) throws IOException { 
     InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 8780); 
     HttpServer server = HttpServer.create(addr, 0); 
     server.createContext("/", new MyHandler()); 
     server.setExecutor(Executors.newCachedThreadPool()); 
     server.start(); 


     URL url = new URL("http://localhost:8780/test"); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setRequestMethod("GET"); 
     InputStreamReader(connection.getInputStream())); 

     // print out the response headers 
     System.out.println("\nHttp Response Headers"); 
     for (int i=0; ; i++) 
     { 
      String headerName = connection.getHeaderFieldKey(i); 
      String headerValue = connection.getHeaderField(i); 

      if (headerName == null && headerValue == null) { 
       // No more headers 
       break; 
      } 
      else if (headerName == null) { 
       System.out.println(" " + headerValue); 
      } 
      else 
       System.out.println(" " + headerName + ": " + headerValue); 
     } 
     connection.disconnect(); 

     server.stop(0); 
     System.out.println("Stopped Server."); 

    } // end main() 
} 

class MyHandler implements HttpHandler { 

    @Override 
    public void handle(HttpExchange exchange) throws IOException 
    { 
     System.out.println("===Enter MyHandler==="); 
     System.out.println("Http Request Header:"); 
     // print out the request methode and url 
     System.out.println(" " + exchange.getRequestMethod() + " " 
       + exchange.getRequestURI() + " " + exchange.getProtocol()); 
     // print out the request headers 
     Headers requestHeaders = exchange.getRequestHeaders(); 
     for (String name : requestHeaders.keySet()) 
     { 
      List<String> values = requestHeaders.get(name); 
      for (String value : values) 
      { 
       System.out.println(" " + name + ": " + value); 
      } 
     } 
     // print out the request body if any 
     BufferedReader in = new BufferedReader(new InputStreamReader(exchange.getRequestBody())); 
     String sCurrentLine; 
     System.out.println("\nHttp Request Body:"); 
     if (! in.ready()) 
      System.out.println(" No Request Body"); 
     else 
     { 
      while ((sCurrentLine = in.readLine()) != null) { 
       System.out.println(" " + sCurrentLine); 
      } 
     } 

     // set up and send response 
     String requestMethod = exchange.getRequestMethod(); 
     if (requestMethod.equalsIgnoreCase("GET")) 
     { 
      Headers responseHeaders = exchange.getResponseHeaders(); 
      responseHeaders.set("Content-Type", "text/html"); 
      exchange.sendResponseHeaders(200, 0); 

      OutputStream responseBody = exchange.getResponseBody(); 
      responseBody.write("<!DOCTYPE html><html><body><h1>My Response Header</h1><p>And some sample data.</p></body></html>".getBytes()); 

      responseBody.close(); 
     } 
     exchange.close(); 
    } // end public void handle() 
} // end class 
+0

可能有事情做與緩存的線程池。它有一箇中斷方法嗎?你有沒有看過Apache HttpUtils,或者JBoss Netty或Apache MINA?另外,你可以使用像XTend這樣的解釋語言編寫你的服務器。 – djangofan 2012-07-12 00:20:04

+1

@djangofan你是對的。我註釋了設置服務器執行程序的行,並立即退出。我不知道你打算用中斷的方式是什麼意思,所以我可能不會。似乎應該有一些方法來停止所有線程並退出。 – user1404617 2012-07-12 01:05:49

+0

是的,這就是所謂的中斷。如果你創建一個線程池,你應該可以通過某種.interrupt()方法/調用來關閉整個池。 – djangofan 2012-07-12 22:14:41

回答

0

我沒有找到的東西,似乎工作,雖然我不知道這是否是正確的方式來處理這個。這不是廣泛測試,但它確實使它關機更快。

我代替:

server.setExecutor(Executors.newCachedThreadPool()); 

有了:

ExecutorService excu = Executors.newCachedThreadPool(); 
server.setExecutor(excu); 

然後就在server.stop(0);之前我加excu.shutdown();