use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener in project grpc-java by grpc.
the class AltsProtocolNegotiatorTest method flushShouldFailAllPromises.
@Test
public void flushShouldFailAllPromises() throws Exception {
doHandshake();
channel.pipeline().addFirst(new ChannelDuplexHandler() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
throw new Exception("Fake exception");
}
});
// Write the message 1 character at a time.
String message = "hello";
final AtomicInteger failures = new AtomicInteger();
for (int ix = 0; ix < message.length(); ++ix) {
ByteBuf in = Unpooled.copiedBuffer(message, ix, 1, UTF_8);
// go/futurereturn-lsc
@SuppressWarnings("unused") Future<?> possiblyIgnoredError = channel.write(in).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
failures.incrementAndGet();
}
}
});
}
channel.flush();
// Verify that the promises fail.
assertEquals(message.length(), failures.get());
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener in project grpc-java by grpc.
the class WriteBufferingAndExceptionHandler method exceptionCaught.
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
assert cause != null;
Throwable previousFailure = failCause;
Status status = Utils.statusFromThrowable(cause).augmentDescription("Channel Pipeline: " + ctx.pipeline().names());
failWrites(status.asRuntimeException());
// 4d. active, prev!=null[connect]: impossible, channel can't be active after a failed connect.
if (ctx.channel().isActive() && previousFailure == null) {
final class LogOnFailure implements ChannelFutureListener {
@Override
public void operationComplete(ChannelFuture future) {
if (!future.isSuccess()) {
logger.log(Level.FINE, "Failed closing channel", future.cause());
}
}
}
ctx.close().addListener(new LogOnFailure());
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener in project Glowstone by GlowstoneMC.
the class HttpClient method connect.
/**
* Opens a URL.
*
* @param url the URL to download
* @param eventLoop an {@link EventLoop} that will receive the response body
* @param callback a callback to handle the response or any error
*/
public void connect(String url, EventLoop eventLoop, HttpCallback callback) {
URI uri = URI.create(url);
String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
int port = uri.getPort();
SslContext sslCtx = null;
if ("https".equalsIgnoreCase(scheme)) {
if (port == -1) {
port = 443;
}
try {
sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
} catch (SSLException e) {
callback.error(e);
return;
}
} else if ("http".equalsIgnoreCase(scheme)) {
if (port == -1) {
port = 80;
}
} else {
throw new IllegalArgumentException("Only http(s) is supported!");
}
new Bootstrap().group(eventLoop).resolver(resolverGroup).channel(Networking.bestSocketChannel()).handler(new HttpChannelInitializer(sslCtx, callback)).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000).connect(InetSocketAddress.createUnresolved(host, port)).addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
String path = uri.getRawPath() + (uri.getRawQuery() == null ? "" : "?" + uri.getRawQuery());
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
request.headers().set(HttpHeaderNames.HOST, host);
future.channel().writeAndFlush(request);
} else {
callback.error(future.cause());
}
});
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener in project mongo-java-driver by mongodb.
the class NettyStream method writeAsync.
@Override
public void writeAsync(final List<ByteBuf> buffers, final AsyncCompletionHandler<Void> handler) {
CompositeByteBuf composite = PooledByteBufAllocator.DEFAULT.compositeBuffer();
for (ByteBuf cur : buffers) {
composite.addComponent(true, ((NettyByteBuf) cur).asByteBuf());
}
channel.writeAndFlush(composite).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(final ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
handler.failed(future.cause());
} else {
handler.completed(null);
}
}
});
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener in project vert.x by eclipse.
the class Http2ConnectionBase method close.
@Override
public Future<Void> close() {
PromiseInternal<Void> promise = context.promise();
ChannelPromise pr = chctx.newPromise();
ChannelPromise channelPromise = pr.addListener(promise);
handlerContext.writeAndFlush(Unpooled.EMPTY_BUFFER, pr);
channelPromise.addListener((ChannelFutureListener) future -> shutdown(0L));
return promise.future();
}
Aggregations