users@grizzly.java.net

Async Response sending

From: <hsn_at_sendmail.cz>
Date: Mon, 01 Sep 2008 20:06:56 +0200

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?

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()?

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;
    }

}