1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.core.service;
21
22 import java.util.concurrent.Executor;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.atomic.AtomicInteger;
26
27 import org.apache.mina.core.RuntimeIoException;
28 import org.apache.mina.core.session.AbstractIoSession;
29 import org.apache.mina.core.session.AttributeKey;
30 import org.apache.mina.core.session.IoSession;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public class SimpleIoProcessorPool<T extends AbstractIoSession> implements IoProcessor<T> {
77
78 private static final int DEFAULT_SIZE = Runtime.getRuntime().availableProcessors() + 1;
79 private static final AttributeKey PROCESSOR = new AttributeKey(SimpleIoProcessorPool.class, "processor");
80
81 private final static Logger LOGGER = LoggerFactory.getLogger(SimpleIoProcessorPool.class);
82
83 private final IoProcessor<T>[] pool;
84 private final AtomicInteger processorDistributor = new AtomicInteger();
85 private final Executor executor;
86 private final boolean createdExecutor;
87
88 private final Object disposalLock = new Object();
89 private volatile boolean disposing;
90 private volatile boolean disposed;
91
92 public SimpleIoProcessorPool(Class<? extends IoProcessor<T>> processorType) {
93 this(processorType, null, DEFAULT_SIZE);
94 }
95
96 public SimpleIoProcessorPool(Class<? extends IoProcessor<T>> processorType, int size) {
97 this(processorType, null, size);
98 }
99
100 public SimpleIoProcessorPool(Class<? extends IoProcessor<T>> processorType, Executor executor) {
101 this(processorType, executor, DEFAULT_SIZE);
102 }
103
104 @SuppressWarnings("unchecked")
105 public SimpleIoProcessorPool(Class<? extends IoProcessor<T>> processorType, Executor executor, int size) {
106 if (processorType == null) {
107 throw new NullPointerException("processorType");
108 }
109 if (size <= 0) {
110 throw new IllegalArgumentException(
111 "size: " + size + " (expected: positive integer)");
112 }
113
114 if (executor == null) {
115 this.executor = executor = Executors.newCachedThreadPool();
116 this.createdExecutor = true;
117 } else {
118 this.executor = executor;
119 this.createdExecutor = false;
120 }
121
122 pool = new IoProcessor[size];
123
124 boolean success = false;
125 try {
126 for (int i = 0; i < pool.length; i ++) {
127 IoProcessor<T> processor = null;
128
129
130 try {
131 try {
132 processor = processorType.getConstructor(ExecutorService.class).newInstance(executor);
133 } catch (NoSuchMethodException e) {
134
135 }
136
137 if (processor == null) {
138 try {
139 processor = processorType.getConstructor(Executor.class).newInstance(executor);
140 } catch (NoSuchMethodException e) {
141
142 }
143 }
144
145 if (processor == null) {
146 try {
147 processor = processorType.getConstructor().newInstance();
148 } catch (NoSuchMethodException e) {
149
150 }
151 }
152 } catch (RuntimeException e) {
153 throw e;
154 } catch (Exception e) {
155 throw new RuntimeIoException(
156 "Failed to create a new instance of " + processorType.getName(), e);
157 }
158
159
160 if (processor == null) {
161 throw new IllegalArgumentException(
162 String.valueOf(processorType) + " must have a public constructor " +
163 "with one " + ExecutorService.class.getSimpleName() + " parameter, " +
164 "a public constructor with one " + Executor.class.getSimpleName() +
165 " parameter or a public default constructor.");
166 }
167
168 pool[i] = processor;
169 }
170
171 success = true;
172 } finally {
173 if (!success) {
174 dispose();
175 }
176 }
177 }
178
179 public final void add(T session) {
180 getProcessor(session).add(session);
181 }
182
183 public final void flush(T session) {
184 getProcessor(session).flush(session);
185 }
186
187 public final void remove(T session) {
188 getProcessor(session).remove(session);
189 }
190
191 public final void updateTrafficControl(T session) {
192 getProcessor(session).updateTrafficControl(session);
193 }
194
195 public boolean isDisposed() {
196 return disposed;
197 }
198
199 public boolean isDisposing() {
200 return disposing;
201 }
202
203 public final void dispose() {
204 if (disposed) {
205 return;
206 }
207
208 synchronized (disposalLock) {
209 if (!disposing) {
210 disposing = true;
211 for (int i = pool.length - 1; i >= 0; i --) {
212 if (pool[i] == null || pool[i].isDisposing()) {
213 continue;
214 }
215
216 try {
217 pool[i].dispose();
218 } catch (Exception e) {
219 LOGGER.warn(
220 "Failed to dispose a " +
221 pool[i].getClass().getSimpleName() +
222 " at index " + i + ".", e);
223 } finally {
224 pool[i] = null;
225 }
226 }
227
228 if (createdExecutor) {
229 ((ExecutorService) executor).shutdown();
230 }
231 }
232 }
233
234 disposed = true;
235 }
236
237 @SuppressWarnings("unchecked")
238 private IoProcessor<T> getProcessor(T session) {
239 IoProcessor<T> p = (IoProcessor<T>) session.getAttribute(PROCESSOR);
240 if (p == null) {
241 p = nextProcessor();
242 IoProcessor<T> oldp =
243 (IoProcessor<T>) session.setAttributeIfAbsent(PROCESSOR, p);
244 if (oldp != null) {
245 p = oldp;
246 }
247 }
248
249 return p;
250 }
251
252 private IoProcessor<T> nextProcessor() {
253 checkDisposal();
254 return pool[Math.abs(processorDistributor.getAndIncrement()) % pool.length];
255 }
256
257 private void checkDisposal() {
258 if (disposed) {
259 throw new IllegalStateException("A disposed processor cannot be accessed.");
260 }
261 }
262 }