why not both Future and CompletionHandler?
Sangjin Lee
sjlee at apache.org
Thu Apr 14 08:20:11 PDT 2011
Yes, I think it can come very handy if you want to add some cross-cutting
concerns in the completion handlers but allow the end users to interact with
the result via Future.
For example, suppose I build a framework/library for others using the async
channels (HTTP app, ssh app, etc.). I want to allow my users freedom to
choose between the "pull" (future) and the "push" (completion handler).
However, *regardless* of what my end user choose to interact with the
result, I want to be able to inject some concerns like logging,
instrumenting, etc. in the form of a completion handler (can be completely
internal to the framework).
With the current APIs, if the user wants a future, the framework cannot use
the completion handler and I would need to find another way to inject such
logic. Or if I want to retain the completion handler for the framework, then
I would need to create my own future objects to provide to the end users.
Either approach is doable, but it would be so much easier if the NIO.2 APIs
provide both mechanisms in a single method call. My 2 cents. Thanks!
Regards,
Sangjin
On Thu, Apr 14, 2011 at 7:54 AM, Alan Bateman <Alan.Bateman at oracle.com>wrote:
> Sangjin Lee wrote:
>
>> Hi there,
>>
>> This may have been discussed in the past, but my googling didn't turn up a
>> discussion...
>>
>> I've been looking at the I/O methods on the AsynchronousSocketChannel, and
>> noticed that they let you interact with *either* Future or
>> CompletionHandler, but not both. Each method flavor seems to have a pair (or
>> more) of overloaded methods that lets you interact with the result only one
>> way. Is there a reason not to use methods that allow you to use both
>> mechanisms (Future and CompletionHandler) instead? For example,
>>
>> public Future<Void> connect(SocketAddress remote, A attachment,
>> CompletionHandler<Void,? super A> handler);
>>
> That's right, you can use a Future or you specify a completion handler to
> consume the result. Do you have examples where you would want to consume the
> result of an I/O operation in a completion handler and at the same time wait
> or poll for the result?
>
> -Alan.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/nio-dev/attachments/20110414/5c2cb87b/attachment.html
More information about the nio-dev
mailing list