2014-07-09 45 views
0

我有一个看起来像这样的资源的方法:如何在Dropwizard中使用Servlet 3.0异步方法?

@Path("hard") 
@GET 
public Response sayHello2(@Context HttpServletRequest request) 
     throws InterruptedException { 
    AsyncContext ac = request.startAsync(); 
    Thread.sleep(1000); 
    ac.complete(); 

    return Response.ok("hello world hard").build(); 
} 

它看起来像代码贯穿而过,但我似乎无法验证异步工作在这种情况下?我正确使用这个吗?

回答

0

我没有dropwizard的经验,但在异步servlet中,您不应该用线程休眠来阻塞servlet线程。在调用request.startAsync()之后,可以在servlet中使用ExecutorService将工作委托给另一个线程。您应该将异步上下文传递给工作线程,并在完成时调用ac.complete()。