Last active
April 3, 2016 06:21
-
-
Save abhirockzz/9cd6b7bd73b3486722636d644bbd559d to your computer and use it in GitHub Desktop.
Asynchronous method in JAX-RS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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