Exemple #1
0
 private static HttpServer createHttpServer(ExecutorService execs) throws Exception {
   InetSocketAddress inetAddress = new InetSocketAddress(0);
   HttpServer testServer = HttpServer.create(inetAddress, 5);
   testServer.setExecutor(execs);
   HttpContext context = testServer.createContext("/test");
   context.setHandler(
       new HttpHandler() {
         public void handle(HttpExchange msg) {
           try {
             synchronized (lock) {
               ++s_received;
               if ((s_received % 1000) == 0) {
                 System.out.println("Received=" + s_received);
               }
             }
             String method = msg.getRequestMethod();
             if (method.equals("POST")) {
               InputStream is = msg.getRequestBody();
               byte[] buf = readFully(is);
               is.close();
               writePostReply(msg, buf);
             } else {
               System.out.println("****** METHOD not handled ***** " + method);
               System.out.println("Received=" + s_received);
             }
             synchronized (lock) {
               ++sent;
               if ((sent % 1000) == 0) {
                 System.out.println("sent=" + sent);
               }
             }
           } catch (Exception e) {
             e.printStackTrace();
           } finally {
             msg.close();
           }
         }
       });
   return testServer;
 }
 @Override
 public synchronized HttpContext createContext(String path, HttpHandler handler) {
   HttpContext context = createContext(path);
   context.setHandler(handler);
   return context;
 }