Search in sources :

Example 56 with AsyncResponse

use of jakarta.ws.rs.container.AsyncResponse in project resteasy by resteasy.

the class JaxrsAsyncResource method negative.

@GET
@Path("negative")
@Produces("text/plain")
public void negative(@Suspended final AsyncResponse response) {
    response.setTimeout(-1, TimeUnit.MILLISECONDS);
    Thread t = new Thread() {

        private Logger logger = Logger.getLogger(JaxrsAsyncResource.class);

        @Override
        public void run() {
            try {
                logger.info("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 57 with AsyncResponse

use of jakarta.ws.rs.container.AsyncResponse in project resteasy by resteasy.

the class JaxrsAsyncServletJaxrsResource method get.

@GET
@Produces("text/plain")
public void get(@Suspended final AsyncResponse response) throws Exception {
    response.setTimeout(2000, TimeUnit.MILLISECONDS);
    Thread t = new Thread() {

        private Logger logger = Logger.getLogger(JaxrsAsyncServletJaxrsResource.class);

        @Override
        public void run() {
            try {
                logger.info("STARTED!!!!");
                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();
}
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) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 58 with AsyncResponse

use of jakarta.ws.rs.container.AsyncResponse in project resteasy by resteasy.

the class AsyncResource 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 log = Logger.getLogger(AsyncResource.class);

        @Override
        public void run() {
            try {
                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));
                log.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 59 with AsyncResponse

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");
}
Also used : HttpRequest(org.jboss.resteasy.spi.HttpRequest) CompletionCallback(jakarta.ws.rs.container.CompletionCallback) Logger(org.jboss.logging.Logger) AsyncResponse(jakarta.ws.rs.container.AsyncResponse) HttpRequest(org.jboss.resteasy.spi.HttpRequest) IOException(java.io.IOException) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) ResteasyContext(org.jboss.resteasy.core.ResteasyContext) Response(jakarta.ws.rs.core.Response) ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) ContainerRequestFilter(jakarta.ws.rs.container.ContainerRequestFilter) SuspendableContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.SuspendableContainerRequestContext) ExecutorService(java.util.concurrent.ExecutorService) ExecutorService(java.util.concurrent.ExecutorService) SuspendableContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.SuspendableContainerRequestContext) AsyncResponse(jakarta.ws.rs.container.AsyncResponse)

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