2014-07-09 25 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(); 
} 

你進入無限循環,因爲你永遠不會獲得到結束塊返回響應和持續不斷的暫停/恢復。

相關問題