Skip to content

Package: NIOTransportBuilder

NIOTransportBuilder

nameinstructionbranchcomplexitylinemethod
NIOTransportBuilder(Class)
M: 57 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 18 C: 0
0%
M: 1 C: 0
0%
build()
M: 103 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 25 C: 0
0%
M: 1 C: 0
0%
getAttributeBuilder()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getClientSocketSoTimeout()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getConnectionTimeout()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getIOStrategy()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMaxAsyncWriteQueueSizeInBytes()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMemoryManager()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getNIOChannelDistributor()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getName()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getProcessor()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getProcessorSelector()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getReadBufferSize()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getReadTimeout(TimeUnit)
M: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getSelectionKeyHandler()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getSelectorHandler()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getSelectorProvider()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getSelectorRunnersCount()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getSelectorThreadPoolConfig()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getWorkerThreadPoolConfig()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getWriteBufferSize()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getWriteTimeout(TimeUnit)
M: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
isOptimizedForMultiplexing()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isReuseAddress()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setAttributeBuilder(AttributeBuilder)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setClientSocketSoTimeout(int)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setConnectionTimeout(int)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setIOStrategy(IOStrategy)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setMaxAsyncWriteQueueSizeInBytes(int)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setMemoryManager(MemoryManager)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setNIOChannelDistributor(NIOChannelDistributor)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setName(String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setOptimizedForMultiplexing(boolean)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setProcessor(Processor)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setProcessorSelector(ProcessorSelector)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setReadBufferSize(int)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setReadTimeout(long, TimeUnit)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
setReuseAddress(boolean)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setSelectionKeyHandler(SelectionKeyHandler)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setSelectorHandler(SelectorHandler)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setSelectorProvider(SelectorProvider)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setSelectorRunnersCount(int)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setSelectorThreadPoolConfig(ThreadPoolConfig)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setWorkerThreadPoolConfig(ThreadPoolConfig)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setWriteBufferSize(int)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setWriteTimeout(long, TimeUnit)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2011, 2020 Oracle and/or its affiliates. All rights reserved.
3: * Copyright (c) 2018 Payara Services Ltd.
4: *
5: * This program and the accompanying materials are made available under the
6: * terms of the Eclipse Public License v. 2.0, which is available at
7: * http://www.eclipse.org/legal/epl-2.0.
8: *
9: * This Source Code may also be made available under the following Secondary
10: * Licenses when the conditions for such availability set forth in the
11: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
12: * version 2 with the GNU Classpath Exception, which is available at
13: * https://www.gnu.org/software/classpath/license.html.
14: *
15: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16: */
17:
18: package org.glassfish.grizzly;
19:
20: import java.nio.channels.spi.SelectorProvider;
21: import java.util.concurrent.TimeUnit;
22:
23: import org.glassfish.grizzly.asyncqueue.AsyncQueueWriter;
24: import org.glassfish.grizzly.attributes.AttributeBuilder;
25: import org.glassfish.grizzly.memory.MemoryManager;
26: import org.glassfish.grizzly.nio.NIOChannelDistributor;
27: import org.glassfish.grizzly.nio.NIOTransport;
28: import org.glassfish.grizzly.nio.SelectionKeyHandler;
29: import org.glassfish.grizzly.nio.SelectorHandler;
30: import org.glassfish.grizzly.strategies.WorkerThreadIOStrategy;
31: import org.glassfish.grizzly.threadpool.ThreadPoolConfig;
32:
33: /**
34: * This builder is responsible for creating {@link NIOTransport} implementations as well as providing basic
35: * configuration for <code>IOStrategies</code> and thread pools.
36: *
37: * @see NIOTransport
38: * @see IOStrategy
39: * @see ThreadPoolConfig
40: *
41: * @since 2.0
42: */
43: @SuppressWarnings("UnusedDeclaration")
44: public abstract class NIOTransportBuilder<T extends NIOTransportBuilder> {
45:
46: protected final Class<? extends NIOTransport> transportClass;
47: protected ThreadPoolConfig workerConfig;
48: protected ThreadPoolConfig kernelConfig;
49: protected SelectorProvider selectorProvider;
50: protected SelectorHandler selectorHandler = SelectorHandler.DEFAULT_SELECTOR_HANDLER;
51: protected SelectionKeyHandler selectionKeyHandler = SelectionKeyHandler.DEFAULT_SELECTION_KEY_HANDLER;
52: protected MemoryManager memoryManager = MemoryManager.DEFAULT_MEMORY_MANAGER;
53: protected AttributeBuilder attributeBuilder = AttributeBuilder.DEFAULT_ATTRIBUTE_BUILDER;
54: protected IOStrategy ioStrategy = WorkerThreadIOStrategy.getInstance();
55: protected int selectorRunnerCount = NIOTransport.DEFAULT_SELECTOR_RUNNER_COUNT;
56: protected NIOChannelDistributor nioChannelDistributor;
57: protected String name;
58: protected Processor processor;
59: protected ProcessorSelector processorSelector;
60: protected int readBufferSize = Transport.DEFAULT_READ_BUFFER_SIZE;
61: protected int writeBufferSize = Transport.DEFAULT_WRITE_BUFFER_SIZE;
62: protected int clientSocketSoTimeout = NIOTransport.DEFAULT_CLIENT_SOCKET_SO_TIMEOUT;
63: protected int connectionTimeout = NIOTransport.DEFAULT_CONNECTION_TIMEOUT;
64: protected boolean reuseAddress = NIOTransport.DEFAULT_REUSE_ADDRESS;
65: protected int maxPendingBytesPerConnection = AsyncQueueWriter.AUTO_SIZE;
66: protected boolean optimizedForMultiplexing = NIOTransport.DEFAULT_OPTIMIZED_FOR_MULTIPLEXING;
67:
68: protected long readTimeout = TimeUnit.MILLISECONDS.convert(Transport.DEFAULT_READ_TIMEOUT, TimeUnit.SECONDS);
69: protected long writeTimeout = TimeUnit.MILLISECONDS.convert(Transport.DEFAULT_WRITE_TIMEOUT, TimeUnit.SECONDS);
70:
71: // ------------------------------------------------------------ Constructors
72:
73: /**
74: * <p>
75: * Constructs a new <code>NIOTransport</code> using the given <code>transportClass</code> and {@link IOStrategy}.
76: * </p>
77: *
78: * <p>
79: * The builder's worker thread pool configuration will be based on the return value of
80: * {@link IOStrategy#createDefaultWorkerPoolConfig(Transport)}. If worker thread configuration is non-null, the initial
81: * selector thread pool configuration will be cloned from it, otherwise a default configuration will be chosen.
82: * </p>
83: *
84: * @param transportClass the class of the {@link NIOTransport} implementation to be used.
85: */
86: protected NIOTransportBuilder(final Class<? extends NIOTransport> transportClass) {
87:
88: this.transportClass = transportClass;
89:
90: }
91:
92: // ---------------------------------------------------------- Public Methods
93:
94: /**
95: * @return the number of {@link java.nio.channels.Selector}s to be created to serve Transport connections. <tt>-1</tt>
96: * is the default value, which lets the Transport to pick the value, usually it's equal to the number of CPU cores
97: * {@link Runtime#availableProcessors()}
98: */
99: public int getSelectorRunnersCount() {
100: return selectorRunnerCount;
101: }
102:
103: /**
104: * Sets the number of {@link java.nio.channels.Selector}s to be created to serve Transport connections. <tt>-1</tt> is
105: * the default value, which lets the Transport to pick the value, usually it's equal to the number of CPU cores
106: * {@link Runtime#availableProcessors()}.
107: *
108: * @param selectorRunnersCount number of channels
109: * @return the builder
110: */
111: public T setSelectorRunnersCount(final int selectorRunnersCount) {
112: this.selectorRunnerCount = selectorRunnersCount;
113: return getThis();
114: }
115:
116: /**
117: * @return the {@link ThreadPoolConfig} that will be used to construct the {@link java.util.concurrent.ExecutorService}
118: * for <code>IOStrategies</code> that require worker threads. This method will return <code>null</code> if a
119: * {@link ThreadPoolConfig} had not been previously set.
120: */
121: public ThreadPoolConfig getWorkerThreadPoolConfig() {
122: return workerConfig;
123: }
124:
125: /**
126: * Sets the {@link ThreadPoolConfig} that will be used to construct the {@link java.util.concurrent.ExecutorService} for
127: * <code>IOStrategies</code> that require worker threads
128: *
129: * @param workerConfig the config
130: * @return this builder
131: */
132: public T setWorkerThreadPoolConfig(final ThreadPoolConfig workerConfig) {
133: this.workerConfig = workerConfig;
134: return getThis();
135: }
136:
137: /**
138: * @return the {@link ThreadPoolConfig} that will be used to construct the {@link java.util.concurrent.ExecutorService}
139: * which will run the {@link NIOTransport}'s {@link org.glassfish.grizzly.nio.SelectorRunner}s.
140: */
141: public ThreadPoolConfig getSelectorThreadPoolConfig() {
142: return kernelConfig;
143: }
144:
145: /**
146: * Sets the {@link ThreadPoolConfig} that will be used to construct the {@link java.util.concurrent.ExecutorService}
147: * which will run the {@link NIOTransport}'s {@link org.glassfish.grizzly.nio.SelectorRunner}s.
148: *
149: * @param kernelConfig the config
150: * @return this builder
151: */
152: public T setSelectorThreadPoolConfig(final ThreadPoolConfig kernelConfig) {
153: this.kernelConfig = kernelConfig;
154: return getThis();
155: }
156:
157: /**
158: * @return the {@link IOStrategy} that will be used by the created {@link NIOTransport}.
159: */
160: public IOStrategy getIOStrategy() {
161: return ioStrategy;
162: }
163:
164: /**
165: * <p>
166: * Changes the {@link IOStrategy} that will be used. Invoking this method may change the return value of
167: * {@link #getWorkerThreadPoolConfig()}
168: *
169: * @param ioStrategy the {@link IOStrategy} to use.
170: *
171: * @return this <code>NIOTransportBuilder</code>
172: */
173: public T setIOStrategy(final IOStrategy ioStrategy) {
174: this.ioStrategy = ioStrategy;
175: return getThis();
176: }
177:
178: /**
179: * @return the {@link MemoryManager} that will be used by the created {@link NIOTransport}. If not explicitly set, then
180: * {@link MemoryManager#DEFAULT_MEMORY_MANAGER} will be used.
181: */
182: public MemoryManager getMemoryManager() {
183: return memoryManager;
184: }
185:
186: /**
187: * Set the {@link MemoryManager} to be used by the created {@link NIOTransport}.
188: *
189: * @param memoryManager the {@link MemoryManager}.
190: *
191: * @return this <code>NIOTransportBuilder</code>
192: */
193: public T setMemoryManager(final MemoryManager memoryManager) {
194: this.memoryManager = memoryManager;
195: return getThis();
196: }
197:
198: /**
199: * @return the {@link SelectorHandler} that will be used by the created {@link NIOTransport}. If not explicitly set,
200: * then {@link SelectorHandler#DEFAULT_SELECTOR_HANDLER} will be used.
201: */
202: public SelectorHandler getSelectorHandler() {
203: return selectorHandler;
204: }
205:
206: /**
207: * Set the {@link SelectorHandler} to be used by the created {@link NIOTransport}.
208: *
209: * @param selectorHandler the {@link SelectorHandler}.
210: *
211: * @return this <code>NIOTransportBuilder</code>
212: */
213: public T setSelectorHandler(final SelectorHandler selectorHandler) {
214: this.selectorHandler = selectorHandler;
215: return getThis();
216: }
217:
218: /**
219: * @return the {@link SelectionKeyHandler} that will be used by the created {@link NIOTransport}. If not explicitly set,
220: * then {@link SelectionKeyHandler#DEFAULT_SELECTION_KEY_HANDLER} will be used.
221: */
222: public SelectionKeyHandler getSelectionKeyHandler() {
223: return selectionKeyHandler;
224: }
225:
226: /**
227: * Set the {@link SelectionKeyHandler} to be used by the created {@link NIOTransport}.
228: *
229: * @param selectionKeyHandler the {@link SelectionKeyHandler}.
230: *
231: * @return this <code>NIOTransportBuilder</code>
232: */
233: public T setSelectionKeyHandler(final SelectionKeyHandler selectionKeyHandler) {
234: this.selectionKeyHandler = selectionKeyHandler;
235: return getThis();
236: }
237:
238: /**
239: * @return the {@link AttributeBuilder} that will be used by the created {@link NIOTransport}. If not explicitly set,
240: * then {@link AttributeBuilder#DEFAULT_ATTRIBUTE_BUILDER} will be used.
241: */
242: public AttributeBuilder getAttributeBuilder() {
243: return attributeBuilder;
244: }
245:
246: /**
247: * Set the {@link AttributeBuilder} to be used by the created {@link NIOTransport}.
248: *
249: * @param attributeBuilder the {@link AttributeBuilder}.
250: *
251: * @return this <code>NIOTransportBuilder</code>
252: */
253: public T setAttributeBuilder(AttributeBuilder attributeBuilder) {
254: this.attributeBuilder = attributeBuilder;
255: return getThis();
256: }
257:
258: /**
259: * @return the {@link NIOChannelDistributor} that will be used by the created {@link NIOTransport}. If not explicitly
260: * set, then {@link AttributeBuilder#DEFAULT_ATTRIBUTE_BUILDER} will be used.
261: */
262: public NIOChannelDistributor getNIOChannelDistributor() {
263: return nioChannelDistributor;
264: }
265:
266: /**
267: * Set the {@link NIOChannelDistributor} to be used by the created {@link NIOTransport}.
268: *
269: * @param nioChannelDistributor the {@link NIOChannelDistributor}.
270: *
271: * @return this <code>NIOTransportBuilder</code>
272: */
273: public T setNIOChannelDistributor(NIOChannelDistributor nioChannelDistributor) {
274: this.nioChannelDistributor = nioChannelDistributor;
275: return getThis();
276: }
277:
278: /**
279: * @return the {@link SelectorProvider} that will be used by the created {@link NIOTransport}. If not explicitly set,
280: * then {@link SelectorProvider#provider()} will be used.
281: */
282: public SelectorProvider getSelectorProvider() {
283: return selectorProvider;
284: }
285:
286: /**
287: * Set the {@link SelectorProvider} to be used by the created {@link NIOTransport}.
288: *
289: * @param selectorProvider the {@link SelectorProvider}.
290: *
291: * @return this <code>NIOTransportBuilder</code>
292: */
293: public T setSelectorProvider(SelectorProvider selectorProvider) {
294: this.selectorProvider = selectorProvider;
295: return getThis();
296: }
297:
298: /**
299: * @return the Transport name
300: * @see Transport#getName()
301: */
302: public String getName() {
303: return name;
304: }
305:
306: /**
307: * @see Transport#setName(String)
308: * @param name the {@link Transport} name
309: * @return this <code>NIOTransportBuilder</code>
310: */
311: public T setName(String name) {
312: this.name = name;
313: return getThis();
314: }
315:
316: /**
317: * @return the default {@link Processor} if a {@link Connection} does not specify a preference
318: * @see Transport#getProcessor()
319: */
320: public Processor getProcessor() {
321: return processor;
322: }
323:
324: /**
325: * @param processor the default {@link Processor} if a {@link Connection} does not specify a preference
326: * @see Transport#setProcessor(Processor)
327: *
328: * @return this <code>NIOTransportBuilder</code>
329: */
330: public T setProcessor(Processor processor) {
331: this.processor = processor;
332: return getThis();
333: }
334:
335: /**
336: * @return the default {@link ProcessorSelector}
337: * @see Transport#getProcessorSelector()
338: */
339: public ProcessorSelector getProcessorSelector() {
340: return processorSelector;
341: }
342:
343: /**
344: * @see Transport#setProcessorSelector(ProcessorSelector)
345: * @param processorSelector the default {@link ProcessorSelector}
346: * @return this <code>NIOTransportBuilder</code>
347: */
348: public T setProcessorSelector(ProcessorSelector processorSelector) {
349: this.processorSelector = processorSelector;
350: return getThis();
351: }
352:
353: /**
354: * @return the default buffer size
355: * @see Transport#getReadBufferSize()
356: */
357: public int getReadBufferSize() {
358: return readBufferSize;
359: }
360:
361: /**
362: * @see Transport#setReadBufferSize(int)
363: * @param readBufferSize the new buffer size
364: * @return this <code>NIOTransportBuilder</code>
365: */
366: public T setReadBufferSize(int readBufferSize) {
367: this.readBufferSize = readBufferSize;
368: return getThis();
369: }
370:
371: /**
372: * @return the default buffer size
373: * @see Transport#getWriteBufferSize()
374: */
375: public int getWriteBufferSize() {
376: return writeBufferSize;
377: }
378:
379: /**
380: * @see Transport#setWriteBufferSize(int)
381: * @param writeBufferSize the new write buffer size
382: * @return this <code>NIOTransportBuilder</code>
383: */
384: public T setWriteBufferSize(int writeBufferSize) {
385: this.writeBufferSize = writeBufferSize;
386: return getThis();
387: }
388:
389: /**
390: * @return gets the timeout on socket blocking operations on the client
391: * @see java.net.Socket#getSoTimeout()
392: */
393: public int getClientSocketSoTimeout() {
394: return clientSocketSoTimeout;
395: }
396:
397: /**
398: * Sets the timeout on socket blocking operations for the client
399: *
400: * @param clientSocketSoTimeout the specified timeout in milliseconds
401: * @return this <code>NIOTransportBuilder</code>
402: * @see java.net.Socket#setSoTimeout(int)
403: */
404: public T setClientSocketSoTimeout(int clientSocketSoTimeout) {
405: this.clientSocketSoTimeout = clientSocketSoTimeout;
406: return getThis();
407: }
408:
409: /**
410: * @return value of the connectio timeout in milliseconds
411: * @see java.net.URLConnection#getConnectTimeout()
412: */
413: public int getConnectionTimeout() {
414: return connectionTimeout;
415: }
416:
417: /**
418: * @param connectionTimeout the value of the connection timeout in milliseconds
419: * @return this <code>NIOTransportBuilder</code>
420: * @see NIOTransport#setConnectionTimeout(int)
421: */
422: public T setConnectionTimeout(int connectionTimeout) {
423: this.connectionTimeout = connectionTimeout;
424: return getThis();
425: }
426:
427: /**
428: * @param timeUnit the {@link TimeUnit} to convert the result to
429: * @return the blocking read timeout in the specified {@link TimeUnit}
430: * @see Transport#getReadTimeout(java.util.concurrent.TimeUnit)
431: */
432: public long getReadTimeout(final TimeUnit timeUnit) {
433:• if (readTimeout <= 0) {
434: return -1;
435: } else {
436: return timeUnit.convert(readTimeout, TimeUnit.MILLISECONDS);
437: }
438: }
439:
440: /**
441: * Sets the value of the blocking read timeout
442: *
443: * @param timeout the new timeout value
444: * @param timeUnit the unit of the new timeout value
445: * @return this NioTransportBuilder
446: * @see Transport#setReadTimeout(long, java.util.concurrent.TimeUnit)
447: */
448: public T setReadTimeout(final long timeout, final TimeUnit timeUnit) {
449:• if (timeout <= 0) {
450: readTimeout = -1;
451: } else {
452: readTimeout = TimeUnit.MILLISECONDS.convert(timeout, timeUnit);
453: }
454: return getThis();
455: }
456:
457: /**
458: * @param timeUnit the {@link TimeUnit} to convert the result to
459: * @return the value of the write timeout
460: * @see Transport#getWriteTimeout(java.util.concurrent.TimeUnit)
461: */
462: public long getWriteTimeout(final TimeUnit timeUnit) {
463:• if (writeTimeout <= 0) {
464: return -1;
465: } else {
466: return timeUnit.convert(writeTimeout, TimeUnit.MILLISECONDS);
467: }
468: }
469:
470: /**
471: * @param timeout the new write timeout value
472: * @param timeUnit the {@link TimeUnit} of the timeout value
473: * @return this NIOTransportBuilder
474: * @see Transport#setWriteTimeout(long, java.util.concurrent.TimeUnit)
475: */
476: public T setWriteTimeout(final long timeout, final TimeUnit timeUnit) {
477:• if (timeout <= 0) {
478: writeTimeout = -1;
479: } else {
480: writeTimeout = TimeUnit.MILLISECONDS.convert(timeout, timeUnit);
481: }
482: return getThis();
483: }
484:
485: /**
486: * Whether address may be reused for multiple sockets
487: *
488: * @return SO_REUSEADDR
489: * @see <a href="http://man7.org/linux/man-pages/man7/socket.7.html">Socket man page</a>
490: */
491: public boolean isReuseAddress() {
492: return reuseAddress;
493: }
494:
495: /**
496: * Sets whether address may be reused for multiple sockets
497: *
498: * @param reuseAddress SO_REUSEADDR
499: * @return this <code>TCPNIOTransportBuilder</code>
500: * @see <a href="http://man7.org/linux/man-pages/man7/socket.7.html">Socket man page</a>
501: */
502: public T setReuseAddress(boolean reuseAddress) {
503: this.reuseAddress = reuseAddress;
504: return getThis();
505: }
506:
507: /**
508: * Max asynchronous write queue size in bytes
509: *
510: * @return the value is per connection, not transport total.
511: * @see org.glassfish.grizzly.asyncqueue.AsyncQueueWriter#getMaxPendingBytesPerConnection()
512: */
513: public int getMaxAsyncWriteQueueSizeInBytes() {
514: return maxPendingBytesPerConnection;
515: }
516:
517: /**
518: * @param maxAsyncWriteQueueSizeInBytes the value is per connection, not transport total.
519: * @return this <code>TCPNIOTransportBuilder</code>
520: * @see org.glassfish.grizzly.asyncqueue.AsyncQueueWriter#setMaxPendingBytesPerConnection(int)
521: */
522: public T setMaxAsyncWriteQueueSizeInBytes(final int maxAsyncWriteQueueSizeInBytes) {
523: this.maxPendingBytesPerConnection = maxAsyncWriteQueueSizeInBytes;
524: return getThis();
525: }
526:
527: /**
528: * @return true, if NIOTransport is configured to use AsyncQueueWriter, optimized to be used in connection multiplexing
529: * mode, or false otherwise.
530: * @see org.glassfish.grizzly.nio.NIOTransport#isOptimizedForMultiplexing()
531: */
532: public boolean isOptimizedForMultiplexing() {
533: return optimizedForMultiplexing;
534: }
535:
536: /**
537: * @param optimizedForMultiplexing Configure NIOTransport to be optimized for connection multiplexing
538: * @see org.glassfish.grizzly.nio.NIOTransport#setOptimizedForMultiplexing(boolean)
539: *
540: * @return this <code>TCPNIOTransportBuilder</code>
541: */
542: public T setOptimizedForMultiplexing(final boolean optimizedForMultiplexing) {
543: this.optimizedForMultiplexing = optimizedForMultiplexing;
544: return getThis();
545: }
546:
547: /**
548: * @return an {@link NIOTransport} based on the builder's configuration.
549: */
550: public NIOTransport build() {
551: NIOTransport transport = create(name);
552: transport.setIOStrategy(ioStrategy);
553:• if (workerConfig != null) {
554: transport.setWorkerThreadPoolConfig(workerConfig.copy());
555: }
556:• if (kernelConfig != null) {
557: transport.setKernelThreadPoolConfig(kernelConfig.copy());
558: }
559: transport.setSelectorProvider(selectorProvider);
560: transport.setSelectorHandler(selectorHandler);
561: transport.setSelectionKeyHandler(selectionKeyHandler);
562: transport.setMemoryManager(memoryManager);
563: transport.setAttributeBuilder(attributeBuilder);
564: transport.setSelectorRunnersCount(selectorRunnerCount);
565: transport.setNIOChannelDistributor(nioChannelDistributor);
566: transport.setProcessor(processor);
567: transport.setProcessorSelector(processorSelector);
568: transport.setClientSocketSoTimeout(clientSocketSoTimeout);
569: transport.setConnectionTimeout(connectionTimeout);
570: transport.setReadTimeout(readTimeout, TimeUnit.MILLISECONDS);
571: transport.setWriteTimeout(writeTimeout, TimeUnit.MILLISECONDS);
572: transport.setReadBufferSize(readBufferSize);
573: transport.setWriteBufferSize(writeBufferSize);
574: transport.setReuseAddress(reuseAddress);
575: transport.setOptimizedForMultiplexing(isOptimizedForMultiplexing());
576: transport.getAsyncQueueIO().getWriter().setMaxPendingBytesPerConnection(maxPendingBytesPerConnection);
577: return transport;
578: }
579:
580: // ------------------------------------------------------- Protected Methods
581:
582: /**
583: * @return this NIOTransportBuilder
584: * @see <a href=
585: * "http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ205">http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ205</a>
586: */
587: protected abstract T getThis();
588:
589: protected abstract NIOTransport create(String name);
590: }