use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project flink by splunk.
the class RestServerEndpoint method shutDownInternal.
/**
* Stops this REST server endpoint.
*
* @return Future which is completed once the shut down has been finished.
*/
protected CompletableFuture<Void> shutDownInternal() {
synchronized (lock) {
CompletableFuture<?> channelFuture = new CompletableFuture<>();
if (serverChannel != null) {
serverChannel.close().addListener(finished -> {
if (finished.isSuccess()) {
channelFuture.complete(null);
} else {
channelFuture.completeExceptionally(finished.cause());
}
});
serverChannel = null;
}
final CompletableFuture<Void> channelTerminationFuture = new CompletableFuture<>();
channelFuture.thenRun(() -> {
CompletableFuture<?> groupFuture = new CompletableFuture<>();
CompletableFuture<?> childGroupFuture = new CompletableFuture<>();
final Time gracePeriod = Time.seconds(10L);
if (bootstrap != null) {
final ServerBootstrapConfig config = bootstrap.config();
final EventLoopGroup group = config.group();
if (group != null) {
group.shutdownGracefully(0L, gracePeriod.toMilliseconds(), TimeUnit.MILLISECONDS).addListener(finished -> {
if (finished.isSuccess()) {
groupFuture.complete(null);
} else {
groupFuture.completeExceptionally(finished.cause());
}
});
} else {
groupFuture.complete(null);
}
final EventLoopGroup childGroup = config.childGroup();
if (childGroup != null) {
childGroup.shutdownGracefully(0L, gracePeriod.toMilliseconds(), TimeUnit.MILLISECONDS).addListener(finished -> {
if (finished.isSuccess()) {
childGroupFuture.complete(null);
} else {
childGroupFuture.completeExceptionally(finished.cause());
}
});
} else {
childGroupFuture.complete(null);
}
bootstrap = null;
} else {
// complete the group futures since there is nothing to stop
groupFuture.complete(null);
childGroupFuture.complete(null);
}
CompletableFuture<Void> combinedFuture = FutureUtils.completeAll(Arrays.asList(groupFuture, childGroupFuture));
combinedFuture.whenComplete((Void ignored, Throwable throwable) -> {
if (throwable != null) {
channelTerminationFuture.completeExceptionally(throwable);
} else {
channelTerminationFuture.complete(null);
}
});
});
return channelTerminationFuture;
}
}
use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project flink by splunk.
the class RestClient method submitRequest.
private <P extends ResponseBody> CompletableFuture<P> submitRequest(String targetAddress, int targetPort, Request httpRequest, JavaType responseType) {
final ChannelFuture connectFuture = bootstrap.connect(targetAddress, targetPort);
final CompletableFuture<Channel> channelFuture = new CompletableFuture<>();
connectFuture.addListener((ChannelFuture future) -> {
if (future.isSuccess()) {
channelFuture.complete(future.channel());
} else {
channelFuture.completeExceptionally(future.cause());
}
});
return channelFuture.thenComposeAsync(channel -> {
ClientHandler handler = channel.pipeline().get(ClientHandler.class);
CompletableFuture<JsonResponse> future;
boolean success = false;
try {
if (handler == null) {
throw new IOException("Netty pipeline was not properly initialized.");
} else {
httpRequest.writeTo(channel);
future = handler.getJsonFuture();
success = true;
}
} catch (IOException e) {
future = FutureUtils.completedExceptionally(new ConnectionException("Could not write request.", e));
} finally {
if (!success) {
channel.close();
}
}
return future;
}, executor).thenComposeAsync((JsonResponse rawResponse) -> parseResponse(rawResponse, responseType), executor);
}
use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project aws-sdk-java-v2 by aws.
the class HealthCheckedChannelPoolTest method stubAcquireTwiceFirstTimeNotKeepAlive.
private void stubAcquireTwiceFirstTimeNotKeepAlive() {
OngoingStubbing<Future<Channel>> stubbing = Mockito.when(downstreamChannelPool.acquire(any()));
stubbing = stubbing.thenAnswer(invocation -> {
Promise<Channel> promise = invocation.getArgument(0, Promise.class);
Channel channel = Mockito.mock(Channel.class);
stubKeepAliveAttribute(channel, false);
Mockito.when(channel.isActive()).thenReturn(true);
channels.add(channel);
promise.setSuccess(channel);
return promise;
});
stubbing.thenAnswer(invocation -> {
Promise<Channel> promise = invocation.getArgument(0, Promise.class);
Channel channel = Mockito.mock(Channel.class);
Mockito.when(channel.isActive()).thenReturn(true);
channels.add(channel);
promise.setSuccess(channel);
stubKeepAliveAttribute(channel, true);
return promise;
});
}
use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project aws-sdk-java-v2 by aws.
the class Http2MultiplexedChannelPool method acquireStreamOnNewConnection.
private void acquireStreamOnNewConnection(Promise<Channel> promise) {
Future<Channel> newConnectionAcquire = connectionPool.acquire();
newConnectionAcquire.addListener(f -> {
if (!newConnectionAcquire.isSuccess()) {
promise.setFailure(newConnectionAcquire.cause());
return;
}
Channel parentChannel = newConnectionAcquire.getNow();
try {
parentChannel.attr(HTTP2_MULTIPLEXED_CHANNEL_POOL).set(this);
// When the protocol future is completed on the new connection, we're ready for new streams to be added to it.
parentChannel.attr(PROTOCOL_FUTURE).get().thenAccept(protocol -> acquireStreamOnFreshConnection(promise, parentChannel, protocol)).exceptionally(throwable -> failAndCloseParent(promise, parentChannel, throwable));
} catch (Throwable e) {
failAndCloseParent(promise, parentChannel, e);
}
});
}
use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project aws-sdk-java-v2 by aws.
the class CancellableAcquireChannelPool method acquire.
@Override
public Future<Channel> acquire(Promise<Channel> acquirePromise) {
Future<Channel> channelFuture = delegatePool.acquire(executor.newPromise());
channelFuture.addListener((Future<Channel> f) -> {
if (f.isSuccess()) {
Channel ch = f.getNow();
if (!acquirePromise.trySuccess(ch)) {
ch.close().addListener(closeFuture -> delegatePool.release(ch));
}
} else {
acquirePromise.tryFailure(f.cause());
}
});
return acquirePromise;
}
Aggregations