@GET public void getList(@Suspended final AsyncResponse ar) throws NamingException { ar.setTimeoutHandler(new TimeoutHandler() { @Override public void handleTimeout(AsyncResponse ar) { ar.resume("Operation timed out"); } }); ar.setTimeout(4000, TimeUnit.MILLISECONDS); ar.register(new MyCompletionCallback()); ar.register(new MyConnectionCallback()); ManagedThreadFactory threadFactory = (ManagedThreadFactory) new InitialContext() .lookup("java:comp/DefaultManagedThreadFactory"); Executors.newSingleThreadExecutor(threadFactory).submit(new Runnable() { @Override public void run() { try { Thread.sleep(3000); ar.resume(response[0]); } catch (InterruptedException ex) { } } }); }
@GET @Path("test") public void test(final @Suspended 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(); }
public static void setTimeout(long timeout, AsyncResponse asyncResponse) { asyncResponse.setTimeout(timeout, TimeUnit.MILLISECONDS); asyncResponse.setTimeoutHandler(resp -> resp.resume( Response.status(Response.Status.GATEWAY_TIMEOUT) .entity("HTTP GET timed out after " + timeout + " ms\n") .build())); }
@Path("/poll") @GET public void poll(@Suspended final AsyncResponse asyncResponse) throws InterruptedException { asyncResponse.setTimeout(30, TimeUnit.SECONDS); this.asyncResponse = asyncResponse; } @POST @Path("/printed") public Response printCallback(String barcode) throws IOException { // ... this.asyncResponse.resume("MESSAGE"); return Response.ok().build(); }
public static IInputStreamConsumer to(AsyncResponse asyncResponse, DocumentType targetType, long requestTimeout) { AsynchronousConversionResponse response = new AsynchronousConversionResponse(asyncResponse, targetType); asyncResponse.setTimeoutHandler(response); asyncResponse.setTimeout(requestTimeout, TimeUnit.MILLISECONDS); return response; }
@GET @Path(ASYNC_TIMEOUT_PATH) public void getAsyncTimeout(@Suspended AsyncResponse asyncResponse) { logger.info("Async timeout endpoint hit"); asyncResponse.setTimeout(SLEEP_TIME_MILLIS, TimeUnit.MILLISECONDS); }
@POST @Path("bid/{exchangeId}/{key}") public void handleBid(@PathParam("exchangeId") final String exchangeId, @PathParam("key") final String key, final OrtbBidRequest request, @Suspended final AsyncResponse asyncResponse) throws FilterException, IOException { // Timeout for clarity. asyncResponse.setTimeout(timeoutMs, TimeUnit.MILLISECONDS); asyncResponse.setTimeoutHandler(timeoutHandler); // Sending no data as an example asyncResponse.resume(Response.noContent().build()); }
@GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public void list(@Suspended final AsyncResponse asyncResponse) { asyncResponse.setTimeout(10, TimeUnit.SECONDS); executorService.submit(() -> { List<Product> res = super.listProducts(); Product[] arr = res.toArray(new Product[res.size()]); asyncResponse.resume(arr); }); }
public StreamingAsyncSubscriber(AsyncResponse ar, String openTag, String closeTag, String sep, long pollTimeout, long asyncTimeout) { super(ar); this.openTag = openTag; this.closeTag = closeTag; this.separator = sep; this.pollTimeout = pollTimeout; this.asyncTimeout = 0; if (asyncTimeout > 0) { ar.setTimeout(asyncTimeout, TimeUnit.MILLISECONDS); ar.setTimeoutHandler(new TimeoutHandlerImpl()); } } @Override
@GET @Path("/setTimeOut") public String setTimeOut() { boolean setTimeout = asyncResponse.setTimeout(2, TimeUnit.SECONDS); return String.valueOf(setTimeout); }
public static void setTimeout(long timeout, AsyncResponse asyncResponse) { asyncResponse.setTimeout(timeout, TimeUnit.MILLISECONDS); asyncResponse.setTimeoutHandler(resp -> resp.resume( Response.status(Response.Status.GATEWAY_TIMEOUT) .entity("HTTP GET timed out after " + timeout + " ms\n") .build())); }
@Override public void handleTimeout(AsyncResponse asyncResponse) { if (!resumeOnly && timeoutExtendedCounter.addAndGet(1) <= 2) { asyncResponse.setTimeout(1, TimeUnit.SECONDS); } else { asyncResponse.resume(books.get(id)); } }
public AsyncResponseHandler withTimeout(Duration timeout, Supplier<Response> timeoutResponse) { asyncResponse.setTimeoutHandler(asyncResponse -> { asyncResponse.resume(timeoutResponse.get()); cancelFuture(); }); asyncResponse.setTimeout(timeout.toMillis(), MILLISECONDS); return this; }
@GET @Path("/books/defaulttimeout") public void getBookDescriptionWithTimeout(@Suspended AsyncResponse async) { async.register(new CallbackImpl()); async.setTimeout(2000, TimeUnit.MILLISECONDS); }
public AsyncResponseHandler withTimeout(Duration timeout, Supplier<Response> timeoutResponse) { asyncResponse.setTimeoutHandler(asyncResponse -> { asyncResponse.resume(timeoutResponse.get()); cancelFuture(); }); asyncResponse.setTimeout(timeout.toMillis(), MILLISECONDS); return this; }
public AsyncResponseHandler withTimeout(Duration timeout, Supplier<Response> timeoutResponse) { asyncResponse.setTimeoutHandler(asyncResponse -> { asyncResponse.resume(timeoutResponse.get()); cancelFuture(); }); asyncResponse.setTimeout(timeout.toMillis(), MILLISECONDS); return this; }
@GET @Path("/books/timeouthandlerresume/{id}") public void getBookDescriptionWithHandlerResumeOnly(@PathParam("id") String id, @Suspended AsyncResponse async) { async.setTimeout(1000, TimeUnit.MILLISECONDS); async.setTimeoutHandler(new TimeoutHandlerImpl(id, true)); }
@GET @Path("/books/timeouthandler/{id}") public void getBookDescriptionWithHandler(@PathParam("id") String id, @Suspended AsyncResponse async) { async.setTimeout(1000, TimeUnit.MILLISECONDS); async.setTimeoutHandler(new TimeoutHandlerImpl(id, false)); }
@GET @Path("books/suspend/unmapped") @Produces("text/plain") public void handleNotMappedAfterSuspend(@Suspended AsyncResponse response) throws BookNotFoundFault { response.setTimeout(2000, TimeUnit.MILLISECONDS); response.setTimeoutHandler(new CancelTimeoutHandlerImpl()); throw new BookNotFoundFault(""); }
@GET @Path("/books/cancel") public void getBookDescriptionWithCancel(@PathParam("id") String id, @Suspended AsyncResponse async) { PhaseInterceptorChain.getCurrentMessage().getClass(); async.setTimeout(2000, TimeUnit.MILLISECONDS); async.setTimeoutHandler(new CancelTimeoutHandlerImpl()); }