GeezerVison is showing github.com — Replace *FutureListener* usage with Lambda where possible by normanmaurer · Pull Request #16032 · netty/netty
Replace *FutureListener* usage with Lambda where possible
#16032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.java
(99)
.json
(3)
All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -223,12 +223,9 @@ protected void decode(final ChannelHandlerContext ctx, I msg, List<Object> out)
// Cache the write listener for reuse.
ChannelFutureListener listener = continueResponseWriteListener;
if (listener == null) {
continueResponseWriteListener = listener = new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
if (!future.isSuccess()) {
ctx.fireExceptionCaught(future.cause());
}
continueResponseWriteListener = listener = future -> {
if (!future.isSuccess()) {
ctx.fireExceptionCaught(future.cause());
}
};
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -37,14 +37,11 @@ public void run() {
}
}, THREAD_POOL_DELAY_SECONDS, TimeUnit.SECONDS);
finishFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture f) {
// Cancel the scheduled timeout.
future.cancel(true);
if (!promise.isDone()) {
ctx.close(promise);
}
finishFuture.addListener(f -> {
// Cancel the scheduled timeout.
future.cancel(true);
if (!promise.isDone()) {
ctx.close(promise);
}
});
} else {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -337,14 +337,11 @@ public void run() {
}
}, THREAD_POOL_DELAY_SECONDS, TimeUnit.SECONDS);
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture f) {
// Cancel the scheduled timeout.
future.cancel(true);
if (!promise.isDone()) {
ctx.close(promise);
}
f.addListener(f1 -> {
// Cancel the scheduled timeout.
future.cancel(true);
if (!promise.isDone()) {
ctx.close(promise);
}
});
} else {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -280,16 +280,13 @@ public void run() {
final int size = 27;
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(size, size);
finalClientChannel.writeAndFlush(buf.writerIndex(buf.writerIndex() + size))
.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
try {
writeFailCauseRef.set(future.cause());
} finally {
latch.countDown();
}
}
});
.addListener(future -> {
try {
writeFailCauseRef.set(future.cause());
} finally {
latch.countDown();
}
});
}
});
latch.await();
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -18,7 +18,6 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
Expand Down
Expand Up
@@ -249,23 +248,17 @@ protected void handleOversizedMessage(final ChannelHandlerContext ctx, HttpMessa
if (oversized instanceof FullHttpMessage ||
!HttpUtil.is100ContinueExpected(oversized) && !HttpUtil.isKeepAlive(oversized)) {
ChannelFuture future = ctx.writeAndFlush(TOO_LARGE_CLOSE.retainedDuplicate());
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
if (!future.isSuccess()) {
logger.debug("Failed to send a 413 Request Entity Too Large.", future.cause());
}
ctx.close();
future.addListener(f -> {
if (!f.isSuccess()) {
logger.debug("Failed to send a 413 Request Entity Too Large.", f.cause());
}
ctx.close();
});
} else {
ctx.writeAndFlush(TOO_LARGE.retainedDuplicate()).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
if (!future.isSuccess()) {
logger.debug("Failed to send a 413 Request Entity Too Large.", future.cause());
ctx.close();
}
ctx.writeAndFlush(TOO_LARGE.retainedDuplicate()).addListener(future -> {
if (!future.isSuccess()) {
logger.debug("Failed to send a 413 Request Entity Too Large.", future.cause());
ctx.close();
}
});
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -287,7 +287,7 @@ public ChannelFuture handshake(Channel channel) {
* the {@link ChannelPromise} to be notified when the opening handshake is sent
*/
public final ChannelFuture handshake(Channel channel, final ChannelPromise promise) {
ChannelPipeline pipeline = channel.pipeline();
final ChannelPipeline pipeline = channel.pipeline();
HttpResponseDecoder decoder = pipeline.get(HttpResponseDecoder.class);
if (decoder == null) {
HttpClientCodec codec = pipeline.get(HttpClientCodec.class);
Expand Down
Expand Up
@@ -322,26 +322,22 @@ public final ChannelFuture handshake(Channel channel, final ChannelPromise promi
FullHttpRequest request = newHandshakeRequest();
channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
if (future.isSuccess()) {
ChannelPipeline p = future.channel().pipeline();
ChannelHandlerContext ctx = p.context(HttpRequestEncoder.class);
if (ctx == null) {
ctx = p.context(HttpClientCodec.class);
}
if (ctx == null) {
promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " +
"an HttpRequestEncoder or HttpClientCodec"));
return;
}
p.addAfter(ctx.name(), "ws-encoder", newWebSocketEncoder());
promise.setSuccess();
} else {
promise.setFailure(future.cause());
channel.writeAndFlush(request).addListener(future -> {
if (future.isSuccess()) {
ChannelHandlerContext ctx = pipeline.context(HttpRequestEncoder.class);
if (ctx == null) {
ctx = pipeline.context(HttpClientCodec.class);
}
if (ctx == null) {
promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " +
"an HttpRequestEncoder or HttpClientCodec"));
return;
}
pipeline.addAfter(ctx.name(), "ws-encoder", newWebSocketEncoder());
promise.setSuccess();
} else {
promise.setFailure(future.cause());
}
});
return promise;
Expand Down
Expand Up
@@ -687,32 +683,24 @@ private ChannelFuture close0(final ChannelOutboundInvoker invoker, final Channel
return promise;
}
promise.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
// If flush operation failed, there is no reason to expect
// a server to receive CloseFrame. Thus this should be handled
// by the application separately.
// Also, close might be called twice from different threads.
if (future.isSuccess() && channel.isActive() &&
FORCE_CLOSE_INIT_UPDATER.compareAndSet(handshaker, 0, 1)) {
final Future<?> forceCloseFuture = channel.executor().schedule(new Runnable() {
@Override
public void run() {
if (channel.isActive()) {
invoker.close();
forceCloseComplete = true;
}
promise.addListener(future -> {
// If flush operation failed, there is no reason to expect
// a server to receive CloseFrame. Thus this should be handled
// by the application separately.
// Also, close might be called twice from different threads.
if (future.isSuccess() && channel.isActive() &&
FORCE_CLOSE_INIT_UPDATER.compareAndSet(handshaker, 0, 1)) {
final Future<?> forceCloseFuture = channel.executor().schedule(new Runnable() {
@Override
public void run() {
if (channel.isActive()) {
invoker.close();
forceCloseComplete = true;
}
}, forceCloseTimeoutMillis, TimeUnit.MILLISECONDS);
}
}, forceCloseTimeoutMillis, TimeUnit.MILLISECONDS);
channel.closeFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
forceCloseFuture.cancel(false);
}
});
}
channel.closeFuture().addListener(f -> forceCloseFuture.cancel(false));
}
});
return promise;
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -16,14 +16,12 @@
package io.netty.handler.codec.http.websocketx;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler.ClientHandshakeStateEvent;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import java.util.concurrent.TimeUnit;
Expand Down
Expand Up
@@ -55,16 +53,13 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
ctx.fireChannelActive();
handshaker.handshake(ctx.channel()).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
if (!future.isSuccess()) {
handshakePromise.tryFailure(future.cause());
ctx.fireExceptionCaught(future.cause());
} else {
ctx.fireUserEventTriggered(
WebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_ISSUED);
}
handshaker.handshake(ctx.channel()).addListener(future -> {
if (!future.isSuccess()) {
handshakePromise.tryFailure(future.cause());
ctx.fireExceptionCaught(future.cause());
} else {
ctx.fireUserEventTriggered(
ClientHandshakeStateEvent.HANDSHAKE_ISSUED);
}
});
applyHandshakeTimeout();
Expand Down
Expand Up
@@ -125,12 +120,7 @@ public void run() {
}, handshakeTimeoutMillis, TimeUnit.MILLISECONDS);
// Cancel the handshake timeout when handshake is finished.
localHandshakePromise.addListener(new FutureListener<Void>() {
@Override
public void operationComplete(Future<Void> f) {
timeoutFuture.cancel(false);
}
});
localHandshakePromise.addListener(f -> timeoutFuture.cancel(false));
}
/**
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -99,12 +99,7 @@ public void close(final ChannelHandlerContext ctx, final ChannelPromise promise)
}
flush(ctx);
applyCloseSentTimeout(ctx);
closeSent.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
ctx.close(promise);
}
});
closeSent.addListener(future -> ctx.close(promise));
}
}
Expand Down
Expand Up
@@ -139,12 +134,7 @@ public void run() {
}
}, forceCloseTimeoutMillis, TimeUnit.MILLISECONDS);
closeSent.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
timeoutTask.cancel(false);
}
});
closeSent.addListener(future -> timeoutTask.cancel(false));
}
/**
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -228,16 +228,13 @@ public final ChannelFuture handshake(Channel channel, FullHttpRequest req,
encoderName = p.context(HttpResponseEncoder.class).name();
p.addBefore(encoderName, "wsencoder", newWebSocketEncoder());
}
channel.writeAndFlush(response).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
if (future.isSuccess()) {
ChannelPipeline p = future.channel().pipeline();
p.remove(encoderName);
promise.setSuccess();
} else {
promise.setFailure(future.cause());
}
channel.writeAndFlush(response).addListener(future -> {
if (future.isSuccess()) {
ChannelPipeline p1 = channel.pipeline();
p1.remove(encoderName);
promise.setSuccess();
} else {
promise.setFailure(future.cause());
}
});
return promise;
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -16,7 +16,6 @@
package io.netty.handler.codec.http.websocketx;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelPipeline;
Expand All
@@ -28,7 +27,6 @@
import io.netty.handler.ssl.SslHandler;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import java.util.concurrent.TimeUnit;
Expand Down
Expand Up
@@ -84,21 +82,18 @@ public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exce
ctx.pipeline().remove(this);
final ChannelFuture handshakeFuture = handshaker.handshake(ctx.channel(), req);
handshakeFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
if (!future.isSuccess()) {
localHandshakePromise.tryFailure(future.cause());
ctx.fireExceptionCaught(future.cause());
} else {
localHandshakePromise.trySuccess();
// Kept for compatibility
ctx.fireUserEventTriggered(
WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE);
ctx.fireUserEventTriggered(
new WebSocketServerProtocolHandler.HandshakeComplete(
req.uri(), req.headers(), handshaker.selectedSubprotocol()));
}
handshakeFuture.addListener(future -> {
if (!future.isSuccess()) {
localHandshakePromise.tryFailure(future.cause());
ctx.fireExceptionCaught(future.cause());
} else {
localHandshakePromise.trySuccess();
// Kept for compatibility
ctx.fireUserEventTriggered(
ServerHandshakeStateEvent.HANDSHAKE_COMPLETE);
ctx.fireUserEventTriggered(
new WebSocketServerProtocolHandler.HandshakeComplete(
req.uri(), req.headers(), handshaker.selectedSubprotocol()));
}
});
applyHandshakeTimeout();
Expand Down
Expand Up
@@ -160,11 +155,6 @@ public void run() {
}, handshakeTimeoutMillis, TimeUnit.MILLISECONDS);
// Cancel the handshake timeout when handshake is finished.
localHandshakePromise.addListener(new FutureListener<Void>() {
@Override
public void operationComplete(Future<Void> f) {
timeoutFuture.cancel(false);
}
});
localHandshakePromise.addListener(f -> timeoutFuture.cancel(false));
}
}
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.