这会阻塞io线程吗?我应该使用`exchange.isInThread()`逻辑

时间:2017-11-01 21:31:04

标签: undertow

所以我正在玩我自己的Undertow路径并发布课程。我写了一个实现HttpHandler接口的类。到目前为止非常直截了当。我的问题。我必须编写一个callback方法才能访问帖子的xml主体。再一次,没什么大不过的......但是......几个问题......

  1. 这只是在不使用测试的情况下工作,看看我们是否在io线程中。尽管如此,我{m} assuming这是一个阻塞操作,可能导致请求排队....是吗?
  2. 从文档和几个示例中我发现,您应该测试一下,看看您是否在io线程中,如果是,请调用exchange.startBlocking(),然后发送,然后退出。再次......是吗?
  3. 在我的示例代码中,我已将其注释掉,但它工作正常,但我认为这是不行的.....是吗?
  4. 显然,还有一个BlockingHandler可以实现,其中实现代码不必对io线程进行测试,然后调用exchange.startBlocking() ...是吗?
  5. 在下面的示例中,我实际上只是从回调中获取response,然后使用exchange进行进一步处理。如果我将代码全部放在callback处理程序中,会不会更好?
  6. 谢谢。

      public class ApiTaxwareHandler implements HttpHandler {
    
          private static final AtomicLong hitCount = new AtomicLong();
    
          @Override
          public void handleRequest(final HttpServerExchange exchange) throws Exception {
              //
              //  Reading the request body is a BLOCKing operation so
              //  we have to push this thread outside of the io thread
              //  and then we can block it.  the if condition below accomplishes
              // that.
      //        if (exchange.isInIoThread()) {
      //            exchange.startBlocking();
      //            exchange.dispatch(this);
      //            return;
      //        }
    
              Map<String, Deque<String>> queryParams = exchange.getQueryParameters();
              if (queryParams.isEmpty()) {
                  throw new Exception("Parameter 'key' is missing from the request");
              }
    
              long bytesInBody = exchange.getRequestContentLength();
    
              //int bytesInBody = exchange.getInputStream().available();
              StringBuilder requestBody = new StringBuilder((int) bytesInBody);
    
              // Low level gobble-d-gook to fully read the request header.
              exchange.getRequestReceiver().receiveFullString(new Receiver.FullStringCallback() {
                  @Override
                  public void handle(HttpServerExchange exchange, String message) {
                      //exchange.getResponseSender().send(message);
                      requestBody.append(message);
                      log.debug("Leaving FullStringCallback and setting request body StringBuilder");
                  }
              });
    
              String apiKey = queryParams.get("key").getFirst();
              String xmlIn = requestBody.toString(); //new Scanner(exchange.getInputStream(), "UTF-8").useDelimiter("\\Z").next();
    
              log.debug("ApiTaxwareHandler.taxLookupHandler.\nPath is: " + exchange.getRequestPath() + "\nQueryParams are: key=" + apiKey + "\nContent size is: " + bytesInBody + "\nRequest body is " + xmlIn);
    
              exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/xml");
              exchange.getResponseSender().send("Responding from " + exchange.getRequestPath() + "  Hit Count now: " + hitCount.incrementAndGet());
          }
    

0 个答案:

没有答案