如何在Dropwizard中使用Jetty Continuations?

时间:2014-07-09 16:35:12

标签: java asynchronous jetty continuations dropwizard

我有一个如下所示的资源方法:

@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方法。我这样做了吗?

1 个答案:

答案 0 :(得分:2)

您可以像使用任何Jetty服务器一样使用延续。像这样的东西是一个非常人为的例子:

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();
}

你进入一个无限循环,因为你永远不会在块结束时返回响应,并且继续不断地暂停/恢复。