use of jakarta.ws.rs.container.AsyncResponse in project resteasy by resteasy.
the class LegacySuspendResource method get.
@GET
@Produces("text/plain")
public void get(@Suspended final AsyncResponse response) {
response.setTimeout(8000, TimeUnit.MILLISECONDS);
Thread t = new Thread() {
@Override
public void run() {
try {
Thread.sleep(100);
Response jaxrs = Response.ok("hello").type(MediaType.TEXT_PLAIN).build();
response.resume(jaxrs);
} catch (Exception e) {
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
logger.error(errors.toString());
}
}
};
t.start();
}
use of jakarta.ws.rs.container.AsyncResponse in project resteasy by resteasy.
the class AsyncRequestFilter method filter.
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
requestContext.getHeaders().add("RequestFilterCallback" + name, String.valueOf(callbackException));
callbackException = null;
SuspendableContainerRequestContext ctx = (SuspendableContainerRequestContext) requestContext;
String action = ctx.getHeaderString(name);
LOG.error("Filter request for " + name + " with action: " + action);
if ("sync-pass".equals(action)) {
// do nothing
} else if ("sync-fail".equals(action)) {
ctx.abortWith(Response.ok(name).build());
} else if ("async-pass".equals(action)) {
ctx.suspend();
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> ctx.resume());
} else if ("async-pass-instant".equals(action)) {
ctx.suspend();
ctx.resume();
} else if ("async-fail".equals(action)) {
ctx.suspend();
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> ctx.abortWith(Response.ok(name).build()));
} else if ("async-fail-instant".equals(action)) {
ctx.suspend();
ctx.abortWith(Response.ok(name).build());
} else if ("async-throw-late".equals(action)) {
ctx.suspend();
HttpRequest req = ResteasyContext.getContextData(HttpRequest.class);
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
LOG.error("Error:", e);
}
AsyncResponse resp = req.getAsyncContext().getAsyncResponse();
resp.register((CompletionCallback) (t) -> {
if (callbackException != null)
throw new RuntimeException("Callback called twice");
callbackException = Objects.toString(t);
});
if ("true".equals(req.getHttpHeaders().getHeaderString("UseExceptionMapper")))
ctx.resume(new AsyncFilterException("ouch"));
else
ctx.resume(new Throwable("ouch"));
});
}
LOG.error("Filter request for " + name + " with action: " + action + " done");
}
use of jakarta.ws.rs.container.AsyncResponse in project resteasy by resteasy.
the class CallbackResourceBase method cancel.
@GET
@Path("cancelvoid")
public String cancel(@QueryParam("stage") String stage) {
AsyncResponse response = takeAsyncResponse(stage);
boolean ret = response.cancel();
// Invoking a cancel(...) method multiple times to cancel request
// processing has the same effect as canceling the request processing
// only once.
ret &= response.cancel();
addResponse(response, stage);
return ret ? TRUE : FALSE;
}
use of jakarta.ws.rs.container.AsyncResponse in project resteasy by resteasy.
the class CallbackResourceBase method resumeWithRuntimeException.
@GET
@Path("resumeruntime")
public String resumeWithRuntimeException(@QueryParam("stage") String stage) {
AsyncResponse async = takeAsyncResponse(stage);
boolean b = async.resume(new RuntimeException(RESUMED));
addResponse(async, stage);
return b ? TRUE : FALSE;
}
use of jakarta.ws.rs.container.AsyncResponse in project resteasy by resteasy.
the class CallbackResourceBase method cancelretry.
@POST
@Path("cancelretry")
public String cancelretry(@QueryParam("stage") String stage, String sRetryAfter) {
AsyncResponse response = takeAsyncResponse(stage);
int retryAfter = Integer.parseInt(sRetryAfter);
boolean b = response.cancel(retryAfter);
// Invoking a cancel(...) method multiple times to cancel request
// processing has the same effect as canceling the request processing
// only once.
b &= response.cancel(retryAfter * 2);
addResponse(response, stage);
return b ? TRUE : FALSE;
}
Aggregations