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    *   https://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 io.netty.channel.nio;
17  
18  import io.netty.channel.Channel;
19  import io.netty.channel.DefaultSelectStrategyFactory;
20  import io.netty.channel.EventLoopTaskQueueFactory;
21  import io.netty.channel.IoEventLoop;
22  import io.netty.channel.IoEventLoopGroup;
23  import io.netty.channel.IoHandler;
24  import io.netty.channel.MultiThreadIoEventLoopGroup;
25  import io.netty.channel.SelectStrategyFactory;
26  import io.netty.channel.SingleThreadEventLoop;
27  import io.netty.util.concurrent.EventExecutor;
28  import io.netty.util.concurrent.EventExecutorChooserFactory;
29  import io.netty.util.concurrent.RejectedExecutionHandler;
30  import io.netty.util.concurrent.RejectedExecutionHandlers;
31  import io.netty.util.internal.logging.InternalLogger;
32  import io.netty.util.internal.logging.InternalLoggerFactory;
33  
34  import java.nio.channels.Selector;
35  import java.nio.channels.spi.SelectorProvider;
36  import java.util.concurrent.Executor;
37  import java.util.concurrent.ThreadFactory;
38  
39  /**
40   * {@link MultiThreadIoEventLoopGroup} implementation which is used for NIO {@link Selector} based {@link Channel}s.
41   *
42   * @deprecated Use {@link MultiThreadIoEventLoopGroup} with {@link io.netty.channel.nio.NioIoHandler}.
43   */
44  @Deprecated
45  public class NioEventLoopGroup extends MultiThreadIoEventLoopGroup implements IoEventLoopGroup {
46  
47      private static final InternalLogger LOGGER = InternalLoggerFactory.getInstance(NioEventLoopGroup.class);
48  
49      /**
50       * Create a new instance using the default number of threads, the default {@link ThreadFactory} and
51       * the {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
52       */
53      public NioEventLoopGroup() {
54          this(0);
55      }
56  
57      /**
58       * Create a new instance using the specified number of threads, {@link ThreadFactory} and the
59       * {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
60       */
61      public NioEventLoopGroup(int nThreads) {
62          this(nThreads, (Executor) null);
63      }
64  
65      /**
66       * Create a new instance using the default number of threads, the given {@link ThreadFactory} and the
67       * {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
68       */
69      public NioEventLoopGroup(ThreadFactory threadFactory) {
70          this(0, threadFactory, SelectorProvider.provider());
71      }
72  
73      /**
74       * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the
75       * {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
76       */
77      public NioEventLoopGroup(int nThreads, ThreadFactory threadFactory) {
78          this(nThreads, threadFactory, SelectorProvider.provider());
79      }
80  
81      public NioEventLoopGroup(int nThreads, Executor executor) {
82          this(nThreads, executor, SelectorProvider.provider());
83      }
84  
85      /**
86       * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
87       * {@link SelectorProvider}.
88       */
89      public NioEventLoopGroup(
90              int nThreads, ThreadFactory threadFactory, final SelectorProvider selectorProvider) {
91          this(nThreads, threadFactory, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);
92      }
93  
94      public NioEventLoopGroup(int nThreads, ThreadFactory threadFactory,
95          final SelectorProvider selectorProvider, final SelectStrategyFactory selectStrategyFactory) {
96          super(nThreads, threadFactory, NioIoHandler.newFactory(selectorProvider, selectStrategyFactory),
97                  RejectedExecutionHandlers.reject());
98      }
99  
100     public NioEventLoopGroup(
101             int nThreads, Executor executor, final SelectorProvider selectorProvider) {
102         this(nThreads, executor, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);
103     }
104 
105     public NioEventLoopGroup(int nThreads, Executor executor, final SelectorProvider selectorProvider,
106                              final SelectStrategyFactory selectStrategyFactory) {
107         super(nThreads, executor, NioIoHandler.newFactory(selectorProvider, selectStrategyFactory),
108                 RejectedExecutionHandlers.reject());
109     }
110 
111     public NioEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
112                              final SelectorProvider selectorProvider,
113                              final SelectStrategyFactory selectStrategyFactory) {
114         super(nThreads, executor, NioIoHandler.newFactory(selectorProvider, selectStrategyFactory), chooserFactory,
115                 RejectedExecutionHandlers.reject());
116     }
117 
118     public NioEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
119                              final SelectorProvider selectorProvider,
120                              final SelectStrategyFactory selectStrategyFactory,
121                              final RejectedExecutionHandler rejectedExecutionHandler) {
122         super(nThreads, executor, NioIoHandler.newFactory(selectorProvider, selectStrategyFactory), chooserFactory,
123                 rejectedExecutionHandler);
124     }
125 
126     public NioEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
127                              final SelectorProvider selectorProvider,
128                              final SelectStrategyFactory selectStrategyFactory,
129                              final RejectedExecutionHandler rejectedExecutionHandler,
130                              final EventLoopTaskQueueFactory taskQueueFactory) {
131         super(nThreads, executor, NioIoHandler.newFactory(selectorProvider, selectStrategyFactory), chooserFactory,
132                 rejectedExecutionHandler, taskQueueFactory);
133     }
134 
135     /**
136      * @param nThreads the number of threads that will be used by this instance.
137      * @param executor the Executor to use, or {@code null} if default one should be used.
138      * @param chooserFactory the {@link EventExecutorChooserFactory} to use.
139      * @param selectorProvider the {@link SelectorProvider} to use.
140      * @param selectStrategyFactory the {@link SelectStrategyFactory} to use.
141      * @param rejectedExecutionHandler the {@link RejectedExecutionHandler} to use.
142      * @param taskQueueFactory the {@link EventLoopTaskQueueFactory} to use for
143      *                         {@link SingleThreadEventLoop#execute(Runnable)},
144      *                         or {@code null} if default one should be used.
145      * @param tailTaskQueueFactory the {@link EventLoopTaskQueueFactory} to use for
146      *                             {@link SingleThreadEventLoop#executeAfterEventLoopIteration(Runnable)},
147      *                             or {@code null} if default one should be used.
148      */
149     public NioEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
150                              SelectorProvider selectorProvider,
151                              SelectStrategyFactory selectStrategyFactory,
152                              RejectedExecutionHandler rejectedExecutionHandler,
153                              EventLoopTaskQueueFactory taskQueueFactory,
154                              EventLoopTaskQueueFactory tailTaskQueueFactory) {
155         super(nThreads, executor, NioIoHandler.newFactory(selectorProvider, selectStrategyFactory), chooserFactory,
156                 rejectedExecutionHandler, taskQueueFactory, tailTaskQueueFactory);
157     }
158 
159     /**
160      * This method is a no-op.
161      *
162      * @deprecated
163      */
164     @Deprecated
165     public void setIoRatio(int ioRatio) {
166         LOGGER.debug("NioEventLoopGroup.setIoRatio(int) logic was removed, this is a no-op");
167     }
168 
169     /**
170      * Replaces the current {@link Selector}s of the child event loops with newly created {@link Selector}s to work
171      * around the  infamous epoll 100% CPU bug.
172      */
173     public void rebuildSelectors() {
174         for (EventExecutor e: this) {
175             ((NioEventLoop) e).rebuildSelector();
176         }
177     }
178 
179     @Override
180     protected IoEventLoop newChild(Executor executor, IoHandler ioHandler, Object... args) {
181         RejectedExecutionHandler rejectedExecutionHandler = (RejectedExecutionHandler) args[0];
182         EventLoopTaskQueueFactory taskQueueFactory = null;
183         EventLoopTaskQueueFactory tailTaskQueueFactory = null;
184 
185         int argsLength = args.length;
186         if (argsLength > 1) {
187             taskQueueFactory = (EventLoopTaskQueueFactory) args[1];
188         }
189         if (argsLength > 2) {
190             tailTaskQueueFactory = (EventLoopTaskQueueFactory) args[2];
191         }
192         return new NioEventLoop(
193                 this, executor, ioHandler, taskQueueFactory, tailTaskQueueFactory, rejectedExecutionHandler);
194     }
195 }