Java Code Examples for javax.ws.rs.container.AsyncResponse#setTimeout()

The following examples show how to use javax.ws.rs.container.AsyncResponse#setTimeout() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HelloResource.java    From Embedded-Jetty-RESTEasy-Guice-Example with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/async-hello")
@Produces(MediaType.TEXT_PLAIN)
public void getAsyncData(@Suspended final AsyncResponse response,
                         @QueryParam("d") @DefaultValue("1") final int delaySec) throws IOException {

    response.setTimeout(6, TimeUnit.SECONDS);

    new Thread(() -> {
        try {
            Thread.sleep(delaySec * 1000);

            if (!response.isSuspended()) {
                System.out.println("Async response is not suspended");
                return;
            }

            if (!response.resume(Response.ok(helloWord.say()).build())) {
                System.out.println("Async response not resumed");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }).start();
}
 
Example 2
Source File: AsyncResource.java    From ee8-sandbox with Apache License 2.0 6 votes vote down vote up
@GET
public void getAsync(final @Suspended AsyncResponse res) {
    res.setTimeoutHandler(
        (ar) -> {
            ar.resume(Response.status(Response.Status.SERVICE_UNAVAILABLE)
                .entity("Operation timed out --- please try again.").build());
        }
    );
    res.setTimeout(1000, TimeUnit.MILLISECONDS);
    executor.submit(() -> {
        //do long run operations.
        try {
            LOG.log(Level.INFO, " execute long run task in AsyncResource");
            //Thread.sleep(new Random().nextInt(1005));
            Thread.sleep(500);
        } catch (InterruptedException ex) {
            LOG.log(Level.SEVERE, "error :{0}", ex.getMessage());
        }
        res.resume(Response.ok("asynchronous resource").build());
    });
}
 
Example 3
Source File: AsyncResource.java    From ee8-sandbox with Apache License 2.0 6 votes vote down vote up
@GET
public void getAsync(final @Suspended AsyncResponse res) {
    res.setTimeoutHandler(
            (ar) -> {
                ar.resume(Response.status(Response.Status.SERVICE_UNAVAILABLE)
                        .entity("Operation timed out --- please try again.").build());
            }
    );
    res.setTimeout(1000, TimeUnit.MILLISECONDS);
    executor.submit(() -> {
        //do long run operations.
        try {
            LOG.log(Level.INFO, " execute long run task in AsyncResource");
            //Thread.sleep(new Random().nextInt(1005));
            Thread.sleep(500);
        } catch (InterruptedException ex) {
            LOG.log(Level.SEVERE, "error :" + ex.getMessage());
        }
        res.resume(Response.ok("asynchronous resource").build());
    });
}
 
Example 4
Source File: QueryResource.java    From heroic with Apache License 2.0 6 votes vote down vote up
private void bindMetricsResponse(
    final AsyncResponse response,
    final AsyncFuture<QueryResult> callback,
    final QueryContext queryContext
) {
    response.setTimeout(300, TimeUnit.SECONDS);

    httpAsync.bind(response, callback, r -> {
        final QueryMetricsResponse qmr =
            new QueryMetricsResponse(queryContext.queryId(), r.getRange(), r.getGroups(),
                r.getErrors(), r.getTrace(), r.getLimits(),
                Optional.of(r.getPreAggregationSampleSize()), r.getCache());
        queryLogger.logFinalResponse(queryContext, qmr);
        return qmr;
    });
}
 
Example 5
Source File: BookContinuationStore.java    From cxf with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/disconnect")
public void handleClientDisconnects(@Suspended AsyncResponse response) {
    response.setTimeout(0, TimeUnit.SECONDS);

    response.register(new ConnectionCallback() {
        @Override
        public void onDisconnect(AsyncResponse disconnected) {
            System.out.println("ConnectionCallback: onDisconnect, client disconnects");
        }
    });

    try {
        Thread.sleep(3000);
    } catch (InterruptedException ex) {
        // ignore
    }

    response.resume(books.values().toString());
}
 
Example 6
Source File: RestImportService.java    From EDDI with Apache License 2.0 5 votes vote down vote up
@Override
public void importBot(InputStream zippedBotConfigFiles, AsyncResponse response) {
    try {
        if (response != null) response.setTimeout(60, TimeUnit.SECONDS);
        File targetDir = new File(FileUtilities.buildPath(tmpPath.toString(), UUID.randomUUID().toString()));
        importBotZipFile(zippedBotConfigFiles, targetDir, response);
    } catch (IOException e) {
        log.error(e.getLocalizedMessage(), e);
        response.resume(new InternalServerErrorException());
    }
}
 
Example 7
Source File: BookContinuationStore.java    From cxf with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 8
Source File: StreamingAsyncSubscriber.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public void handleTimeout(AsyncResponse asyncResponse) {
    if (queue.isEmpty()) {
        asyncResponse.setTimeout(asyncTimeout, TimeUnit.MILLISECONDS);
    } else {
        resumeAsyncResponse();
    }

}
 
Example 9
Source File: BookContinuationStore.java    From cxf with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 10
Source File: MessagesResource.java    From porcupine with Apache License 2.0 5 votes vote down vote up
@GET
@Path("heavy")
public void getHeavyMessage(@Suspended AsyncResponse ar) {
    service.getHeavyMessage(ar::resume);
    ar.setTimeoutHandler(this::onTimeout);
    ar.setTimeout(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS);
}
 
Example 11
Source File: MessagesResource.java    From porcupine with Apache License 2.0 5 votes vote down vote up
@GET
@Path("light")
public void getLightMessage(@Suspended AsyncResponse ar) {
    service.getLightMessage(ar::resume);
    ar.setTimeoutHandler(this::onTimeout);
    ar.setTimeout(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS);
}
 
Example 12
Source File: SampleResource.java    From wingtips with Apache License 2.0 5 votes vote down vote up
@GET
@Path(ASYNC_TIMEOUT_PATH)
public void getAsyncTimeout(@Suspended AsyncResponse asyncResponse) {
    logger.info("Async timeout endpoint hit");

    asyncResponse.setTimeout(SLEEP_TIME_MILLIS, TimeUnit.MILLISECONDS);
}
 
Example 13
Source File: StreamingAsyncSubscriber.java    From cxf with Apache License 2.0 5 votes vote down vote up
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());
    }
}
 
Example 14
Source File: OrderService.java    From qcon-microservices with Apache License 2.0 5 votes vote down vote up
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()));
}
 
Example 15
Source File: AsynchronousResources.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Produces(TEXT_PLAIN)
@Path("/suspended/resume-timeout")
@GET
public void getAsyncResponseResumeTimeout(@Suspended final AsyncResponse ar) {
    ar.resume("DONE");
    ar.setTimeout(1, MINUTES);
}
 
Example 16
Source File: AsynchronousResources.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Produces(TEXT_PLAIN)
@Path("/suspended/timeout-expire-handled")
@GET
public void getAsyncResponseTimeoutExpireHandled(@Suspended final AsyncResponse ar) {
    ar.setTimeoutHandler(ar2 -> ar2.resume(status(GATEWAY_TIMEOUT).build()));
    ar.setTimeout(1, NANOSECONDS);
}
 
Example 17
Source File: AsynchronousResources.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Produces(TEXT_PLAIN)
@Path("/suspended/timeout-expire")
@GET
public void getAsyncResponseTimeoutExpire(@Suspended final AsyncResponse ar) {
    // Set timeout twice to ensure users can update it at will
    ar.setTimeout(1, MINUTES);
    ar.setTimeout(1, NANOSECONDS);
}
 
Example 18
Source File: BookContinuationStore.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public void handleTimeout(AsyncResponse asyncResponse) {
    if (!resumeOnly && timeoutExtendedCounter.addAndGet(1) <= 2) {
        asyncResponse.setTimeout(1, TimeUnit.SECONDS);
    } else {
        asyncResponse.resume(books.get(id));
    }
}
 
Example 19
Source File: CancellableResources.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
@Path("/suspended")
@GET
public void getForeverSuspended(@Suspended final AsyncResponse ar) {
    // The scheduled task in DefaultContainerResponseWriter will never complete
    ar.setTimeout(7, DAYS);
}
 
Example 20
Source File: BookContinuationStore.java    From cxf with Apache License 2.0 4 votes vote down vote up
@GET
@Path("/books/defaulttimeout")
public void getBookDescriptionWithTimeout(@Suspended AsyncResponse async) {
    async.register(new CallbackImpl());
    async.setTimeout(2000, TimeUnit.MILLISECONDS);
}