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