【问题标题】:How do I use Jetty Continuations in Dropwizard?如何在 Dropwizard 中使用 Jetty Continuations?
【发布时间】:2014-08-30 19:20:50
【问题描述】:
我有一个如下所示的资源方法:
@Path("/helloworld")
@GET
public Response sayHello(@Context HttpServletRequest request)
throws InterruptedException {
Continuation c = ContinuationSupport.getContinuation(request);
c.suspend();
Thread.sleep(1000);
c.resume();
return Response.ok("hello world hard").build();
}
似乎当我调用这个端点时,dropwizard 最终会在无限循环中调用 sayHello 方法。我这样做对吗?
【问题讨论】:
标签:
java
asynchronous
jetty
continuations
dropwizard
【解决方案1】:
您可以像使用任何 Jetty 服务器一样使用 continuation。像这样真正人为的例子:
public Response sayHello(@Context HttpServletRequest request)
throws InterruptedException {
Continuation c = ContinuationSupport.getContinuation(request);
c.setTimeout(2000);
c.suspend();
// Do work
System.out.println("halp");
// End condition
if (c.isInitial() != true) {
c.complete();
return Response.ok().build();
}
return Response.serverError().build();
}
您进入了一个无限循环,因为您永远无法到达块的末尾以返回响应,并且继续不断地暂停/恢复。