Skip to content

Instantly share code, notes, and snippets.

@abhirockzz
Created March 13, 2015 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhirockzz/e892ac81c6c30396f80f to your computer and use it in GitHub Desktop.
Save abhirockzz/e892ac81c6c30396f80f to your computer and use it in GitHub Desktop.
JAX-RS async time out
@GET
@Produces("text/plain")
public void execute(@Suspended AsyncResponse response){
System.out.println("Initially invoked on thread - "+ Thread.currentThread.getName() + ". This will free up soon !");
//just having this would result in HTTP 503 after 10 seconds
response.setTimeout(10, TimeUnit.SECONDS);
//client will recieve a HTTP 408 (timeout error) after 10 seconds
response.setTimeoutHandler((asyncResp) -> asyncResp.resume(Response.status(Response.Status.REQUEST_TIMEOUT)).build());
new Thread(() -> {
try {
Thread.sleep(11000);
} catch (InterruptedException ex) {
//ignoring
}
}).start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment