Skip to content

Instantly share code, notes, and snippets.

@jsanda
Created April 2, 2015 22:09
Show Gist options
  • Save jsanda/d1a0b642f4bb381369b0 to your computer and use it in GitHub Desktop.
Save jsanda/d1a0b642f4bb381369b0 to your computer and use it in GitHub Desktop.
@Path("/")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public class AsyncHandler {
private static final ListeningExecutorService threadPool = MoreExecutors.listeningDecorator(
Executors.newFixedThreadPool(4));
@POST
@Path("/{tenantId}/metrics/numeric/{id}/data")
public void addData(
@Suspended final AsyncResponse asyncResponse,
@PathParam("tenantId") final String tenantId,
@PathParam("id") String id,
List<Integer> values) {
Callable<Response> callable = () -> Response.status(Response.Status.OK).build();
ListenableFuture<Response> future = threadPool.submit(callable);
Futures.addCallback(future, new FutureCallback<Response>() {
@Override
public void onSuccess(Response response) {
asyncResponse.resume(response);
}
@Override
public void onFailure(Throwable t) {
String msg = "Failed to perform operation due to an error: " + t.getMessage();
asyncResponse.resume(Response.serverError().entity(msg).build());
}
}, threadPool);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment