View Javadoc
1   /*
2    * Copyright 2014 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.epoll;
17  
18  import io.netty.channel.DefaultSelectStrategyFactory;
19  import io.netty.channel.EventLoopGroup;
20  import io.netty.channel.EventLoopTaskQueueFactory;
21  import io.netty.channel.IoEventLoop;
22  import io.netty.channel.IoHandlerFactory;
23  import io.netty.channel.MultiThreadIoEventLoopGroup;
24  import io.netty.channel.SelectStrategyFactory;
25  import io.netty.channel.SingleThreadEventLoop;
26  import io.netty.util.concurrent.EventExecutorChooserFactory;
27  import io.netty.util.concurrent.RejectedExecutionHandler;
28  import io.netty.util.concurrent.RejectedExecutionHandlers;
29  
30  import java.util.concurrent.Executor;
31  import java.util.concurrent.ThreadFactory;
32  
33  /**
34   * {@link EventLoopGroup} which uses epoll under the covers. Because of this
35   * it only works on linux.
36   *
37   * @deprecated Use {@link MultiThreadIoEventLoopGroup} with {@link EpollIoHandler#newFactory()}.
38   */
39  @Deprecated
40  public final class EpollEventLoopGroup extends MultiThreadIoEventLoopGroup {
41  
42      // This does not use static by design to ensure the class can be loaded and only do the check when its actually
43      // instanced.
44      {
45          // Ensure JNI is initialized by the time this class is loaded.
46          Epoll.ensureAvailability();
47      }
48  
49      /**
50       * Create a new instance using the default number of threads and the default {@link ThreadFactory}.
51       */
52      public EpollEventLoopGroup() {
53          this(0);
54      }
55  
56      /**
57       * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
58       */
59      public EpollEventLoopGroup(int nThreads) {
60          this(nThreads, (ThreadFactory) null);
61      }
62  
63      /**
64       * Create a new instance using the default number of threads and the given {@link ThreadFactory}.
65       */
66      @SuppressWarnings("deprecation")
67      public EpollEventLoopGroup(ThreadFactory threadFactory) {
68          this(0, threadFactory, 0);
69      }
70  
71      /**
72       * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
73       */
74      @SuppressWarnings("deprecation")
75      public EpollEventLoopGroup(int nThreads, SelectStrategyFactory selectStrategyFactory) {
76          this(nThreads, (ThreadFactory) null, selectStrategyFactory);
77      }
78  
79      /**
80       * Create a new instance using the specified number of threads and the given {@link ThreadFactory}.
81       */
82      @SuppressWarnings("deprecation")
83      public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory) {
84          this(nThreads, threadFactory, 0);
85      }
86  
87      public EpollEventLoopGroup(int nThreads, Executor executor) {
88          this(nThreads, executor, DefaultSelectStrategyFactory.INSTANCE);
89      }
90  
91      /**
92       * Create a new instance using the specified number of threads and the given {@link ThreadFactory}.
93       */
94      @SuppressWarnings("deprecation")
95      public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, SelectStrategyFactory selectStrategyFactory) {
96          this(nThreads, threadFactory, 0, selectStrategyFactory);
97      }
98  
99      /**
100      * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
101      * maximal amount of epoll events to handle per epollWait(...).
102      *
103      * @deprecated  Use {@link #EpollEventLoopGroup(int)} or {@link #EpollEventLoopGroup(int, ThreadFactory)}
104      */
105     @Deprecated
106     public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce) {
107         this(nThreads, threadFactory, maxEventsAtOnce, DefaultSelectStrategyFactory.INSTANCE);
108     }
109 
110     /**
111      * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
112      * maximal amount of epoll events to handle per epollWait(...).
113      *
114      * @deprecated  Use {@link #EpollEventLoopGroup(int)}, {@link #EpollEventLoopGroup(int, ThreadFactory)}, or
115      * {@link #EpollEventLoopGroup(int, SelectStrategyFactory)}
116      */
117     @Deprecated
118     public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce,
119                                SelectStrategyFactory selectStrategyFactory) {
120         super(nThreads, threadFactory, EpollIoHandler.newFactory(maxEventsAtOnce, selectStrategyFactory),
121                 RejectedExecutionHandlers.reject());
122     }
123 
124     public EpollEventLoopGroup(int nThreads, Executor executor, SelectStrategyFactory selectStrategyFactory) {
125         super(nThreads, executor, EpollIoHandler.newFactory(0, selectStrategyFactory),
126                 RejectedExecutionHandlers.reject());
127     }
128 
129     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
130                                SelectStrategyFactory selectStrategyFactory) {
131         super(nThreads, executor, EpollIoHandler.newFactory(0, selectStrategyFactory), chooserFactory,
132                 RejectedExecutionHandlers.reject());
133     }
134 
135     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
136                                SelectStrategyFactory selectStrategyFactory,
137                                RejectedExecutionHandler rejectedExecutionHandler) {
138         super(nThreads, executor, EpollIoHandler.newFactory(0, selectStrategyFactory), chooserFactory,
139                 rejectedExecutionHandler);
140     }
141 
142     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
143                                SelectStrategyFactory selectStrategyFactory,
144                                RejectedExecutionHandler rejectedExecutionHandler,
145                                EventLoopTaskQueueFactory queueFactory) {
146         super(nThreads, executor, EpollIoHandler.newFactory(0, selectStrategyFactory), chooserFactory,
147                 rejectedExecutionHandler, queueFactory);
148     }
149 
150     /**
151      * @param nThreads the number of threads that will be used by this instance.
152      * @param executor the Executor to use, or {@code null} if default one should be used.
153      * @param chooserFactory the {@link EventExecutorChooserFactory} to use.
154      * @param selectStrategyFactory the {@link SelectStrategyFactory} to use.
155      * @param rejectedExecutionHandler the {@link RejectedExecutionHandler} to use.
156      * @param taskQueueFactory the {@link EventLoopTaskQueueFactory} to use for
157      *                         {@link SingleThreadEventLoop#execute(Runnable)},
158      *                         or {@code null} if default one should be used.
159      * @param tailTaskQueueFactory the {@link EventLoopTaskQueueFactory} to use for
160      *                             {@link SingleThreadEventLoop#executeAfterEventLoopIteration(Runnable)},
161      *                             or {@code null} if default one should be used.
162      */
163     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
164                                SelectStrategyFactory selectStrategyFactory,
165                                RejectedExecutionHandler rejectedExecutionHandler,
166                                EventLoopTaskQueueFactory taskQueueFactory,
167                                EventLoopTaskQueueFactory tailTaskQueueFactory) {
168         super(nThreads, executor, EpollIoHandler.newFactory(0, selectStrategyFactory),
169                 chooserFactory, rejectedExecutionHandler, taskQueueFactory, tailTaskQueueFactory);
170     }
171 
172     @Override
173     protected IoEventLoop newChild(Executor executor, IoHandlerFactory ioHandlerFactory, Object... args) {
174         RejectedExecutionHandler rejectedExecutionHandler = (RejectedExecutionHandler) args[0];
175         EventLoopTaskQueueFactory taskQueueFactory = null;
176         EventLoopTaskQueueFactory tailTaskQueueFactory = null;
177 
178         int argsLength = args.length;
179         if (argsLength > 1) {
180             taskQueueFactory = (EventLoopTaskQueueFactory) args[1];
181         }
182         if (argsLength > 2) {
183             tailTaskQueueFactory = (EventLoopTaskQueueFactory) args[2];
184         }
185         return new EpollEventLoop(this, executor, ioHandlerFactory, taskQueueFactory, tailTaskQueueFactory,
186                 rejectedExecutionHandler);
187     }
188 }