Пример #1
0
  public static ServerAddress startThreadPoolServer(
      InetSocketAddress address,
      TProcessor processor,
      String serverName,
      String threadName,
      int numThreads)
      throws TTransportException {

    // if port is zero, then we must bind to get the port number
    ServerSocket sock;
    try {
      sock = ServerSocketChannel.open().socket();
      sock.setReuseAddress(true);
      sock.bind(address);
      address = new InetSocketAddress(address.getHostName(), sock.getLocalPort());
    } catch (IOException ex) {
      throw new TTransportException(ex);
    }
    TServerTransport transport = new TBufferedServerSocket(sock, 32 * 1024);
    TThreadPoolServer.Args options = new TThreadPoolServer.Args(transport);
    options.protocolFactory(ThriftUtil.protocolFactory());
    options.transportFactory(ThriftUtil.transportFactory());
    options.processorFactory(new ClientInfoProcessorFactory(processor));
    return new ServerAddress(new TThreadPoolServer(options), address);
  }
Пример #2
0
 public static ServerAddress startHsHaServer(
     InetSocketAddress address,
     TProcessor processor,
     final String serverName,
     String threadName,
     final int numThreads,
     long timeBetweenThreadChecks,
     long maxMessageSize)
     throws TTransportException {
   TNonblockingServerSocket transport = new TNonblockingServerSocket(address);
   // check for the special "bind to everything address"
   if (address.getAddress().getHostAddress().equals("0.0.0.0")) {
     // can't get the address from the bind, so we'll do our best to invent our hostname
     try {
       address =
           new InetSocketAddress(InetAddress.getLocalHost().getHostName(), address.getPort());
     } catch (UnknownHostException e) {
       throw new TTransportException(e);
     }
   }
   THsHaServer.Args options = new THsHaServer.Args(transport);
   options.protocolFactory(ThriftUtil.protocolFactory());
   options.transportFactory(ThriftUtil.transportFactory(maxMessageSize));
   options.stopTimeoutVal(5);
   /*
    * Create our own very special thread pool.
    */
   final ThreadPoolExecutor pool = new SimpleThreadPool(numThreads, "ClientPool");
   // periodically adjust the number of threads we need by checking how busy our threads are
   SimpleTimer.getInstance()
       .schedule(
           new Runnable() {
             @Override
             public void run() {
               if (pool.getCorePoolSize() <= pool.getActiveCount()) {
                 int larger = pool.getCorePoolSize() + Math.min(pool.getQueue().size(), 2);
                 log.info("Increasing server thread pool size on " + serverName + " to " + larger);
                 pool.setMaximumPoolSize(larger);
                 pool.setCorePoolSize(larger);
               } else {
                 if (pool.getCorePoolSize() > pool.getActiveCount() + 3) {
                   int smaller = Math.max(numThreads, pool.getCorePoolSize() - 1);
                   if (smaller != pool.getCorePoolSize()) {
                     // there is a race condition here... the active count could be higher by the
                     // time
                     // we decrease the core pool size... so the active count could end up higher
                     // than
                     // the core pool size, in which case everything will be queued... the increase
                     // case
                     // should handle this and prevent deadlock
                     log.info(
                         "Decreasing server thread pool size on " + serverName + " to " + smaller);
                     pool.setCorePoolSize(smaller);
                   }
                 }
               }
             }
           },
           timeBetweenThreadChecks,
           timeBetweenThreadChecks);
   options.executorService(pool);
   options.processorFactory(new TProcessorFactory(processor));
   return new ServerAddress(new THsHaServer(options), address);
 }