1

I'm reading an http response using the apache async client. Every time I read a chunk of data I want to write it to the servletoutputstream in a non-blocking mode. Something like this:

// decoder.read is executed when data available for reading
while (decoder.read(this.bbuf) > 0)
{
 this.bbuf.flip();

 arr = new byte[numbytesread];
 this.bbuf.rewind();
 this.bbuf.get(arr);

 // Blocking write to servletoutputstream 'sos'
 this.sos.write(arr);

 this.bbuf.compact();
}

Obviously this does not work even if I wrap the 'sos' in a WritableChannel, because I always end up with the bytearrayoutputstream from the servletoutputstream.

So I should add a WriteListener to the servletoutputstream to switch to nio mode, but here comes the problem I'm not able to solve. How can I pass every chunk of data from my http callback to the writelistener to work asynchronus and non blocking?

Is this possible? If so, can anyone give me a clue about how to do it?

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
JBalaguero
  • 181
  • 2
  • 13
  • Use the producer-consummer pattern, store your chunks into a stack, from where you will take them to feed the servletoutputstream. – Nadir Mar 30 '16 at 13:26
  • I was thinking something like this, but I lose the non-blocking mode so the write listener has to block on the queue waiting for an available packet. – JBalaguero Mar 30 '16 at 13:31

0 Answers0