示例#1
0
    public ListenerThread(ApiServer requestHandler, int port) {
      try {
        _serverSocket = new ServerSocket(port);
      } catch (IOException ioex) {
        s_logger.error("error initializing api server", ioex);
        return;
      }

      _params = new BasicHttpParams();
      _params
          .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 30000)
          .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
          .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
          .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
          .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpComponents/1.1");

      // Set up the HTTP protocol processor
      BasicHttpProcessor httpproc = new BasicHttpProcessor();
      httpproc.addInterceptor(new ResponseDate());
      httpproc.addInterceptor(new ResponseServer());
      httpproc.addInterceptor(new ResponseContent());
      httpproc.addInterceptor(new ResponseConnControl());

      // Set up request handlers
      HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
      reqistry.register("*", requestHandler);

      // Set up the HTTP service
      _httpService =
          new HttpService(
              httpproc, new NoConnectionReuseStrategy(), new DefaultHttpResponseFactory());
      _httpService.setParams(_params);
      _httpService.setHandlerResolver(reqistry);
    }
    protected void accept() throws IOException {
      // Set up HTTP connection
      Socket socket = servicedSocket.accept();
      acceptedConnections.incrementAndGet();
      DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
      conn.bind(socket, serverParams);

      // Set up the HTTP service
      HttpService httpService =
          new HttpService(httpProcessor, reuseStrategy, new DefaultHttpResponseFactory());
      httpService.setParams(serverParams);
      httpService.setHandlerResolver(handlerRegistry);

      // Start worker thread
      Thread t = new Thread(new Worker(httpService, conn));
      workerThreads.add(t);
      t.setDaemon(true);
      t.start();
    } // accept