use of jakarta.ws.rs.container.AsyncResponse in project resteasy by resteasy.
the class JaxrsAsyncResource method timeout.
@GET
@Path("timeout")
@Produces("text/plain")
public void timeout(@Suspended final AsyncResponse response) {
response.setTimeout(100, TimeUnit.MILLISECONDS);
Thread t = new Thread() {
private Logger logger = Logger.getLogger(JaxrsAsyncResource.class);
@Override
public void run() {
try {
logger.info("Timeout thread started.");
Thread.sleep(1000);
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 JaxrsAsyncServletJaxrsResource method timeout.
@GET
@Path("timeout")
@Produces("text/plain")
public void timeout(@Suspended final AsyncResponse response) {
response.setTimeout(10, TimeUnit.MILLISECONDS);
Thread t = new Thread() {
private Logger logger = Logger.getLogger(JaxrsAsyncServletJaxrsResource.class);
@Override
public void run() {
try {
logger.info("STARTED!!!!");
Thread.sleep(100000);
Response jaxrs = Response.ok("goodbye").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 JaxrsAsyncServletJaxrsResource method cancel.
@GET
@Path("cancel")
@Produces("text/plain")
public void cancel(@Suspended final AsyncResponse response) throws Exception {
response.setTimeout(10000, TimeUnit.MILLISECONDS);
final CountDownLatch sync = new CountDownLatch(1);
final CountDownLatch ready = new CountDownLatch(1);
Thread t = new Thread() {
private Logger logger = Logger.getLogger(JaxrsAsyncServletJaxrsResource.class);
@Override
public void run() {
try {
sync.countDown();
logger.info("cancel awaiting thread");
ready.await();
logger.info("cancel resuming");
Response jaxrs = Response.ok("hello").type(MediaType.TEXT_PLAIN).build();
cancelled = !response.resume(jaxrs);
} catch (Exception e) {
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
logger.error(errors.toString());
}
}
};
t.start();
sync.await();
logger.info("Cancelling...");
response.cancel();
ready.countDown();
}
use of jakarta.ws.rs.container.AsyncResponse in project resteasy by resteasy.
the class JaxrsAsyncServletResource method takeAsyncResponse.
protected static AsyncResponse takeAsyncResponse(int stageId) {
final ResponseBuilder error = createErrorResponseBuilder();
AsyncResponse asyncResponse = null;
try {
asyncResponse = stage[stageId].take();
} catch (InterruptedException e) {
throw new WebApplicationException(error.entity("ArrayBlockingQueue#take").build());
}
return asyncResponse;
}
use of jakarta.ws.rs.container.AsyncResponse in project resteasy by resteasy.
the class JaxrsAsyncServletResource method cancelDate.
@POST
@Path("canceldate")
public String cancelDate(@QueryParam("stage") String stage, String sRetryAfter) {
AsyncResponse response = takeAsyncResponse(stage);
long retryAfter = Long.parseLong(sRetryAfter);
boolean b = response.cancel(new Date(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(new Date(retryAfter + 20000));
addResponse(response, stage);
return b ? TRUE : FALSE;
}
Aggregations