CompletionHandler should take the channel too

Rémi Forax forax at univ-mlv.fr
Tue Apr 21 15:36:08 PDT 2009


Hi Alan, hi all,
Writing a stateless completion handler as suggested in the doc is not
that easy because the completion handler doesn't provide a way to get
the async channel on which the async operation was called.

This is not that stupid, if you take a look to the selector's SelectionKey,
it provides the channel.

So I propose to add a new type parameter to the CompletionHandler:
public interface CompletionHandler<V, C extends AsyncronousChannel, A> {
    void completed(V result, C asyncChannel, A attachment);
    void failed(Throwable exc, C asyncChannel, A attachment);
    void cancelled(C asyncChannel, A attachment);
}

and read on an AsyncSocketChannel becomes:
public abstract <A> Future<Integer> read(ByteBuffer dst,
                                             long timeout,
                                             TimeUnit unit,
                                             A attachment,
                                             CompletionHandler<Integer, 
AsyncSocketChannel, ? super A> handler);

The current implementation already store the async channel in 
AbstractFuture,
so it doesn't seem to be a great deal (perhaps a small nightmare if you 
want to
parameterize AbstractFuture with the type of AsyncChannel, but I'm
not sure it worth it).

Rémi



More information about the nio-dev mailing list