Skip to content

Instantly share code, notes, and snippets.

@abhirockzz
Last active April 3, 2016 06:21
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/9cd6b7bd73b3486722636d644bbd559d to your computer and use it in GitHub Desktop.
Save abhirockzz/9cd6b7bd73b3486722636d644bbd559d to your computer and use it in GitHub Desktop.
Asynchronous method in JAX-RS
@Path("async")
@Stateless
public class AsyncResource {
@Resource
ManagedExecutorService mes;
@GET
public void async(@Suspended AsyncResponse ar) {
String initialThread = Thread.currentThread().getName();
System.out.println("Thread: "+ initialThread + " in action...");
mes.execute(new Runnable() {
@Override
public void run() {
try {
String processingThread = Thread.currentThread().getName();
System.out.println("Processing thread: " + processingThread);
Thread.sleep(5000);
String respBody = "Process initated in " + initialThread + " and finished in " + processingThread;
ar.resume(Response.ok(respBody).build());
} catch (InterruptedException ex) {
Logger.getLogger(AsyncResource.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
System.out.println(initialThread + " freed ...");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment