2013-03-11 08:38:56 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2013 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
2020-10-23 14:44:18 +02:00
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
2013-03-11 08:38:56 +01:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.util.concurrent;
|
|
|
|
|
2016-07-19 20:39:44 +02:00
|
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
|
|
|
|
2013-03-22 01:06:08 +01:00
|
|
|
import java.util.concurrent.AbstractExecutorService;
|
2013-03-11 08:38:56 +01:00
|
|
|
import java.util.concurrent.Callable;
|
2019-01-23 08:32:05 +01:00
|
|
|
import java.util.concurrent.Executors;
|
2013-03-11 08:38:56 +01:00
|
|
|
|
2013-03-18 14:18:31 +01:00
|
|
|
/**
|
2013-03-22 01:06:08 +01:00
|
|
|
* Abstract base class for {@link EventExecutor} implementations.
|
2013-03-18 14:18:31 +01:00
|
|
|
*/
|
2013-03-22 01:06:08 +01:00
|
|
|
public abstract class AbstractEventExecutor extends AbstractExecutorService implements EventExecutor {
|
2016-07-19 20:39:44 +02:00
|
|
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(AbstractEventExecutor.class);
|
2014-03-24 03:12:17 +01:00
|
|
|
static final long DEFAULT_SHUTDOWN_QUIET_PERIOD = 2;
|
|
|
|
static final long DEFAULT_SHUTDOWN_TIMEOUT = 15;
|
|
|
|
|
2021-09-02 10:46:54 +02:00
|
|
|
private final Future<?> successfulVoidFuture = DefaultPromise.newSuccessfulPromise(this, null).asFuture();
|
2014-03-24 03:05:51 +01:00
|
|
|
|
2013-03-22 01:06:08 +01:00
|
|
|
@Override
|
|
|
|
public <V> Future<V> newSucceededFuture(V result) {
|
2021-08-24 09:34:50 +02:00
|
|
|
if (result == null) {
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
Future<V> f = (Future<V>) successfulVoidFuture;
|
|
|
|
return f;
|
|
|
|
}
|
2021-09-02 19:11:01 +02:00
|
|
|
return EventExecutor.super.newSucceededFuture(result);
|
2013-03-22 01:06:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-01-23 08:32:05 +01:00
|
|
|
public final Future<?> submit(Runnable task) {
|
2013-03-22 01:06:08 +01:00
|
|
|
return (Future<?>) super.submit(task);
|
2013-03-11 08:38:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-01-23 08:32:05 +01:00
|
|
|
public final <T> Future<T> submit(Runnable task, T result) {
|
2013-03-22 01:06:08 +01:00
|
|
|
return (Future<T>) super.submit(task, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-01-23 08:32:05 +01:00
|
|
|
public final <T> Future<T> submit(Callable<T> task) {
|
2013-03-22 01:06:08 +01:00
|
|
|
return (Future<T>) super.submit(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-01-23 08:32:05 +01:00
|
|
|
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
|
Clean up Future/Promises API (#11575)
Motivation:
The generics for the existing futures, promises, and listeners are too complicated.
This complication comes from the existence of `ChannelPromise` and `ChannelFuture`, which forces listeners to care about the particular _type_ of future being listened on.
Modification:
* Add a `FutureContextListener` which can take a context object as an additional argument. This allows our listeners to have the channel piped through to them, so they don't need to rely on the `ChannelFuture.channel()` method.
* Make the `FutureListener`, along with the `FutureContextListener` sibling, the default listener API, retiring the `GenericFutureListener` since we no longer need to abstract over the type of the future.
* Change all uses of `ChannelPromise` to `Promise<Void>`.
* Change all uses of `ChannelFuture` to `Future<Void>`.
* Change all uses of `GenericFutureListener` to either `FutureListener` or `FutureContextListener` as needed.
* Remove `ChannelFutureListener` and `GenericFutureListener`.
* Introduce a `ChannelFutureListeners` enum to house the constants that previously lived in `ChannelFutureListener`. These constants now implement `FutureContextListener` and take the `Channel` as a context.
* Remove `ChannelPromise` and `ChannelFuture` — all usages now rely on the plain `Future` and `Promise` APIs.
* Add static factory methods to `DefaultPromise` that allow us to create promises that are initialised as successful or failed.
* Remove `CompleteFuture`, `SucceededFuture`, `FailedFuture`, `CompleteChannelFuture`, `SucceededChannelFuture`, and `FailedChannelFuture`.
* Remove `ChannelPromiseNotifier`.
Result:
Cleaner generics and more straight forward code.
2021-08-20 09:55:16 +02:00
|
|
|
return newRunnableFuture(newPromise(), runnable, value);
|
2013-03-22 01:06:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-01-23 08:32:05 +01:00
|
|
|
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
|
Clean up Future/Promises API (#11575)
Motivation:
The generics for the existing futures, promises, and listeners are too complicated.
This complication comes from the existence of `ChannelPromise` and `ChannelFuture`, which forces listeners to care about the particular _type_ of future being listened on.
Modification:
* Add a `FutureContextListener` which can take a context object as an additional argument. This allows our listeners to have the channel piped through to them, so they don't need to rely on the `ChannelFuture.channel()` method.
* Make the `FutureListener`, along with the `FutureContextListener` sibling, the default listener API, retiring the `GenericFutureListener` since we no longer need to abstract over the type of the future.
* Change all uses of `ChannelPromise` to `Promise<Void>`.
* Change all uses of `ChannelFuture` to `Future<Void>`.
* Change all uses of `GenericFutureListener` to either `FutureListener` or `FutureContextListener` as needed.
* Remove `ChannelFutureListener` and `GenericFutureListener`.
* Introduce a `ChannelFutureListeners` enum to house the constants that previously lived in `ChannelFutureListener`. These constants now implement `FutureContextListener` and take the `Channel` as a context.
* Remove `ChannelPromise` and `ChannelFuture` — all usages now rely on the plain `Future` and `Promise` APIs.
* Add static factory methods to `DefaultPromise` that allow us to create promises that are initialised as successful or failed.
* Remove `CompleteFuture`, `SucceededFuture`, `FailedFuture`, `CompleteChannelFuture`, `SucceededChannelFuture`, and `FailedChannelFuture`.
* Remove `ChannelPromiseNotifier`.
Result:
Cleaner generics and more straight forward code.
2021-08-20 09:55:16 +02:00
|
|
|
return newRunnableFuture(newPromise(), callable);
|
2013-03-11 08:38:56 +01:00
|
|
|
}
|
2016-07-19 20:39:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to execute the given {@link Runnable} and just log if it throws a {@link Throwable}.
|
|
|
|
*/
|
2019-01-23 08:32:05 +01:00
|
|
|
static void safeExecute(Runnable task) {
|
2016-07-19 20:39:44 +02:00
|
|
|
try {
|
|
|
|
task.run();
|
|
|
|
} catch (Throwable t) {
|
|
|
|
logger.warn("A task raised an exception. Task: {}", task, t);
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 08:32:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new {@link RunnableFuture} build on top of the given {@link Promise} and {@link Callable}.
|
|
|
|
*
|
|
|
|
* This can be used if you want to override {@link #newTaskFor(Callable)} and return a different
|
|
|
|
* {@link RunnableFuture}.
|
|
|
|
*/
|
|
|
|
private static <V> RunnableFuture<V> newRunnableFuture(Promise<V> promise, Callable<V> task) {
|
|
|
|
return new RunnableFutureAdapter<>(promise, task);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new {@link RunnableFuture} build on top of the given {@link Promise} and {@link Runnable} and
|
|
|
|
* {@code value}.
|
|
|
|
*
|
|
|
|
* This can be used if you want to override {@link #newTaskFor(Runnable, V)} and return a different
|
|
|
|
* {@link RunnableFuture}.
|
|
|
|
*/
|
|
|
|
private static <V> RunnableFuture<V> newRunnableFuture(Promise<V> promise, Runnable task, V value) {
|
|
|
|
return new RunnableFutureAdapter<>(promise, Executors.callable(task, value));
|
|
|
|
}
|
2013-03-11 08:38:56 +01:00
|
|
|
}
|