users@grizzly.java.net

Re: Async Response sending

From: Igor Minar <iiminar_at_gmail.com>
Date: Mon, 1 Sep 2008 17:16:54 -0700

On Sep 1, 2008, at 11:06 AM, hsn_at_sendmail.cz wrote:

> i have following filter, it should do async response sending and
> close connection. It more or less works but
> it have 2 issues:
>
> 1. after executing key.channel().close() it takes about 1 second to
> get socket closed from view of other computer
> on the same LAN. Other java apps (i mean not async) close socket
> immediately. Can i do something to make
> close faster?

That's because the close operation is asynchronous and is executed by
the selector thread. You can wakeup() the selector thread after you
call close() to speed up the process.

> 2. Can i somewhat stop receiving more requests on socket during my
> async reply sending? If i send 2 fast requests both are executed, i
> want to get second ignored, so i need to deregister OP_READ on
> selector()?

yes

/i

>
>
> will postExecute(Context ctx)
> ctx.setKeyRegistrationState(KeyRegistrationState.NONE); return
> false; do it?
>
> public class AsyncReplySender implements ProtocolFilter {
>
> class writeCallback implements AsyncWriteCallbackHandler {
>
> public void onIOException(IOException ioException,
> SelectionKey key,
> ByteBuffer buffer, Queue<AsyncWriteQueueRecord>
> remainingQueue) {
> onWriteCompleted(key, buffer);
> }
>
> public void onWriteCompleted(SelectionKey key, ByteBuffer
> buffer) {
> try {
> key.channel().close();
> }
> catch (IOException e) {
> // ignore it
> }
> // key.cancel();
> System.out.println("key canceled");
> }
> }
>
> private writeCallback wcallback;
>
> public boolean execute(Context ctx) throws IOException {
> ByteBuffer towrite;
> String response;
> response = (String) ctx.getAttribute(RESPONSE);
> System.out.println(response);
> towrite = ByteBuffer.allocate(response.length());
> towrite.put(response.getBytes("ascii"));
> towrite.flip();
> AsyncQueueWritable wqueue = ctx.getAsyncQueueWritable();
> if ( wcallback == null )
> wcallback = new writeCallback();
> wqueue.writeToAsyncQueue(towrite, wcallback);
> return false;
> }
>
> public boolean postExecute(Context ctx) throws IOException {
> // ctx.setKeyRegistrationState(KeyRegistrationState.NONE);
> return true;
> }
>
> }