Cau,
> 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?
I would agree with Igor, this could be connected to the selector,
which has to be woke up. Though, IMHO, there is more brutal way to
close the connection:
if (channel instanceof SocketChannel) {
Socket socket = ((SocketChannel) channel).socket();
try {
if (!socket.isInputShutdown()) socket.shutdownInput();
} catch (IOException ex){
;
}
try {
if (!socket.isOutputShutdown())
socket.shutdownOutput();
} catch (IOException ex){
;
}
try{
socket.close();
} catch (IOException ex){
;
}
}
try{
channel.close();
} catch (IOException ex){
; // LOG ME
}
>
> 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?
Absolutely. It should work for you.
WBR,
Alexey.
>
>
> 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;
> }
>
> }