下面列出了io.netty.util.concurrent.Future#await ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void close() {
if (closed.compareAndSet(false, true)) {
Future<?> closeCompleteFuture = doClose();
try {
if (!closeCompleteFuture.await(10, TimeUnit.SECONDS)) {
throw new RuntimeException("Event loop didn't close after 10 seconds.");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
Throwable exception = closeCompleteFuture.cause();
if (exception != null) {
throw new RuntimeException("Failed to close channel pool.", exception);
}
}
}
@Override
public void close() {
if (closed.compareAndSet(false, true)) {
Future<?> closeCompleteFuture = doClose();
try {
if (!closeCompleteFuture.await(10, TimeUnit.SECONDS)) {
throw new RuntimeException("Event loop didn't close after 10 seconds.");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
Throwable exception = closeCompleteFuture.cause();
if (exception != null) {
throw new RuntimeException("Failed to close channel pool.", exception);
}
}
}
@SuppressWarnings("FutureReturnValueIgnored")
public void close() {
LOG.debug("Closing Netty acceptor...");
if (workerGroup == null || bossGroup == null) {
LOG.error("Netty acceptor is not initialized");
throw new IllegalStateException("Invoked close on an Acceptor that wasn't initialized");
}
Future<?> workerWaiter = workerGroup.shutdownGracefully();
Future<?> bossWaiter = bossGroup.shutdownGracefully();
/*
* We shouldn't raise an IllegalStateException if we are interrupted. If we did so, the
* broker is not shut down properly.
*/
LOG.info("Waiting for worker and boss event loop groups to terminate...");
try {
workerWaiter.await(10, TimeUnit.SECONDS);
bossWaiter.await(10, TimeUnit.SECONDS);
} catch (InterruptedException iex) {
LOG.warn("An InterruptedException was caught while waiting for event loops to terminate...");
}
if (!workerGroup.isTerminated()) {
LOG.warn("Forcing shutdown of worker event loop...");
workerGroup.shutdownGracefully(0L, 0L, TimeUnit.MILLISECONDS);
}
if (!bossGroup.isTerminated()) {
LOG.warn("Forcing shutdown of boss event loop...");
bossGroup.shutdownGracefully(0L, 0L, TimeUnit.MILLISECONDS);
}
}
@AfterClass
public static void afterClass() throws InterruptedException {
Future<?> group1Future = group1.shutdownGracefully(0, 0, SECONDS);
Future<?> group2Future = group2.shutdownGracefully(0, 0, SECONDS);
Future<?> sharedGroupFuture = sharedGroup.shutdownGracefully(0, 0, SECONDS);
group1Future.await();
group2Future.await();
sharedGroupFuture.await();
}
private static void await(Future<?> future)
{
try {
future.await();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
public void stop() throws InterruptedException {
logger.debug("Requesting the server to stop.");
Future<?> bossWait = bossGroup.shutdownGracefully(0, SHUTDOWN_TIMEOUT_SECONDS, TimeUnit.SECONDS);
Future<?> workWait = workGroup.shutdownGracefully(0, SHUTDOWN_TIMEOUT_SECONDS, TimeUnit.SECONDS);
bossWait.await();
workWait.await();
logger.debug("Server.stopped");
}
@Override
public void close() {
final Future<?> future = eventLoopGroup.shutdownGracefully();
try {
logger.debug("shutdown {}-eventLoopGroup", factoryName);
future.await(1000 * 3);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
ExecutorUtils.shutdownExecutorService(factoryName + "-eventLoopExecutor", eventLoopExecutor);
ExecutorUtils.shutdownExecutorService(factoryName + "-executorService", executorService);
}