use of javax.ws.rs.container.AsyncResponse in project component-runtime by Talend.
the class ExecutionResource method read.
/**
* Read inputs from an instance of mapper. The number of returned records if enforced to be limited to 1000.
* The format is a JSON based format where each like is a json record.
*
* @param family the component family.
* @param component the component name.
* @param size the maximum number of records to read.
* @param configuration the component configuration as key/values.
*/
@POST
@Deprecated
@Produces("talend/stream")
@Path("read/{family}/{component}")
public void read(@Suspended final AsyncResponse response, @Context final Providers providers, @PathParam("family") final String family, @PathParam("component") final String component, @QueryParam("size") @DefaultValue("50") final long size, final Map<String, String> configuration) {
final long maxSize = Math.min(size, MAX_RECORDS);
response.setTimeoutHandler(asyncResponse -> log.warn("Timeout on dataset retrieval"));
response.setTimeout(appConfiguration.datasetRetrieverTimeout(), SECONDS);
executorService.submit(() -> {
final Optional<Mapper> mapperOptional = manager.findMapper(family, component, getConfigComponentVersion(configuration), configuration);
if (!mapperOptional.isPresent()) {
response.resume(new WebApplicationException(Response.status(BAD_REQUEST).entity(new ErrorPayload(COMPONENT_MISSING, "Didn't find the input component")).build()));
return;
}
final Mapper mapper = mapperOptional.get();
mapper.start();
try {
final Input input = mapper.create();
try {
input.start();
response.resume((StreamingOutput) output -> {
Object data;
int current = 0;
while (current++ < maxSize && (data = input.next()) != null) {
if (CharSequence.class.isInstance(data) || Number.class.isInstance(data) || Boolean.class.isInstance(data)) {
final PrimitiveWrapper wrapper = new PrimitiveWrapper();
wrapper.setValue(data);
data = wrapper;
}
inlineStreamingMapper.toJson(data, output);
output.write(EOL);
}
});
} finally {
input.stop();
}
} finally {
mapper.stop();
}
});
}
use of javax.ws.rs.container.AsyncResponse in project wildfly by wildfly.
the class AsyncResource method getBasic.
@GET
@Path("basic")
@Produces("text/plain")
public void getBasic(@Suspended final AsyncResponse response) throws Exception {
response.setTimeout(1, TimeUnit.SECONDS);
Thread t = new Thread() {
@Override
public void run() {
try {
Response jaxrs = Response.ok("basic").type(MediaType.TEXT_PLAIN).build();
response.resume(jaxrs);
} catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();
}
use of javax.ws.rs.container.AsyncResponse in project tomee by apache.
the class JAXRSInvoker method invoke.
public Object invoke(Exchange exchange, Object request) {
MessageContentsList responseList = checkExchangeForResponse(exchange);
if (responseList != null) {
return responseList;
}
AsyncResponse asyncResp = exchange.get(AsyncResponse.class);
if (asyncResp != null) {
AsyncResponseImpl asyncImpl = (AsyncResponseImpl) asyncResp;
asyncImpl.prepareContinuation();
try {
asyncImpl.handleTimeout();
return handleAsyncResponse(exchange, asyncImpl);
} catch (Throwable t) {
return handleAsyncFault(exchange, asyncImpl, t);
}
}
ResourceProvider provider = getResourceProvider(exchange);
Object rootInstance = null;
Message inMessage = exchange.getInMessage();
try {
rootInstance = getServiceObject(exchange);
Object serviceObject = getActualServiceObject(exchange, rootInstance);
return invoke(exchange, request, serviceObject);
} catch (WebApplicationException ex) {
responseList = checkExchangeForResponse(exchange);
if (responseList != null) {
return responseList;
}
return handleFault(ex, inMessage);
} finally {
boolean suspended = isSuspended(exchange);
if (suspended || exchange.isOneWay() || inMessage.get(Message.THREAD_CONTEXT_SWITCHED) != null) {
ServerProviderFactory.clearThreadLocalProxies(inMessage);
}
if (suspended || isServiceObjectRequestScope(inMessage)) {
persistRoots(exchange, rootInstance, provider);
} else {
provider.releaseInstance(inMessage, rootInstance);
}
}
}
use of javax.ws.rs.container.AsyncResponse in project bisq-api by mrosseel.
the class TradeResource method handlePaymentStatusChange.
private void handlePaymentStatusChange(String tradeId, AsyncResponse asyncResponse, CompletableFuture<Void> completableFuture) {
completableFuture.thenApply(response -> asyncResponse.resume(Response.status(Response.Status.OK).build())).exceptionally(e -> {
final Throwable cause = e.getCause();
final Response.ResponseBuilder responseBuilder;
if (cause instanceof ValidationException) {
responseBuilder = toValidationErrorResponse(cause, 422);
} else if (cause instanceof NotFoundException) {
responseBuilder = toValidationErrorResponse(cause, 404);
} else {
final String message = cause.getMessage();
responseBuilder = Response.status(500);
if (null != message)
responseBuilder.entity(new ValidationErrorMessage(ImmutableList.of(message)));
log.error("Unable to confirm payment started for trade: " + tradeId, cause);
}
return asyncResponse.resume(responseBuilder.build());
});
}
Aggregations