Search in sources :

Example 6 with AsyncResponse

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();
}
Also used : Response(jakarta.ws.rs.core.Response) AsyncResponse(jakarta.ws.rs.container.AsyncResponse) StringWriter(java.io.StringWriter) Logger(org.jboss.logging.Logger) PrintWriter(java.io.PrintWriter) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 7 with AsyncResponse

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();
}
Also used : AsyncResponse(jakarta.ws.rs.container.AsyncResponse) Response(jakarta.ws.rs.core.Response) StringWriter(java.io.StringWriter) Logger(org.jboss.logging.Logger) ForbiddenException(jakarta.ws.rs.ForbiddenException) PrintWriter(java.io.PrintWriter) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 8 with AsyncResponse

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();
}
Also used : AsyncResponse(jakarta.ws.rs.container.AsyncResponse) Response(jakarta.ws.rs.core.Response) StringWriter(java.io.StringWriter) CountDownLatch(java.util.concurrent.CountDownLatch) Logger(org.jboss.logging.Logger) ForbiddenException(jakarta.ws.rs.ForbiddenException) PrintWriter(java.io.PrintWriter) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 9 with AsyncResponse

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;
}
Also used : WebApplicationException(jakarta.ws.rs.WebApplicationException) ResponseBuilder(jakarta.ws.rs.core.Response.ResponseBuilder) AsyncResponse(jakarta.ws.rs.container.AsyncResponse)

Example 10 with 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;
}
Also used : AsyncResponse(jakarta.ws.rs.container.AsyncResponse) Date(java.util.Date) Path(jakarta.ws.rs.Path) POST(jakarta.ws.rs.POST)

Aggregations

AsyncResponse (jakarta.ws.rs.container.AsyncResponse)59 Path (jakarta.ws.rs.Path)47 GET (jakarta.ws.rs.GET)43 Response (jakarta.ws.rs.core.Response)23 Produces (jakarta.ws.rs.Produces)16 PrintWriter (java.io.PrintWriter)13 StringWriter (java.io.StringWriter)13 Logger (org.jboss.logging.Logger)13 POST (jakarta.ws.rs.POST)10 ForbiddenException (jakarta.ws.rs.ForbiddenException)6 IOException (java.io.IOException)4 WebApplicationException (jakarta.ws.rs.WebApplicationException)3 CompletionCallback (jakarta.ws.rs.container.CompletionCallback)2 ContainerRequestContext (jakarta.ws.rs.container.ContainerRequestContext)2 ContainerRequestFilter (jakarta.ws.rs.container.ContainerRequestFilter)2 ResponseBuilder (jakarta.ws.rs.core.Response.ResponseBuilder)2 Date (java.util.Date)2 Objects (java.util.Objects)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2