Skip to content
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

[Netty 5] Lambda-friendly future listeners #9353

Open
Bennett-Lynch opened this issue Jul 11, 2019 · 2 comments
Open

[Netty 5] Lambda-friendly future listeners #9353

Bennett-Lynch opened this issue Jul 11, 2019 · 2 comments

Comments

@Bennett-Lynch
Copy link
Contributor

Java 8, which introduced lambdas, allows for the following code:

channel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
    @Override
    public void operationComplete(ChannelFuture future) {
        ...
    }
});

to become:

channel.writeAndFlush(msg).addListener((ChannelFutureListener) future -> {
    ...
});

However, this requires adding a type declaration to the lambda parameter, e.g., (ChannelFutureListener).

Given the prominence of ChannelFutureListener, would it be worth adding a method signature to ChannelFuture that explicitly supports ChannelFutureListener?

Perhaps it would actually be worth renaming the current addListener in the Future interface to addGenericListener and then introducing type-explicit methods in extensions of Future.

E.g., the ChannelFuture interface would add:

ChannelFuture addListener(ChannelFutureListener listener);

Resulting in the slightly less verbose:

channel.writeAndFlush(msg).addListener(future -> {
    ...
});
@normanmaurer
Copy link
Member

@Bennett-Lynch yeah... That said I think we may want to cleanup all of this a bit for Netty 5. See https://github.com/netty/netty/milestone/195

@Bennett-Lynch
Copy link
Contributor Author

Bennett-Lynch commented Aug 1, 2019

Thanks, @normanmaurer.

If I can sneak one more feature request in here, I'd also like to see onSuccess() and onFailure() convenience listeners added. E.g.,

ctx.write(msg).addListener((ChannelFutureListener) future -> {
    if (future.isSuccess()) {
        ...
    }
});

would become:

ctx.write(msg).onSuccess((ChannelFutureListener) future -> {
    ...
});

And ChannelFutureListener.CLOSE_ON_FAILURE could be deprecated in favor of:

ctx.write(msg).onFailure(ChannelFutureListener.CLOSE);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants