@GET @Path("/{name}/status") @ManagedAsync public void getStatus( @PathParam("name") final String name, @Suspended final AsyncResponse response) { Optional<CassandraDaemonTask> taskOption = Optional.ofNullable(state.getDaemons().get(name)); if (!taskOption.isPresent()) { response.resume( Response.status(Response.Status.NOT_FOUND).build()); } else { CassandraDaemonTask task = taskOption.get(); client.status(task.getHostname(), task.getExecutor().getApiPort() ).whenCompleteAsync((status, error) -> { if (status != null) { response.resume(status); } else { response.resume(Response.serverError()); } }); } }
@RolesAllowed({"user", "admin"}) @Path("/{id}") @GET @Produces(MediaType.APPLICATION_JSON) @ManagedAsync public void getBook(@PathParam("id") String id, @Suspended final AsyncResponse asyncResponse) { // asyncResponse.resume( dao.getBookAsync(id) ); ListenableFuture<Book> bookFutures = dao.getBookAsync(id); Futures.addCallback(bookFutures, new FutureCallback<Book>() { @Override public void onSuccess(Book result) { EntityTag entityTag = generateEntityTag(result); Response.ResponseBuilder rb = request.evaluatePreconditions(entityTag); if (rb != null) { asyncResponse.resume(rb.build()); } else { asyncResponse.resume(Response.ok().tag(entityTag).entity(result).build()); } } @Override public void onFailure(Throwable t) { // we cound throw Jersey's WebApplicationException. asyncResponse.resume(t); } }); }
@GET @ManagedAsync @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.TEXT_PLAIN) public void sayAsyncHello(@Suspended final AsyncResponse response) { (new Thread() { @Override public void run() { response.resume(String.format(RESPONSE, value)); } }).start(); }
@GET @ManagedAsync @Produces(MediaType.APPLICATION_JSON) public void getLives(@Suspended final AsyncResponse asyncResponse, @DefaultValue("0") @QueryParam("newestid") final int newestId, @QueryParam("oldestid") final Integer oldestId) { asyncResponse.setTimeoutHandler(asyncResponse1 -> { logger.info("reached timeout"); asyncResponse1.resume(Response.ok().build()); }); asyncResponse.setTimeout(5, TimeUnit.MINUTES); try { List<Life> lives = oldestId == null ? Lifes.getLastLives(newestId) : Lifes.getOlderLives(oldestId); if (lives.size() > 0) { final GenericEntity<List<Life>> entity = new GenericEntity<List<Life>>(lives) { }; asyncResponse.resume(entity); } else { LifeProvider.suspend(asyncResponse); } } catch (SQLException e) { logger.error(e, e); asyncResponse .resume(new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR)); } }
@GET @ManagedAsync public void ping(@Suspended final AsyncResponse response) { response.resume(Response.status(Response.Status.OK).entity("pong").build()); }