View Javadoc

    1   /*
    2    * Copyright 2012 The Netty Project
    3    *
    4    * The Netty Project licenses this file to you under the Apache License,
    5    * version 2.0 (the "License"); you may not use this file except in compliance
    6    * with the License. You may obtain a copy of the License at:
    7    *
    8    *   http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16
    17  package org.jboss.netty.channel.socket.nio;
    18
    19  import java.util.concurrent.Executor;
    20  import java.util.concurrent.atomic.AtomicInteger;
    21
    22  import org.jboss.netty.channel.Channel;
    23  import org.jboss.netty.channel.socket.Worker;
    24  import org.jboss.netty.util.ExternalResourceReleasable;
    25  import org.jboss.netty.util.internal.ExecutorUtil;
    26
    27  /**
    28   * Abstract base class for {@link WorkerPool} implementations that create the {@link Worker}'s
    29   * up-front and return them in a "fair" fashion when calling {@link #nextWorker()}
    30   */
    31  public abstract class AbstractNioWorkerPool<E extends AbstractNioWorker>
    32          implements WorkerPool<E>, ExternalResourceReleasable {
    33
    34      private final AbstractNioWorker[] workers;
    35      private final AtomicInteger workerIndex = new AtomicInteger();
    36      private final Executor workerExecutor;
    37      private final boolean allowShutDownOnIdle;
    38
    39      /**
    40       * Create a new instance
    41       *
    42       * @param workerExecutor the {@link Executor} to use for the {@link Worker}'s
    43       * @param allowShutdownOnIdle allow the {@link Worker}'s to shutdown when there is not
    44       *                            {@link Channel} is registered with it
    45       * @param workerCount the count of {@link Worker}'s to create
    46       * @deprecated use {@link #AbstractNioWorkerPool(Executor, int)}
    47       */
    48      AbstractNioWorkerPool(Executor workerExecutor, int workerCount, boolean allowShutDownOnIdle) {
    49          if (workerExecutor == null) {
    50              throw new NullPointerException("workerExecutor");
    51          }
    52          if (workerCount <= 0) {
    53              throw new IllegalArgumentException(
    54                      "workerCount (" + workerCount + ") " +
    55                      "must be a positive integer.");
    56          }
    57          workers = new AbstractNioWorker[workerCount];
    58
    59          for (int i = 0; i < workers.length; i++) {
    60              workers[i] = createWorker(workerExecutor);
    61          }
    62          this.allowShutDownOnIdle = allowShutDownOnIdle;
    63          this.workerExecutor = workerExecutor;
    64
    65      }
    66
    67      /**
    68       * Create a new instance
    69       *
    70       * @param workerExecutor the {@link Executor} to use for the {@link Worker}'s
    71       * @param workerCount the count of {@link Worker}'s to create
    72       */
    73      AbstractNioWorkerPool(Executor workerExecutor, int workerCount) {
    74          this(workerExecutor, workerCount, false);
    75      }
    76
    77      /**
    78       * Create a new {@link Worker} which uses the given {@link Executor} to service IO
    79       *
    80       *
    81       * @param executor the {@link Executor} to use
    82       * @param allowShutdownOnIdle allow the {@link Worker} to shutdown when there is not
    83       *                            {@link Channel} is registered with it
    84       * @return worker the new {@link Worker}
    85       * @deprecated use {@link #createWorker(Executor)}
    86       */
    87      @Deprecated
    88      protected abstract E createWorker(Executor executor, boolean allowShutdownOnIdle);
    89
    90      /**
    91       * Create a new {@link Worker} which uses the given {@link Executor} to service IO
    92       *
    93       *
    94       * @param executor the {@link Executor} to use
    95       * @return worker the new {@link Worker}
    96       */
    97      protected E createWorker(Executor executor) {
    98          return createWorker(executor, allowShutDownOnIdle);
    99      }
    100
    101     @SuppressWarnings("unchecked")
    102     public E nextWorker() {
    103         return (E) workers[Math.abs(workerIndex.getAndIncrement() % workers.length)];
    104     }
    105
    106
    107     public void releaseExternalResources() {
    108         ExecutorUtil.terminate(workerExecutor);
    109     }
    110
    111 }