@GET @ManagedAsync public void getMessage(@Suspended final AsyncResponse ar) throws InterruptedException { suspended.put(ar); }
@POST @ManagedAsync public String postMessage(final Message message) throws InterruptedException { final AsyncResponse asyncResponse = suspended.take(); asyncResponse.resume(message); return "Sent!"; } }
@GET @ManagedAsync public void longGet(@Suspended final AsyncResponse ar, @QueryParam("id") int requestId) { try { Thread.sleep(SLEEP_TIME_IN_MILLIS); } catch (InterruptedException ex) { LOGGER.log(Level.SEVERE, "Response processing interrupted", ex); } ar.resume(requestId + " - " + NOTIFICATION_RESPONSE); } }
@POST @ManagedAsync @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.MULTIPART_FORM_DATA) @Path("/segments") @ApiOperation(value = "Upload a segment", notes = "Upload a segment as binary") // For the multipart endpoint, we will always move segment to final location regardless of the segment endpoint. public void uploadSegmentAsMultiPart(FormDataMultiPart multiPart, @ApiParam(value = "Whether to enable parallel push protection") @DefaultValue("false") @QueryParam(FileUploadDownloadClient.QueryParameters.ENABLE_PARALLEL_PUSH_PROTECTION) boolean enableParallelPushProtection, @Context HttpHeaders headers, @Context Request request, @Suspended final AsyncResponse asyncResponse) { try { asyncResponse.resume(uploadSegment(multiPart, enableParallelPushProtection, headers, request, true)); } catch (Throwable t) { asyncResponse.resume(t); } }
@POST @ManagedAsync @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.MULTIPART_FORM_DATA) @Path("/v2/segments") @ApiOperation(value = "Upload a segment", notes = "Upload a segment as binary") // This behavior does not differ from v1 of the same endpoint. public void uploadSegmentAsMultiPartV2(FormDataMultiPart multiPart, @ApiParam(value = "Whether to enable parallel push protection") @DefaultValue("false") @QueryParam(FileUploadDownloadClient.QueryParameters.ENABLE_PARALLEL_PUSH_PROTECTION) boolean enableParallelPushProtection, @Context HttpHeaders headers, @Context Request request, @Suspended final AsyncResponse asyncResponse) { try { asyncResponse.resume(uploadSegment(multiPart, enableParallelPushProtection, headers, request, true)); } catch (Throwable t) { asyncResponse.resume(t); } }
@POST @ManagedAsync @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @Path("/segments") @ApiOperation(value = "Upload a segment", notes = "Upload a segment as json") // We use this endpoint with URI upload because a request sent with the multipart content type will reject the POST // request if a multipart object is not sent. This endpoint does not move the segment to its final location; // it keeps it at the downloadURI header that is set. We will not support this endpoint going forward. public void uploadSegmentAsJson(String segmentJsonStr, @ApiParam(value = "Whether to enable parallel push protection") @DefaultValue("false") @QueryParam(FileUploadDownloadClient.QueryParameters.ENABLE_PARALLEL_PUSH_PROTECTION) boolean enableParallelPushProtection, @Context HttpHeaders headers, @Context Request request, @Suspended final AsyncResponse asyncResponse) { try { asyncResponse.resume(uploadSegment(null, enableParallelPushProtection, headers, request, false)); } catch (Throwable t) { asyncResponse.resume(t); } }
@POST @ManagedAsync @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @Path("/v2/segments") @ApiOperation(value = "Upload a segment", notes = "Upload a segment as json") // We use this endpoint with URI upload because a request sent with the multipart content type will reject the POST // request if a multipart object is not sent. This endpoint is recommended for use. It differs from the first // endpoint in how it moves the segment to a Pinot-determined final directory. public void uploadSegmentAsJsonV2(String segmentJsonStr, @ApiParam(value = "Whether to enable parallel push protection") @DefaultValue("false") @QueryParam(FileUploadDownloadClient.QueryParameters.ENABLE_PARALLEL_PUSH_PROTECTION) boolean enableParallelPushProtection, @Context HttpHeaders headers, @Context Request request, @Suspended final AsyncResponse asyncResponse) { try { asyncResponse.resume(uploadSegment(null, enableParallelPushProtection, headers, request, true)); } catch (Throwable t) { asyncResponse.resume(t); } }
@GET @Path("async") @ManagedAsync public void traceWithAsync(@Suspended final AsyncResponse asyncResponse, @Context ContainerRequestContext context) { final Span span = OpenTracingUtils.getRequestSpan(context).orElseThrow(() -> new RuntimeException("tracing failed")); span.log("In the resource method."); Executors.newSingleThreadExecutor().submit(() -> { try { Thread.sleep(200); } catch (InterruptedException e) { span.log("Interrupted"); e.printStackTrace(); } span.log("Resuming"); asyncResponse.resume("OK"); }); span.log("Before exiting the resource method"); }
@POST @Timed @ManagedAsync public void changeGraph(JsonFeatureCollection collection, @Suspended AsyncResponse response) { response.resume(graphHopper.changeGraph(collection.getFeatures())); }
/** * Persist an Order to Kafka. Returns once the order is successfully written to R nodes where * R is the replication factor configured in Kafka. * * @param order the order to add * @param timeout the max time to wait for the response from Kafka before timing out the POST */ @POST @ManagedAsync @Path("/orders") @Consumes(MediaType.APPLICATION_JSON) public void submitOrder(final OrderBean order, @QueryParam("timeout") @DefaultValue(CALL_TIMEOUT) final Long timeout, @Suspended final AsyncResponse response) { setTimeout(timeout, response); Order bean = fromBean(order); producer.send(new ProducerRecord<>(ORDERS.name(), bean.getId(), bean), callback(response, bean.getId())); }
@ManagedAsync @Path("/orders/{id}") @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
@GET @ManagedAsync @Path("orders/{id}/validated") public void getPostValidationWithTimeout(@PathParam("id") final String id, @QueryParam("timeout") @DefaultValue(CALL_TIMEOUT) Long timeout, @Suspended final AsyncResponse asyncResponse) { setTimeout(timeout, asyncResponse); HostStoreInfo hostForKey = getKeyLocationOrBlock(id, asyncResponse); if (hostForKey == null) { //request timed out so return return; } //Retrieve the order locally or reach out to a different instance if the required partition is hosted elsewhere. if (thisHost(hostForKey)) { fetchLocal(id, asyncResponse, (k, v) -> (v.getState() == OrderState.VALIDATED || v.getState() == OrderState.FAILED)); } else { fetchFromOtherHost(new Paths(hostForKey.getHost(), hostForKey.getPort()).urlGetValidated(id), asyncResponse, timeout); } }
@GET @ManagedAsync @Path("/{schema_name}/{index_name}/replication") @Produces({ServiceInterface.APPLICATION_JSON_UTF8, SmileMediaTypes.APPLICATION_JACKSON_SMILE}) ReplicationStatus replicationCheck(@PathParam("schema_name") String schema_name, @PathParam("index_name") String index_name);
@POST @ManagedAsync @Path("/{schema_name}/{index_name}/backup/{backup_name}") @Produces({ServiceInterface.APPLICATION_JSON_UTF8, SmileMediaTypes.APPLICATION_JACKSON_SMILE}) SortedMap<String, SortedMap<String, BackupStatus>> doBackup( @PathParam("schema_name") String schema_name, @PathParam("index_name") String index_name, @PathParam("backup_name") String backup_name);
@GET @ManagedAsync @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "Get a security domain") @ApiResponses({ @ApiResponse(code = 200, message = "Domain", response = Domain.class), @ApiResponse(code = 500, message = "Internal server error")}) public void get(@PathParam("domain") String domainId, @Suspended final AsyncResponse response) { domainService.findById(domainId) .switchIfEmpty(Maybe.error(new DomainNotFoundException(domainId))) .map(domain -> Response.ok(domain).build()) .subscribe( result -> response.resume(result), error -> response.resume(error)); }
/** * Persist an Order to Kafka. Returns once the order is successfully written to R nodes where * R is the replication factor configured in Kafka. * * @param order the order to add * @param timeout the max time to wait for the response from Kafka before timing out the POST */ @POST @ManagedAsync @Path("/orders") @Consumes(MediaType.APPLICATION_JSON) public void submitOrder(final Order order, @QueryParam("timeout") @DefaultValue(CALL_TIMEOUT) final Long timeout, @Suspended final AsyncResponse response) { setTimeout(timeout, response); Order bean = order; producer.send(new ProducerRecord<>(Schemas.Topics.ORDERS.name(), bean.getId(), bean), callback(response, bean.getId())); }
@GET @ManagedAsync @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "Get a security domain") @ApiResponses({ @ApiResponse(code = 200, message = "Domain", response = Domain.class), @ApiResponse(code = 500, message = "Internal server error")}) public void get(@PathParam("domain") String domainId, @Suspended final AsyncResponse response) { domainService.findById(domainId) .switchIfEmpty(Maybe.error(new DomainNotFoundException(domainId))) .map(domain -> Response.ok(domain).build()) .subscribe( result -> response.resume(result), error -> response.resume(error)); }
@ManagedAsync @Path("/orders/{id}") @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
@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()); } }); } }
@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(); } }