Created
April 2, 2015 22:09
-
-
Save jsanda/d1a0b642f4bb381369b0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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("/") | |
@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