Exemplo n.º 1
0
 public NetAcceptor(int port) throws IOException {
   AsynchronousChannelGroup group =
       AsynchronousChannelGroup.withCachedThreadPool(
           Executors.newCachedThreadPool(), 1); // server连接接收线程池
   serverChannel = AsynchronousServerSocketChannel.open(group);
   serverChannel.bind(new InetSocketAddress(port));
 }
  private void init() {
    if (isInitialized) return;

    synchronized (this) {
      if (isInitialized) return;

      try {
        group =
            AsynchronousChannelGroup.withThreadPool(
                new ThreadPoolExecutor(
                    config.getAsynchronousCorePoolSize(),
                    config.getAsynchronousMaximumPoolSize(),
                    config.getAsynchronousPoolKeepAliveTime(),
                    TimeUnit.MILLISECONDS,
                    new LinkedTransferQueue<Runnable>(),
                    new ThreadFactory() {

                      @Override
                      public Thread newThread(Runnable r) {
                        return new Thread(r, "firefly asynchronous server thread");
                      }
                    }));
        log.info(
            "create asychronous I/O thread pool. core pool size: {}, max pool size: {}, pool keep alive time: {}ms",
            config.getAsynchronousCorePoolSize(),
            config.getAsynchronousMaximumPoolSize(),
            config.getAsynchronousPoolKeepAliveTime());
        EventManager eventManager = new DefaultEventManager(config);
        worker = new AsynchronousTcpWorker(config, eventManager);
      } catch (IOException e) {
        log.error("initialization server channel group error", e);
      }
      isInitialized = true;
    }
  }
Exemplo n.º 3
0
  private static AsynchronousChannelGroup createAsynchronousChannelGroup() {
    // Need to do this with the right thread context class loader else the
    // first web app to call this will trigger a leak
    ClassLoader original = Thread.currentThread().getContextClassLoader();

    try {
      Thread.currentThread().setContextClassLoader(AsyncIOThreadFactory.class.getClassLoader());

      // These are the same settings as the default
      // AsynchronousChannelGroup
      int initialSize = Runtime.getRuntime().availableProcessors();
      ExecutorService executorService =
          new ThreadPoolExecutor(
              0,
              Integer.MAX_VALUE,
              Long.MAX_VALUE,
              TimeUnit.MILLISECONDS,
              new SynchronousQueue<Runnable>(),
              new AsyncIOThreadFactory());

      try {
        return AsynchronousChannelGroup.withCachedThreadPool(executorService, initialSize);
      } catch (IOException e) {
        // No good reason for this to happen.
        throw new IllegalStateException(sm.getString("asyncChannelGroup.createFail"));
      }
    } finally {
      Thread.currentThread().setContextClassLoader(original);
    }
  }
Exemplo n.º 4
0
 public static void unregister() {
   synchronized (lock) {
     usageCount--;
     if (usageCount == 0) {
       group.shutdown();
       group = null;
     }
   }
 }
Exemplo n.º 5
0
  public AIOSocketMgr() throws Exception {
    if (!allowInstance) {
      throw new Exception();
    }
    try {
      AsynchronousChannelGroup resourceGroup =
          AsynchronousChannelGroup.withCachedThreadPool(Executors.newCachedThreadPool(), 8);
      server = AsynchronousServerSocketChannel.open(resourceGroup);
      server.bind(new InetSocketAddress(PORT), 100);

      acceptHandler = new AcceptCompletionHandler();
      readHandler = new ReadCompletionHandler();
      authHandler = new AuthSessionCompletionHandler();

      bindProtocol();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
 @Override
 public void shutdown() {
   if (group != null) group.shutdown();
 }