2014-07-09 21 views
2

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

@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方法。我是否正确地做这件事?

回答

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

你进入无限循环,因为你永远不会获得到结束块返回响应和持续不断的暂停/恢复。

相关问题