The CompletionHandler<V,A> Interface in Asynchronous context

向雅 fyaoxy at gmail.com
Wed Nov 11 17:55:03 PST 2009


Hi,

The CompletionHandler interface have notification method:
	public void completed(Integer result, T attachment);

In common case, read and write operation coding seems OK.
In most code, if handle the read operation, you should or must handle
result==-1 case.
And even if result==-1, maybe you still need check the read buffer to
see if have some incoming data.
so the code snippet veeeery common:
@Override
public void completed(Integer result, T attachment){
  if(result==-1){
     ...check buffer if has data incoming.
     ...if has data doSomething,
    ...over it.
  }
}

@Override
public void completed(Integer result, T attachment){
  if(result!=-1){ doSomething; return;  }
     ...check buffer if has data incoming.
     ...if has data doSomething,
    ...over it.
}

In the asynchronous context, that kind of resulting seems not so brilliant.
if the let the code more simple, additional method need which only for
read operation, and for more compatibility, just use a new
ReadCompletionHandler interface. maybe like this:

public interface ReadCompletionHandler<T> extends CompletionHandler<Integer, T>{
//this method only call if the channel read all data over.
void completed(T attachment);
}

And the internal ReadTask implementation just add a interface type and
-1 value check.
So, the above code snippet will not present. everybody more happy to
code complete.

Any thoughts?

Regards,
qinxian


More information about the nio-dev mailing list