How to send an asynchronous response to the Undertow HttpHandler

I am looking for an example that shows how you write an answer asynchronously in an HttpHandler approach? The problem is that when HttpServerExchange.endExchange is called Response, it is cleared. My sample HttpHandler uses the rx-java library from Scala.

class MyHandler() extends HttpHandler {
  override def handleRequest(exchange: HttpServerExchange) = {
    val observable = Observable.items(List(1, 2, 3)) // simplistic not long running
    observable.map {
      // this is run async
      myList => exchange.getResponseSender.send(myList.toString)
    }
  }
}
+4
source share
1 answer

If you call the dispatch () method, the exchange will not end when the call stack returns, but even so, that would be great.

You probably want something like:

exchange.dispatch(SameThreadExecutor.INSTANCE, () -> {
observable.map {
  // this is run async
  myList => exchange.getResponseSender.send(myList.toString)
}}

, , async, , . , , .

+1

All Articles