View Javadoc
1   /*
2    * Copyright 2016 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 io.netty.util.concurrent;
17  
18  import java.util.concurrent.RejectedExecutionException;
19  import java.util.concurrent.TimeUnit;
20  import java.util.concurrent.locks.LockSupport;
21  
22  /**
23   * Expose helper methods which create different {@link RejectedExecutionHandler}s.
24   */
25  public final class RejectedExecutionHandlers {
26      private static final RejectedExecutionHandler REJECT = new RejectedExecutionHandler() {
27          @Override
28          public void rejected(Runnable task, SingleThreadEventExecutor executor) {
29              throw new RejectedExecutionException();
30          }
31      };
32  
33      private RejectedExecutionHandlers() { }
34  
35      /**
36       * Returns a {@link RejectedExecutionHandler} that will always just throw a {@link RejectedExecutionException}.
37       */
38      public static RejectedExecutionHandler reject() {
39          return REJECT;
40      }
41  
42      /**
43       * Tries to backoff when the task can not be added due restrictions for an configured amount of time. This
44       * is only done if the task was added from outside of the event loop which means
45       * {@link EventExecutor#inEventLoop()} returns {@code false}.
46       */
47      public static RejectedExecutionHandler backoff(final int retries, long backoffAmount, TimeUnit unit) {
48          if (retries <= 0) {
49              throw new IllegalArgumentException(retries + ": " + retries + " (expected: > 0)");
50          }
51          final long backOffNanos = unit.toNanos(backoffAmount);
52          return new RejectedExecutionHandler() {
53              @Override
54              public void rejected(Runnable task, SingleThreadEventExecutor executor) {
55                  if (!executor.inEventLoop()) {
56                      for (int i = 0; i < retries; i++) {
57                          // Try to wake up the executor so it will empty its task queue.
58                          executor.wakeup(false);
59  
60                          LockSupport.parkNanos(backOffNanos);
61                          if (executor.offerTask(task)) {
62                              return;
63                          }
64                      }
65                  }
66                  // Either we tried to add the task from within the EventLoop or we was not able to add it even with
67                  // backoff.
68                  throw new RejectedExecutionException();
69              }
70          };
71      }
72  }