This blog post covers
- asynchronous programming support in JAX-RS
- and some of its potential gotchas
- along with a sample (maven based) Java EE project on Github
Man, I tend to blog a lot about JAX-RS and REST !
Basics of server side async JAX-RS
On the server side, asynchronous behaviour is driven by
- @Suspended: annotation which instructs the container to inject an instance of AsyncResponse and invoke the method asynchronously
- AsyncResponse: bridge between the application logic and the client request
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 …"); | |
} | |
} |
Since we have clearly expressed our asynchronous requirements, the container will ensure that
- the calling thread is released
- the actual business logic is executed in a different thread (in this case its the thread pool taken care of the by the Managed Executor Service in the Java EE container – thanks to Concurrency Utilties in Java EE 7)
Things to watch out for .. a.k.a gotchas
Although the calling (request) thread is released, the underlying I/O thread still blocks until the processing in background thread continues.In the above example, the caller would have to wait 5 seconds since that’s the delay we have purposefully introduced within the code. In simple words, the client (e.g. browser executing a HTTP GET) keeps waiting until the business logic execution is finished by calling the resume method of the injected AsynResponse object (remember, it’s the bridge b/w the application & the client).
Timeouts to the rescue
One can specify a time out period after which the client gets back a HTTP 503 Service Unavailable response (default convention)
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) { | |
ar.setTimeout(3, TimeUnit.SECONDS); //setting the time out to 3 seconds | |
String initialThread = Thread.currentThread().getName(); | |
…… | |
} | |
} |
Fine grained time outs
The default behaviour (HTTP 503 on time out) might not be suitable for all use cases. For example, you might want to implement a solution where a tracking identifier needs to be sent to the client (for future) if the actual processing does not finish in due time (before timeout triggers). Having the ability to send a custom HTTP response on time out can prove useful. The AsyncResponse API makes this possible via the notion of a time out handler. You can do this by
- a direct HTTP response (see below example)
- via an exception (by passing an instance of Throwable to AsyncResponse#resume)
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
@GET | |
public void async(@Suspended AsyncResponse ar) { | |
ar.setTimeout(3, TimeUnit.SECONDS); | |
ar.setTimeoutHandler(new TimeoutHandler() { | |
@Override | |
public void handleTimeout(AsyncResponse asyncResponse) { | |
asyncResponse.resume(Response.accepted(UUID.randomUUID().toString()).build()); //sending HTTP 202 (Accepted) | |
} | |
}); | |
…… | |
Client side async using the JAX-RS Client API
Using asynchronous behaviour on the client side is pretty easy. All you need to do is obtain an instance of AsyncInvoker by calling async on the Invocation.Builder
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
public Response test() throws Exception{ | |
Client client = ClientBuilder.newBuilder().build(); | |
WebTarget target = client.target("https://api.github.com/search/users?q=abhirockzz"); | |
Invocation.Builder reqBuilder = target.request(); | |
AsyncInvoker asyncInvoker = reqBuilder.async(); | |
Future<Response> futureResp = asyncInvoker.get(); | |
Response response = futureResp.get(); //blocks until client responds or times out | |
String responseBody = response.readEntity(String.class); | |
return Response.status(response.getStatus()).entity(responseBody).build(); | |
} |
Above gotchas applicable here
As you might have already observed, async behaviour in (server side) JAX-RS is not the same as in other typical async APIs i.e. the client is still blocked.
- You should use the async method (as demonstrated above) to call a server side REST API in an async manner even if the server REST API itself is asynchronous in nature (implemented using @Suspended AsyncResponse) .
I repeat: do not be under the impression that just because the server API is async, your client thread will return immediately
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
public Response test() throws Exception{ | |
Client client = ClientBuilder.newBuilder().build(); | |
WebTarget target = client.target("http://localhost:8080/jaxrs-async-service/async"); | |
Invocation.Builder reqBuilder = target.request(); | |
Response response = reqBuilder.get(); //this will block until server responds or triggers out (even if its aysnc) | |
String responseBody = response.readEntity(String.class); | |
return Response.status(response.getStatus()).entity(responseBody).build(); | |
} |
- The call to async returns an instance of Future object – and you might already know, the get method (of Future) blocks. So use it with care and at the correct point in your application logic
Further reading
- I’ll continue to pitch my latest JAX-RS 2.0 article in the Java Magazine edition 🙂 Hope that’s ok !
- Check out the JAX-RS 2.0 specification doc
That’s all. Be async. Be safe
Cheers!
Pingback: Nuances of JAX-RS client side async callback | Thinking in Java EE (at least trying to!)