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  package org.jboss.netty.channel.socket.nio;
17  
18  import org.jboss.netty.logging.InternalLogger;
19  import org.jboss.netty.logging.InternalLoggerFactory;
20  import org.jboss.netty.util.internal.SystemPropertyUtil;
21  
22  import java.io.IOException;
23  import java.nio.channels.CancelledKeyException;
24  import java.nio.channels.Selector;
25  import java.util.concurrent.TimeUnit;
26  
27  final class SelectorUtil {
28      private static final InternalLogger logger =
29          InternalLoggerFactory.getInstance(SelectorUtil.class);
30  
31      static final int DEFAULT_IO_THREADS = Runtime.getRuntime().availableProcessors() * 2;
32      static final long DEFAULT_SELECT_TIMEOUT = 500;
33      static final long SELECT_TIMEOUT =
34              SystemPropertyUtil.getLong("org.jboss.netty.selectTimeout", DEFAULT_SELECT_TIMEOUT);
35      static final long SELECT_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(SELECT_TIMEOUT);
36      static final boolean EPOLL_BUG_WORKAROUND =
37              SystemPropertyUtil.getBoolean("org.jboss.netty.epollBugWorkaround", false);
38  
39      // Workaround for JDK NIO bug.
40      //
41      // See:
42      // - http://bugs.sun.com/view_bug.do?bug_id=6427854
43      // - https://github.com/netty/netty/issues/203
44      static {
45          String key = "sun.nio.ch.bugLevel";
46          try {
47              String buglevel = System.getProperty(key);
48              if (buglevel == null) {
49                  System.setProperty(key, "");
50              }
51          } catch (SecurityException e) {
52              if (logger.isDebugEnabled()) {
53                  logger.debug("Unable to get/set System Property '" + key + '\'', e);
54              }
55          }
56          if (logger.isDebugEnabled()) {
57              logger.debug("Using select timeout of " + SELECT_TIMEOUT);
58              logger.debug("Epoll-bug workaround enabled = " + EPOLL_BUG_WORKAROUND);
59          }
60      }
61  
62      static Selector open() throws IOException {
63          return Selector.open();
64      }
65  
66      static int select(Selector selector) throws IOException {
67          try {
68              return selector.select(SELECT_TIMEOUT);
69          } catch (CancelledKeyException e) {
70              if (logger.isDebugEnabled()) {
71                  logger.debug(
72                          CancelledKeyException.class.getSimpleName() +
73                          " raised by a Selector - JDK bug?", e);
74              }
75              // Harmless exception - log anyway
76          }
77          return -1;
78      }
79  
80      private SelectorUtil() {
81          // Unused
82      }
83  }