Search in sources :

Example 1 with AsyncResponse

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

the class TestResource method defaultTest.

@GET
@Path("default")
public void defaultTest(@Suspended final AsyncResponse response) {
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                LOG.info("TestResource: async thread started");
                // Jetty async timeout defaults to 30000.
                Thread.sleep(35000);
                Response jaxrs = Response.ok("test").type(MediaType.TEXT_PLAIN).build();
                response.resume(jaxrs);
                LOG.info("TestResource: async thread finished");
            } catch (Exception e) {
                LOG.error(e.getMessage(), e);
            }
        }
    };
    t.start();
}
Also used : Response(jakarta.ws.rs.core.Response) AsyncResponse(jakarta.ws.rs.container.AsyncResponse) Path(jakarta.ws.rs.Path) GET(jakarta.ws.rs.GET)

Example 2 with AsyncResponse

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

the class TestResource method test.

@GET
@Path("test")
public void test(@Suspended final AsyncResponse response) {
    response.setTimeout(5000, TimeUnit.MILLISECONDS);
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                LOG.info("TestResource: async thread started");
                Thread.sleep(10000);
                Response jaxrs = Response.ok("test").type(MediaType.TEXT_PLAIN).build();
                response.resume(jaxrs);
                LOG.info("TestResource: async thread finished");
            } catch (Exception e) {
                LOG.error(e.getMessage(), e);
            }
        }
    };
    t.start();
}
Also used : Response(jakarta.ws.rs.core.Response) AsyncResponse(jakarta.ws.rs.container.AsyncResponse) Path(jakarta.ws.rs.Path) GET(jakarta.ws.rs.GET)

Example 3 with AsyncResponse

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

the class AsyncResource method get.

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

        private Logger log = Logger.getLogger(AsyncResource.class);

        @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));
                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) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 4 with AsyncResponse

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

the class JaxrsAsyncResource method zero.

@GET
@Path("zero")
@Produces("text/plain")
public void zero(@Suspended final AsyncResponse response) {
    response.setTimeout(0, 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 5 with AsyncResponse

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

the class JaxrsAsyncResource method get.

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

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

        @Override
        public void run() {
            try {
                logger.info("Get thread 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 : 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) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

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