1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.jboss.netty.channel.socket.nio;
18
19 import org.jboss.netty.channel.socket.Worker;
20 import org.jboss.netty.util.ExternalResourceReleasable;
21 import org.jboss.netty.util.internal.ExecutorUtil;
22
23 import java.util.concurrent.Executor;
24 import java.util.concurrent.atomic.AtomicInteger;
25
26
27
28
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 private volatile boolean initDone;
37
38
39
40
41
42
43
44 AbstractNioWorkerPool(Executor workerExecutor, int workerCount) {
45 this(workerExecutor, workerCount, true);
46 }
47
48 AbstractNioWorkerPool(Executor workerExecutor, int workerCount, boolean autoInit) {
49 if (workerExecutor == null) {
50 throw new NullPointerException("workerExecutor");
51 }
52 if (workerCount <= 0) {
53 throw new IllegalArgumentException(
54 "workerCount (" + workerCount + ") " + "must be a positive integer.");
55 }
56 workers = new AbstractNioWorker[workerCount];
57 this.workerExecutor = workerExecutor;
58 if (autoInit) {
59 init();
60 }
61 }
62 protected void init() {
63 if (initDone) {
64 throw new IllegalStateException("Init was done before");
65 }
66 initDone = true;
67
68 for (int i = 0; i < workers.length; i++) {
69 workers[i] = newWorker(workerExecutor);
70 }
71 }
72
73
74
75
76
77
78
79
80
81
82 @Deprecated
83 protected E createWorker(Executor executor) {
84 throw new IllegalStateException("This will be removed. Override this and the newWorker(..) method!");
85 }
86
87
88
89
90
91
92
93
94
95
96
97 @SuppressWarnings("deprecation")
98 protected E newWorker(Executor executor) {
99 return createWorker(executor);
100 }
101
102 @SuppressWarnings("unchecked")
103 public E nextWorker() {
104 return (E) workers[Math.abs(workerIndex.getAndIncrement() % workers.length)];
105 }
106
107 public void rebuildSelectors() {
108 for (AbstractNioWorker worker: workers) {
109 worker.rebuildSelector();
110 }
111 }
112
113 public void releaseExternalResources() {
114 shutdown();
115 ExecutorUtil.shutdownNow(workerExecutor);
116 }
117
118 public void shutdown() {
119 for (AbstractNioWorker worker: workers) {
120 worker.shutdown();
121 }
122 }
123
124 }