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.socket.Worker;
23  import org.jboss.netty.util.ExternalResourceReleasable;
24  import org.jboss.netty.util.internal.ExecutorUtil;
25  
26  /**
27   * Abstract base class for {@link WorkerPool} implementations that create the {@link Worker}'s
28   * up-front and return them in a "fair" fashion when calling {@link #nextWorker()}
29   */
30  public abstract class AbstractNioWorkerPool<E extends AbstractNioWorker>
31          implements WorkerPool<E>, ExternalResourceReleasable {
32  
33      private final AbstractNioWorker[] workers;
34      private final AtomicInteger workerIndex = new AtomicInteger();
35      private final Executor workerExecutor;
36  
37  
38      /**
39       * Create a new instance
40       *
41       * @param workerExecutor the {@link Executor} to use for the {@link Worker}'s
42       * @param workerCount the count of {@link Worker}'s to create
43       */
44      AbstractNioWorkerPool(Executor workerExecutor, int workerCount) {
45          if (workerExecutor == null) {
46              throw new NullPointerException("workerExecutor");
47          }
48          if (workerCount <= 0) {
49              throw new IllegalArgumentException(
50                      "workerCount (" + workerCount + ") " +
51                      "must be a positive integer.");
52          }
53          workers = new AbstractNioWorker[workerCount];
54  
55          for (int i = 0; i < workers.length; i++) {
56              workers[i] = createWorker(workerExecutor);
57          }
58          this.workerExecutor = workerExecutor;
59      }
60  
61      /**
62       * Create a new {@link Worker} which uses the given {@link Executor} to service IO
63       *
64       *
65       * @param executor the {@link Executor} to use
66       * @return worker the new {@link Worker}
67       */
68      protected abstract E createWorker(Executor executor);
69  
70      @SuppressWarnings("unchecked")
71      public E nextWorker() {
72          return (E) workers[Math.abs(workerIndex.getAndIncrement() % workers.length)];
73      }
74  
75  
76      public void releaseExternalResources() {
77          ExecutorUtil.terminate(workerExecutor);
78          for (AbstractNioWorker worker: workers) {
79              worker.releaseExternalResources();
80          }
81      }
82  
83  }