2

I have a scenario where I’m attempting to serve data in a non-blocking fashion which is sourced by a RxJava Observable (also non-blocking). I’m using the WriteListener callback provided by ServletOutputStream. I’m running into an issue where the write is throwing an IllegalStateException (java.lang.IllegalStateException: UT010035: Stream in async mode was not ready for IO operation) immediately after a successful isReady() check on the ServletOutputStream.

While looking deeper, I noticed this comment in the Undertow implementation of ServletOutputStream:

Once the write listener has been set operations must only be invoked on this stream from the write listener callback. Attempting to invoke from a different thread will result in an IllegalStateException.

Given that my data source is asynchronous there are scenarios where the onWritePossible() callback will reach a state where there no data immediately available and I would need to wait for more to be received from the source. In these cases, I would need to interact with the stream from the callback of my data source which is going to be a different thread. The only other option would be to suspend the thread used to call onWritePossible() and wait for more data to arrive, but this would be a blocking operation which defeats the whole purpose.

Is there another approach that I’m missing? The single thread requirement of Undertow doesn’t seem to be required by the Servlet 3.1 spec. From what I’ve read, other implementations appear to tolerate the multi-threaded approach given that the application coordinates the stream access synchronization.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Your question is about asynchronous I/O, not non-blocking. They aren't the same thing. – user207421 Dec 01 '17 at 23:11
  • I would ask on the undertow-dev list, it looks indeed like a non compliant Serviet implementation (if it is part of undertow and not provided by wildfly) – eckes Dec 01 '17 at 23:31
  • @EJP Yes and no. I'm trying to achieve a non-blocking operation via a pair of asynchronous APIs. I do agree that your edit makes the question more clear so thank you for that. – tcoates1362 Dec 04 '17 at 15:45
  • @eckes Thanks. I actually tried to post this question to the undertow-dev list before posting here, but it was bounced back with a "permission denied" error. I was hoping some of the undertow devs might see this. – tcoates1362 Dec 04 '17 at 15:49

0 Answers0