View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.nio.reactor;
29  
30  import org.apache.http.annotation.NotThreadSafe;
31  import org.apache.http.util.Args;
32  
33  /**
34   * I/O reactor configuration parameters.
35   *
36   * @since 4.2
37   */
38  @NotThreadSafe
39  public final class IOReactorConfig implements Cloneable {
40  
41      private static final int AVAIL_PROCS = Runtime.getRuntime().availableProcessors();
42  
43      public static final IOReactorConfig DEFAULT = new Builder().build();
44  
45      // TODO: make final
46      private long selectInterval;
47      private long shutdownGracePeriod;
48      private boolean interestOpQueued;
49      private int ioThreadCount;
50      private int soTimeout;
51      private boolean soReuseAddress;
52      private int soLinger;
53      private boolean soKeepAlive;
54      private boolean tcpNoDelay;
55      private int connectTimeout;
56      private int sndBufSize;
57      private int rcvBufSize;
58  
59      @Deprecated
60      public IOReactorConfig() {
61          super();
62          this.selectInterval = 1000;
63          this.shutdownGracePeriod = 500;
64          this.interestOpQueued = false;
65          this.ioThreadCount = AVAIL_PROCS;
66          this.soTimeout = 0;
67          this.soReuseAddress = false;
68          this.soLinger = -1;
69          this.soKeepAlive = false;
70          this.tcpNoDelay = true;
71          this.connectTimeout = 0;
72          this.sndBufSize = 0;
73          this.rcvBufSize = 0;
74      }
75  
76      IOReactorConfig(
77              final long selectInterval,
78              final long shutdownGracePeriod,
79              final boolean interestOpQueued,
80              final int ioThreadCount,
81              final int soTimeout,
82              final boolean soReuseAddress,
83              final int soLinger,
84              final boolean soKeepAlive,
85              final boolean tcpNoDelay,
86              final int connectTimeout,
87              final int sndBufSize,
88              final int rcvBufSize) {
89          super();
90          this.selectInterval = selectInterval;
91          this.shutdownGracePeriod = shutdownGracePeriod;
92          this.interestOpQueued = interestOpQueued;
93          this.ioThreadCount = ioThreadCount;
94          this.soTimeout = soTimeout;
95          this.soReuseAddress = soReuseAddress;
96          this.soLinger = soLinger;
97          this.soKeepAlive = soKeepAlive;
98          this.tcpNoDelay = tcpNoDelay;
99          this.connectTimeout = connectTimeout;
100         this.sndBufSize = sndBufSize;
101         this.rcvBufSize = rcvBufSize;
102     }
103 
104     /**
105      * Determines time interval in milliseconds at which the I/O reactor wakes up to check for
106      * timed out sessions and session requests.
107      * <p/>
108      * Default: <code>1000</code> milliseconds.
109      */
110     public long getSelectInterval() {
111         return this.selectInterval;
112     }
113 
114     /**
115      * @deprecated (4.3) use {@link Builder#setSelectInterval(long)}
116      */
117     @Deprecated
118     public void setSelectInterval(final long selectInterval) {
119         Args.positive(selectInterval, "Select internal");
120         this.selectInterval = selectInterval;
121     }
122 
123     /**
124      * Determines grace period in milliseconds the I/O reactors are expected to block waiting
125      * for individual worker threads to terminate cleanly.
126      * <p/>
127      * Default: <code>500</code> milliseconds.
128      */
129     public long getShutdownGracePeriod() {
130         return this.shutdownGracePeriod;
131     }
132 
133     /**
134      * @deprecated (4.3) use {@link Builder#setShutdownGracePeriod(long)}
135      */
136     @Deprecated
137     public void setShutdownGracePeriod(final long gracePeriod) {
138         Args.positive(gracePeriod, "Shutdown grace period");
139         this.shutdownGracePeriod = gracePeriod;
140     }
141 
142     /**
143      * Determines whether or not I/O interest operations are to be queued and executed
144      * asynchronously by the I/O reactor thread or to be applied to the underlying
145      * {@link java.nio.channels.SelectionKey} immediately.
146      * <p/>
147      * Default: <code>false</code>
148      *
149      * @see java.nio.channels.SelectionKey
150      * @see java.nio.channels.SelectionKey#interestOps()
151      * @see java.nio.channels.SelectionKey#interestOps(int)
152      */
153     public boolean isInterestOpQueued() {
154         return this.interestOpQueued;
155     }
156 
157     /**
158      * @deprecated (4.3) use {@link Builder#setInterestOpQueued(boolean)}
159      */
160     @Deprecated
161     public void setInterestOpQueued(final boolean interestOpQueued) {
162         this.interestOpQueued = interestOpQueued;
163     }
164 
165     /**
166      * Determines the number of I/O dispatch threads to be used by the I/O reactor.
167      * <p/>
168      * Default: <code>2</code>
169      */
170     public int getIoThreadCount() {
171         return this.ioThreadCount;
172     }
173 
174     /**
175      * @deprecated (4.3) use {@link Builder#setIoThreadCount(int)}
176      */
177     @Deprecated
178     public void setIoThreadCount(final int ioThreadCount) {
179         Args.positive(ioThreadCount, "I/O thread count");
180         this.ioThreadCount = ioThreadCount;
181     }
182 
183     /**
184      * Determines the default socket timeout value for non-blocking I/O operations.
185      * <p/>
186      * Default: <code>0</code> (no timeout)
187      *
188      * @see java.net.SocketOptions#SO_TIMEOUT
189      */
190     public int getSoTimeout() {
191         return soTimeout;
192     }
193 
194     /**
195      * @deprecated (4.3) use {@link Builder#setSoTimeout(int)}
196      */
197     @Deprecated
198     public void setSoTimeout(final int soTimeout) {
199         this.soTimeout = soTimeout;
200     }
201 
202     /**
203      * Determines the default value of the {@link java.net.SocketOptions#SO_REUSEADDR} parameter
204      * for newly created sockets.
205      * <p/>
206      * Default: <code>false</code>
207      *
208      * @see java.net.SocketOptions#SO_REUSEADDR
209      */
210     public boolean isSoReuseAddress() {
211         return soReuseAddress;
212     }
213 
214     /**
215      * @deprecated (4.3) use {@link Builder#setSoReuseAddress(boolean)}
216      */
217     @Deprecated
218     public void setSoReuseAddress(final boolean soReuseAddress) {
219         this.soReuseAddress = soReuseAddress;
220     }
221 
222     /**
223      * Determines the default value of the {@link java.net.SocketOptions#SO_LINGER} parameter
224      * for newly created sockets.
225      * <p/>
226      * Default: <code>-1</code>
227      *
228      * @see java.net.SocketOptions#SO_LINGER
229      */
230     public int getSoLinger() {
231         return soLinger;
232     }
233 
234     /**
235      * @deprecated (4.3) use {@link Builder#setSoLinger(int)}
236      */
237     @Deprecated
238     public void setSoLinger(final int soLinger) {
239         this.soLinger = soLinger;
240     }
241 
242     /**
243      * Determines the default value of the {@link java.net.SocketOptions#SO_KEEPALIVE} parameter
244      * for newly created sockets.
245      * <p/>
246      * Default: <code>-1</code>
247      *
248      * @see java.net.SocketOptions#SO_KEEPALIVE
249      */
250     public boolean isSoKeepalive() {
251         return this.soKeepAlive;
252     }
253 
254     /**
255      * @deprecated (4.3) use {@link Builder#setSoKeepAlive(boolean)}
256      */
257     @Deprecated
258     public void setSoKeepalive(final boolean soKeepAlive) {
259         this.soKeepAlive = soKeepAlive;
260     }
261 
262     /**
263      * Determines the default value of the {@link java.net.SocketOptions#TCP_NODELAY} parameter
264      * for newly created sockets.
265      * <p/>
266      * Default: <code>false</code>
267      *
268      * @see java.net.SocketOptions#TCP_NODELAY
269      */
270     public boolean isTcpNoDelay() {
271         return tcpNoDelay;
272     }
273 
274     /**
275      * @deprecated (4.3) use {@link Builder#setTcpNoDelay(boolean)}
276      */
277     @Deprecated
278     public void setTcpNoDelay(final boolean tcpNoDelay) {
279         this.tcpNoDelay = tcpNoDelay;
280     }
281 
282     /**
283      * Determines the default connect timeout value for non-blocking connection requests.
284      * <p/>
285      * Default: <code>0</code> (no timeout)
286      */
287     public int getConnectTimeout() {
288         return connectTimeout;
289     }
290 
291     /**
292      * @deprecated (4.3) use {@link Builder#setConnectTimeout(int)}
293      */
294     @Deprecated
295     public void setConnectTimeout(final int connectTimeout) {
296         this.connectTimeout = connectTimeout;
297     }
298 
299     /**
300      * Determines the default value of the {@link java.net.SocketOptions#SO_SNDBUF} parameter
301      * for newly created sockets.
302      * <p/>
303      * Default: <code>0</code> (system default)
304      *
305      * @see java.net.SocketOptions#SO_SNDBUF
306      */
307     public int getSndBufSize() {
308         return sndBufSize;
309     }
310 
311     /**
312      * @deprecated (4.3) use {@link Builder#setSndBufSize(int)}
313      */
314     @Deprecated
315     public void setSndBufSize(final int sndBufSize) {
316         this.sndBufSize = sndBufSize;
317     }
318 
319     /**
320      * Determines the default value of the {@link java.net.SocketOptions#SO_RCVBUF} parameter
321      * for newly created sockets.
322      * <p/>
323      * Default: <code>0</code> (system default)
324      *
325      * @see java.net.SocketOptions#SO_RCVBUF
326      */
327     public int getRcvBufSize() {
328         return rcvBufSize;
329     }
330 
331     /**
332      * @deprecated (4.3) use {@link Builder#setRcvBufSize(int)}
333      */
334     @Deprecated
335     public void setRcvBufSize(final int rcvBufSize) {
336         this.rcvBufSize = rcvBufSize;
337     }
338 
339     @Override
340     protected IOReactorConfig clone() throws CloneNotSupportedException {
341         return (IOReactorConfig) super.clone();
342     }
343 
344     public static Builder custom() {
345         return new Builder();
346     }
347 
348     public static Builder copy(final IOReactorConfig config) {
349         Args.notNull(config, "I/O reactor config");
350         return new Builder()
351             .setSelectInterval(config.getSelectInterval())
352             .setShutdownGracePeriod(config.getShutdownGracePeriod())
353             .setInterestOpQueued(config.isInterestOpQueued())
354             .setIoThreadCount(config.getIoThreadCount())
355             .setSoTimeout(config.getSoTimeout())
356             .setSoReuseAddress(config.isSoReuseAddress())
357             .setSoLinger(config.getSoLinger())
358             .setSoKeepAlive(config.isSoKeepalive())
359             .setTcpNoDelay(config.isTcpNoDelay())
360             .setConnectTimeout(config.getConnectTimeout());
361     }
362 
363     public static class Builder {
364 
365         private long selectInterval;
366         private long shutdownGracePeriod;
367         private boolean interestOpQueued;
368         private int ioThreadCount;
369         private int soTimeout;
370         private boolean soReuseAddress;
371         private int soLinger;
372         private boolean soKeepAlive;
373         private boolean tcpNoDelay;
374         private int connectTimeout;
375         private int sndBufSize;
376         private int rcvBufSize;
377 
378         Builder() {
379             this.selectInterval = 1000;
380             this.shutdownGracePeriod = 500;
381             this.interestOpQueued = false;
382             this.ioThreadCount = AVAIL_PROCS;
383             this.soTimeout = 0;
384             this.soReuseAddress = false;
385             this.soLinger = -1;
386             this.soKeepAlive = false;
387             this.tcpNoDelay = true;
388             this.connectTimeout = 0;
389             this.sndBufSize = 0;
390             this.rcvBufSize = 0;
391         }
392 
393         public Builder setSelectInterval(final long selectInterval) {
394             this.selectInterval = selectInterval;
395             return this;
396         }
397 
398         public Builder setShutdownGracePeriod(final long shutdownGracePeriod) {
399             this.shutdownGracePeriod = shutdownGracePeriod;
400             return this;
401         }
402 
403         public Builder setInterestOpQueued(final boolean interestOpQueued) {
404             this.interestOpQueued = interestOpQueued;
405             return this;
406         }
407 
408         public Builder setIoThreadCount(final int ioThreadCount) {
409             this.ioThreadCount = ioThreadCount;
410             return this;
411         }
412 
413         public Builder setSoTimeout(final int soTimeout) {
414             this.soTimeout = soTimeout;
415             return this;
416         }
417 
418         public Builder setSoReuseAddress(final boolean soReuseAddress) {
419             this.soReuseAddress = soReuseAddress;
420             return this;
421         }
422 
423         public Builder setSoLinger(final int soLinger) {
424             this.soLinger = soLinger;
425             return this;
426         }
427 
428         public Builder setSoKeepAlive(final boolean soKeepAlive) {
429             this.soKeepAlive = soKeepAlive;
430             return this;
431         }
432 
433         public Builder setTcpNoDelay(final boolean tcpNoDelay) {
434             this.tcpNoDelay = tcpNoDelay;
435             return this;
436         }
437 
438         public Builder setConnectTimeout(final int connectTimeout) {
439             this.connectTimeout = connectTimeout;
440             return this;
441         }
442 
443         public Builder setSndBufSize(final int sndBufSize) {
444             this.sndBufSize = sndBufSize;
445             return this;
446         }
447 
448         public Builder setRcvBufSize(final int rcvBufSize) {
449             this.rcvBufSize = rcvBufSize;
450             return this;
451         }
452 
453         public IOReactorConfig build() {
454             return new IOReactorConfig(
455                     selectInterval, shutdownGracePeriod, interestOpQueued, ioThreadCount,
456                     soTimeout, soReuseAddress, soLinger, soKeepAlive, tcpNoDelay,
457                     connectTimeout, sndBufSize, rcvBufSize);
458         }
459 
460     }
461 
462     @Override
463     public String toString() {
464         final StringBuilder builder = new StringBuilder();
465         builder.append("[selectInterval=").append(this.selectInterval)
466                 .append(", shutdownGracePeriod=").append(this.shutdownGracePeriod)
467                 .append(", interestOpQueued=").append(this.interestOpQueued)
468                 .append(", ioThreadCount=").append(this.ioThreadCount)
469                 .append(", soTimeout=").append(this.soTimeout)
470                 .append(", soReuseAddress=").append(this.soReuseAddress)
471                 .append(", soLinger=").append(this.soLinger)
472                 .append(", soKeepAlive=").append(this.soKeepAlive)
473                 .append(", tcpNoDelay=").append(this.tcpNoDelay)
474                 .append(", connectTimeout=").append(this.connectTimeout)
475                 .append(", sndBufSize=").append(this.sndBufSize)
476                 .append(", rcvBufSize=").append(this.rcvBufSize)
477                 .append("]");
478         return builder.toString();
479     }
480 
481 }